且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在远程服务器上运行脚本文件

更新时间:2023-02-22 20:50:54

你可以使用 Powershell 新建 PSSession.

You could use Powershell New-PSSession.

摘自微软网站:

New-PSSession cmdlet 创建 Windows PowerShell 会话(PSSession) 在本地或远程计算机上.当您创建一个PSSession,Windows PowerShell 建立持久连接到远程计算机.

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.

将以下内容保存到 PS1 文件中:

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

一旦您有了脚本,您就可以按照 PowerShell 类.

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