How can I launch .cmd files on a remote machine?

蹲街弑〆低调 提交于 2019-12-05 21:40:30

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!