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

本文关键字:运行 脚本 文件 服务器 | 更新日期: 2023-09-27 17:59:50

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

一旦在服务器B上进行了身份验证,用户需要执行三个主要任务:

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

我在CodeProject上的一篇文章中使用了一些示例代码来进行所有的远程桌面连接,它可以正常工作。代码使用ActiveX MSTSC库。

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

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

谢谢你的帮助。

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

您可以使用Powershell New PSSession。

摘录自微软网站:

New-PSSessioncmdlet创建Windows PowerShell会话(PSSession)。当您创建PSSession,Windows PowerShell建立到的持久连接远程计算机。

将以下内容保存到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类中的示例对C#应用程序进行建模。

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