如何使用凭据在远程计算机上为 运行 命令制作批处理脚本
How to make a batch script to run commands on a remote machine using credentials
我已经编写了一些批处理脚本来在机器上执行 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 服务器 - 示例
WinSCP
希望对您有所帮助。
我未能找到在远程计算机上 运行 批处理脚本的方法,但我使用了 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
运行 powershell 管理员中的上述命令 window 将 运行 远程计算机中 <ps1_file_path>
中的脚本。
你必须考虑到,命令
set-item wsman:localhost\client\trustedhosts -value <Remote_computer_name> -Force
将远程机器添加到受信任的主机列表中。
此外,命令 Set-ExecutionPolicy -ExecutionPolicy Bypass
将允许所有 powershell 脚本在我们的机器上 运行,我不确定使用它是否安全。
希望它能以某种方式帮助某人,但你必须进行研究以确保这种方法是安全的。
参考:
尝试使用 PsExec
https://docs.microsoft.com/en-us/sysinternals/downloads/psexec
这是 telnet 的轻量级替代品
我已经编写了一些批处理脚本来在机器上执行 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 服务器 - 示例 WinSCP
希望对您有所帮助。
我未能找到在远程计算机上 运行 批处理脚本的方法,但我使用了 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
运行 powershell 管理员中的上述命令 window 将 运行 远程计算机中 <ps1_file_path>
中的脚本。
你必须考虑到,命令
set-item wsman:localhost\client\trustedhosts -value <Remote_computer_name> -Force
将远程机器添加到受信任的主机列表中。
此外,命令 Set-ExecutionPolicy -ExecutionPolicy Bypass
将允许所有 powershell 脚本在我们的机器上 运行,我不确定使用它是否安全。
希望它能以某种方式帮助某人,但你必须进行研究以确保这种方法是安全的。
参考:
尝试使用 PsExec https://docs.microsoft.com/en-us/sysinternals/downloads/psexec
这是 telnet 的轻量级替代品