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

标签 c# .net batch-file scripting remote-access

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

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

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

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

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

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

感谢您的帮助。

最佳答案

您可以使用 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# 应用程序进行建模。 .

关于c# - 在远程服务器上运行脚本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31056573/

相关文章:

c# - 无法在 Visual Studio 2008 中创建 list 资源名称错误

c# - 使用 SqlConnection 打开 DataReader 错误

c# - 在 C# 中获取内存中下一个对象大小的空间

c# - NLog 到数据库 (Oracle)

c# - 使用平移和缩放计算正确的光标位置

C#接口(interface)问题

Windows 8 cli 截图

batch-file - 如何在 Windows 启动时自动启动批处理文件

windows - 使用 Windows 批处理文件在文本文件中添加新行

c# - 如何使连接字符串在 T4 模板中可用?