[英]How can I launch .cmd files on a remote machine?
I need to be able to launch a .cmd file that is on a remote machine, from within the directory that the file resides on that machine . 我需要能够从文件驻留在该计算机上的目录中启动远程计算机上的.cmd文件。
I've tried: invoke-command -ComputerName test123 -ScriptBlock { cmd /cc:/myfile.cmd }
in powershell, which launches the .cmd, but then fails because it can't find the corresponding .cmds that this one launches (which all reside in the same directory). 我试过:在powershell中
invoke-command -ComputerName test123 -ScriptBlock { cmd /cc:/myfile.cmd }
,它启动.cmd,但后来失败,因为它找不到这个启动的相应.cmd(它们都位于同一目录中)。
Is there a way to launch this .cmd file, and have it's execution persist? 有没有办法启动这个.cmd文件,并让它的执行持续存在? ie, even after the powershell window is closed, the .cmd will continue to run on the remote machine.
即,即使在PowerShell窗口关闭后,.cmd仍将继续在远程计算机上运行。
You need to change the working directory in the scriptblock. 您需要更改scriptblock中的工作目录。 Add a
Set-Location
before calling the batch script: 在调用批处理脚本之前添加
Set-Location
:
Invoke-Command -ComputerName test123 -ScriptBlock {
Set-Location 'C:\'
& cmd /c ".\myfile.cmd"
}
If you need to create a detached process, you can do that for instance via WMI: 如果您需要创建一个分离的进程,您可以通过WMI执行此操作:
$hostname = 'test123'
$command = 'C:\path\to\script.cmd'
$workdir = 'C:\working\directory'
$p = [wmiclass]"\\$hostname\root\cimv2:Win32_Process"
$p.Create($command, $workdir)
Note that you need admin privileges on the remote host for this. 请注意,您需要远程主机上的管理员权限。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.