How Reboot(Restart) vps with c# codes? - c#

I want to write some codes for a server that can Reboot(Restart) all vps s or one of them inside that server!
VPS = VIRTUAL PRIVATE SERVER
i have some questions about that:
how can i write those codes with c# codes for that job? and how can i recognize every vps inside server?
where should i run those codes ? form a vps inside that server or
another place?(want to access all vps s inside server using VMware)
really appreciate for viewing and help
stack is always my love

Taken from this SO question
Process.Start("shutdown","/s /t 0");
(fits Windows machines only)
Now, combine this with a WCF service running on each VPS - you will have to code a management console (that you will use later on your computer, for instance) that will know (or, have features to add) a known VPS server (by, for example, it's IP address), and allow you to invoke this function on the remote VPSs
This way, you`ll have a service running on each VPS that allows you to sreboot, while having a management console to send shut-down directives to all or some of your servers from a remote computer

In combination with the process.Start solution form Shai you could use psshutdown.
It does require that the boxes are domain joined, from the docs:
... and if you specify a wildcard (\*), the command runs on all computers
in the current domain

Related

PHP code located on linux server needs to run windows .exe

The project I am apart of uses a Linux server to host our code. The API I was provided to use was only usable within C#, so currently I have a Windows executable that performs a small task. This executable is called to execute by a PHP file from a website.
Currently, I am attempting to run the file remotely from the Linux server by letting the Windows executable reside on a server running Windows and doing a remote execute call using http://php.net/manual/en/features.remote-files.php, but even if this works, it doesn't seem like a safe or correct way to do things.
A senior suggested I use an IIS server to call it securely, but I am not familiar with this. Before continuing, I thought it would be responsible to ask: is there is a way to have this PHP code residing on a Linux server to run a Windows executable without having to call a remote Windows server for it to run on.
You could write a small web API to run on your Windows server, that after authenticating the request will allow you to execute certain commands locally. Your application running on the linux server could then send requests to this web API and either wait for the results to be returned in that request, or have the Windows server send the results back in an API request later.
The other option which is arguably simpler... just host the entirety of your application on the Windows server.

Check whether application is running under Terminal Services as opposed to Remote Desktop

I have a .NET application where I need to be able to disallow running it on a Terminal Services server for licensing reasons. I am aware of the SystemInformation API to determine if a program is running under a remote session (as detailed in this question for example). However, that API does not distinguish between an application running on a normal server which has the 2 allowed remote desktop sessions and a full-blown terminal server which might have 500 sessions running on it.
Is there a simple programmatic way that I can determine whether the application is running on a full-on terminal server?
I am fine with the assumption that more than 2 allowed sessions means a full-on terminal server, and I am also fine with P/Invoke if that's what's required.
One way is to use the below code:
string s = System.Environment.GetEnvironmentVariable("SessionName");
If the value of s is "Console", it could be running in terminal services. On the other hand, if the value is something like "RDP-Tcp#01", it is running under Remote Desktop.

Checking "connectivity" between remote server A and remote server B programmatically

We have a requirement (I believe as part of a post build/deployment verification test (BVT)) to test that remote server A can "connect" to remote server B.
I want to develop a console application using C# on my LOCAL developer VM. This should then "connect" to remote server A. It then needs to check that remote server A can "connect" to remote server B (via the original connection from LOCAL to remote server A).
Currently I have generated a simple "helper" exe that connects to remote server B (using a prior known connection string point to remote server B and SqlConnection classes). The exe is copied to remote server A as part of the build process (at the end). PSEXEC.exe on the build server then remotely starts the "helper" exe on remote server A and checks the %errorcode%. This then determines if the BVT should pass or fail (Then the "helper" exe is then deleted from remote server A).
FYI all machines run Windows Server 2008 R2 x64 Enterprise and are on the same local domain. I am an administrator on all 3 machines.
Remote server A is a web server and remote server B is a SQL database server. The LOCAL machine will/does represent the build server. This test is simple as saying "can the web server connect to the database server". The difficulty is how to do that remotely.
I know I can just do System/integration/end-to-end testing (using Coded UI etc) and we are doing this but want to consider this requirement/test as well (as the servers and network configuration etc are a "shared responsibility" with an external party and we cant guarantee that anything has/will change without our knowledge (other than reacting via Service Desk processes))
The process I have adopted seems OTT but does appear to work (if I close ports etc). Is there a completely .NET way of doing it using TCPCLient classess or something etc? What about doing webservices and hosting them on the webserver and call them from the build server? Other ideas and things to consider more than welcome.
Why not create a test page on the web server that carries out an innocuous operation against the SQL server.
The page might execute a simple query (select top(1) name from person) for example.
I can see web-services tagged in your question. If you want to verify connectivity to a server hosting web services or between a server hosting web services and an underlying back-end server (database for example), the best way to be sure (in my opinion) would be to call an innocuous service that does a simple read. Retrieving WSDLs would not be enough.
You can use Management objects to query and control computers and servers on your domain.
http://msdn.microsoft.com/en-us/library/system.management.aspx
SQL server includes some extra objects specific to managing SQL Server.
http://msdn.microsoft.com/en-us/library/ms220749.aspx
If you have Linqpad:
ManagedComputer mc = new ManagedComputer( "yourserver" );
mc.Dump();
otherwise create a console app and create an instance of ManagedComputer and use the debugger to query the computer's SQL server, to see the sort of information available.
As you can see its a big subject...

C# - command line application calls

I have done plenty of C# shell command calls, apps, batch files etc. The other day I was asked if it would be a problem if an executable that I currently run from my web site app, would move to another server on our intranet.
In other words the web site app and the executable that I am running through Process.Start(...) are located on the same box currently - all good there. Now there is a wish to separate the two on two different servers.
I done a few futile attempts to execute an app (located on server B) from server A (where the web site resides).
Is there a way that I have not run cross yet to do this ?
Thanks
PsExec is one way with minimal coding. Using System.Diagnostic.Process, you can call this command:
psexec \\ServerB (path)\myapp.exe arg0 arg1 ...
To control the processes of server A by running an application on server B, you would need an application running on server B that would get controlled remotely somehow.
As an example, let's say server A runs unix, so you could write a application that would connect to server A using ssh, authenticate and then control the processes and whatnot like you do in a shell. If server A does not allow ssh connection, you could write your own application that would be running on server A listening to some connection and commands that would come from an application in server B.
It's quite hard to understand what are your current settings and why would you even want to switch the application from server A to server B, so a little more information would wield you a better answer.
Austin's PSExec approach is probably the easiest approach to executing an EXE on a remote machine, but you may want to consider a potentially more robust solution:
You could modify your command-line app to run as a service and to respond to requests for work and/or data via a WCF (binaryXML/TCP or XML/HTTP) call.

asp/php communication to local service/application

I have a local application which parses data. What i need is to develop a web interface to query statistics and set configurations for the application thats running at all times. Since I am the developer of both applications I have full access to both source.
My main goals here are:
- have a service or app running at all times on the webserver doing most of the work
- have a webUI which will connect to this app and display stats and make configurations
- service/app will be writen in C#
- WebUI will be written in asp or php
- the WebUI must be accessable through the web domain ie: http://server/TestApp on default port 80 were there are other web apps running already.
I know there are ways to do this using a specific port to communicate to eachother, but i would like to avoid this. What is the best way to communicate between these apps?
Thanks,
-Stewart
Use a FileWatcher in C# to watch a file that PHP can read/write to. When something needs parsed, you can try it that way.
You could also give PHP permissions to exec or use the ` character to execute your program which return the result to a varible ex:
$var = `C:\What\Ever\program.exe $param1`;
The local port listener is not a bad idea though.

Categories