2

I'm tasked with automating an internal process. This process involves first being logged on a remote server (A). From server A, a user would connect to Remote Server (B).

Once authenticated onto Server B, the users need to perform three main tasks:

  1. Change a file name in a local directory.
  2. Open command prompt and perform "iisreset"
  3. Open Internet explorer to ensure the connection with IIS is re-established.

I've used some sample code form a post on CodeProject to make all the remote desktop connections and it's working without issue. The codes uses the ActiveX MSTSC Library.

My question lies in the steps outlined above. Once connected to Server B, how do I pragmatically perform these operations. Right now, We do have an internal script which performs these steps. To execute the script, the user must log onto Server B and manually run the Script.

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

Thanks you for your help.

Stephen Sugumar
  • 545
  • 3
  • 9
  • 35
  • 1
    Does this need to happen at a specific time? Could you just have a process running on server B that checks if it needs to run the script? – Tim Freese Jun 25 '15 at 17:15
  • No, there is no time interval to the process. It happens on the fly as it is a support services for a product. – Stephen Sugumar Jun 25 '15 at 17:16

2 Answers2

2

You could use Powershell New-PSSession.

Excerpt from Microsoft Site:

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

Save the following to 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 a script, you can model you C# application after it following the example from PowerShell Class.

Black Frog
  • 11,595
  • 1
  • 35
  • 66
  • Hey, thank you for the response. I'll look into running a PS1 file from a C# program. The requirements state the program must be built on a .NET platform since it has some GUI components and extra features. – Stephen Sugumar Jun 25 '15 at 17:28
  • 1
    My script is just showing what the powershell script would look like. Then to run it from c# executable, you could do the following: [Using C# to execute PowerShell script with command line args using V2 methods](http://stackoverflow.com/questions/17867636/using-c-sharp-to-execute-powershell-script-with-command-line-args-using-v2-metho) – Black Frog Jun 25 '15 at 17:31
0

You may use psexec to run scripts on another machine. It comes with the sysinternals suite.
Command would seem like below:
psexec \\serverB <local path of the script>

AlThomas
  • 4,169
  • 12
  • 22
Abdullah Nehir
  • 1,027
  • 13
  • 23