Run the script file on the remote server

I have been tasked with automating the internal process. This process involves first logging on to the remote server (A). From server A, the user will connect to the remote server (B).

After authentication on server B, users need to complete three basic tasks:

  • Change the file name in the local directory.
  • Open a command prompt and run "iisreset"
  • Open Internet Explorer to reestablish communication with IIS.

I used some sample code form in the CodeProject post to make all remote desktop connections and work without problems. Codes use the ActiveX MSTSC Library .

My question is the steps described above. After connecting to server B, it’s pragmatic for me to perform these operations. Right now we have an internal script that performs these actions. To execute the script, the user must go to server B and manually run the Script.

If it is possible to make connections and execute the script pragmatically, this would be a solution. If for some reason this is not possible, I would have to follow three steps pragmatically as a solution instead.

Thank you for your help.

+2
source share
2 answers

You can use Powershell New-PSSession .

Excerpt from the Microsoft website:

The New-PSSession cmdlet creates a Windows PowerShell (PSSession) session on a local or remote computer. When you create a PSSession, Windows PowerShell establishes a permanent connection to the remote computer.

Save the following in a PS1 file:

Write-Output "Please supply your credential for <insert server name>" $cred = Get-Credential -Message "Enter credential for <insert server name>" Try { $s1 = new-pssession -computername <fully qualified domain name (FQDN)> q -credential $cred -Authentication Credssp } Catch [system.exception] { "Your account doesn't have access to <insert server name>" "Not sure what permission are really require on the server" "When I have a chance, need to test with the other developers" } # If you wanted to set a path you could do this $env:Path = $env:Path + ";C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE" # Create the command we want to execute on the remote server # This block can contain anything you do at command prompt $block = { dir cd "\Foo" .\Bar.bat } if ($s1) { Invoke-Command $block -session $s1 remove-pssession $s1 } Start-Sleep 5 

Once you have the script, you can model your C # application after it, following the example from the PowerShell Class .

+2
source

You can use psexec to run scripts on another computer. It comes with sysinternals package.
The command will look like this:
psexec \ serverB <local script path>

0
source

All Articles