如何在远程计算机上启动.cmd文件?(How can I launch .cmd files on a remote machine?)

编程入门 行业动态 更新时间:2024-10-20 16:38:26
如何在远程计算机上启动.cmd文件?(How can I launch .cmd files on a remote machine?)

我需要能够从文件驻留在该计算机上的目录中启动远程计算机上的.cmd文件。

我试过:在powershell中invoke-command -ComputerName test123 -ScriptBlock { cmd /cc:/myfile.cmd } ,它会启动.cmd,但之后会失败,因为它找不到这个启动的相应.cmd(它们都位于同一目录中)。

有没有办法启动这个.cmd文件,并让它的执行持续存在? 即,即使在PowerShell窗口关闭后,.cmd仍将继续在远程计算机上运行。

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.

I've tried: invoke-command -ComputerName test123 -ScriptBlock { cmd /c c:/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).

Is there a way to launch this .cmd file, and have it's execution persist? i.e., even after the powershell window is closed, the .cmd will continue to run on the remote machine.

最满意答案

您需要更改scriptblock中的工作目录。 在调用批处理脚本之前添加Set-Location :

Invoke-Command -ComputerName test123 -ScriptBlock { Set-Location 'C:\' & cmd /c ".\myfile.cmd" }

如果您需要创建一个分离的进程,您可以通过WMI执行此操作:

$hostname = 'test123' $command = 'C:\path\to\script.cmd' $workdir = 'C:\working\directory' $p = [wmiclass]"\\$hostname\root\cimv2:Win32_Process" $p.Create($command, $workdir)

请注意,您需要远程主机上的管理员权限。

You need to change the working directory in the scriptblock. Add a Set-Location before calling the batch script:

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:

$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.

更多推荐

本文发布于:2023-07-04 17:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1026916.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:机上   文件   如何在   remote   machine

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!