Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
459 views
in Technique[技术] by (71.8m points)

c# - Run script file on remote server

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.0Common7IDE"

# 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...