我的任务是实现内部流程的自动化。 此过程首先涉及登录到远程服务器 (A)。 用户将从服务器 A 连接到远程服务器 (B)。
通过服务器 B 的身份验证后,用户需要执行三个主要任务:
- 更改本地目录中的文件名。
- 打开命令提示符并执行“iisreset”
- 打开 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/