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

Run script file on remote server

我的任务是使内部流程自动化。 此过程涉及首先登录到远程服务器 (A)。 从服务器 A,用户将连接到远程服务器 (B)。

一旦通过服务器 B 的身份验证,用户需要执行三个主要任务:

  1. 更改本地目录中的文件名。
  2. 打开命令提示符并执行"iisreset"
  3. 打开 Internet explorer 以确保重新建立与 IIS 的连接。

我在 CodeProject 上使用了 post 中的一些示例代码来建立所有远程桌面连接,并且工作正常。 代码使用 ActiveX MSTSC Library.

我的问题在于上述步骤。 连接到服务器 B 后,我如何务实地执行这些操作。 现在,我们确实有一个执行这些步骤的内部脚本。 要执行脚本,用户必须登录到服务器 B 并手动 运行 脚本。

如果可以建立连接并实用地执行脚本,这将是一个解决方案。如果出于某种原因这是不可能的,我将需要以务实的方式执行这三个步骤作为解决方案。

感谢您的帮助。

您可以在另一台机器上使用 psexec 运行 脚本。它带有 sysinternals 套件。
命令如下所示:
psexec \\serverB <脚本的本地路径>

您可以使用 Powershell New-PSSession

微软网站摘录:

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 文件中:

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

获得脚本后,您可以按照 PowerShell Class 中的示例为 C# 应用程序建模。