C# 如何使批处理脚本使用凭据在远程计算机上运行命令

C# 如何使批处理脚本使用凭据在远程计算机上运行命令,c#,docker,batch-file,remote-server,C#,Docker,Batch File,Remote Server,我编写了一些批处理脚本来在机器上执行docker命令,并使用c#程序使其运行。我可以在本地机器上成功运行这些批处理脚本,但我希望它在远程机器上运行。如何通过提供远程计算机访问凭据来实现这一点?我正在寻找批处理命令,这些命令将首先连接到远程机器,然后在远程机器中执行其主体。 下面是一个示例批处理脚本 ::Start an exited container ::Iam expecting some code here to connect to a remote machine @echo off

我编写了一些批处理脚本来在机器上执行docker命令,并使用c#程序使其运行。我可以在本地机器上成功运行这些批处理脚本,但我希望它在远程机器上运行。如何通过提供远程计算机访问凭据来实现这一点?我正在寻找批处理命令,这些命令将首先连接到远程机器,然后在远程机器中执行其主体。 下面是一个示例批处理脚本

::Start an exited container
::Iam expecting some code here to connect to a remote machine
@echo off
docker start %1

或者,有没有通过c#程序实现的选项

请尝试使用WinRM连接。一些样本信息-

或Windows服务器上的ssh服务器-示例


希望有帮助。

我找不到在远程计算机上运行批处理脚本的方法,但我使用了powershell脚本而不是批处理脚本。如果可以使用powershell脚本而不是批处理脚本,则可以使用此方法

set-item wsman:localhost\client\trustedhosts -value <Remote_computer_name> -Force
$pword = ConvertTo-SecureString -String <Remote_machine_password> -AsPlainText -Force
$user = <Remote_userName>
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user, $pword
Set-ExecutionPolicy -ExecutionPolicy Bypass
Invoke-Command -ComputerName <Remote_computer_name> -filepath <ps1_file_path> -Credential $Credential
设置项目wsman:localhost\client\trustedhosts-value-Force
$pword=converttosecurestring-String-AsPlainText-Force
$user=
$Credential=新对象-TypeName System.Management.Automation.PSCredential-ArgumentList$user,$pword
设置ExecutionPolicy-ExecutionPolicy旁路
调用命令-ComputerName-filepath-Credential$Credential
在powershell管理员窗口中运行上述命令将在远程计算机中运行
中的脚本


你必须考虑这个命令。
设置项目wsman:localhost\client\trustedhosts-value-Force
将远程计算机添加到受信任的主机列表中。 另外,命令
Set ExecutionPolicy-ExecutionPolicy Bypass
将允许所有powershell脚本在我们的机器上运行,我不确定使用是否安全。 希望它能在某种程度上帮助一些人,但你必须做研究,以确保这种方法是安全的。 参考:

尝试使用PsExec


这是telnet的一个轻量级替代品

有任何选项可以通过ssh连接到该机器吗?