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

查看:913
本文介绍了我如何在远程计算机上启动.cmd文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够在文件驻留在该计算机上的目录中启动位于远程计算机上的.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.

我试过:在powershell中调用命令-ComputerName test123 -ScriptBlock {cmd /cc:/myfile.cmd} ,启动.cmd,但是失败,因为它找不到相应的.cmds这个启动(它们都驻留在同一目录)。

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

有没有办法启动这个。 cmd文件,并让它的执行持久?即,在powershell窗口关闭之后,.cmd将继续在远程机器上运行。

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.

推荐答案

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

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"
}






如果你需要创建一个分离的进程,你可以通过例如WMI:


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.

这篇关于我如何在远程计算机上启动.cmd文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆