简体   繁体   English

如何在远程计算机上启动.cmd文件?

[英]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.

相关问题 如何通过Windows 7计算机中的cmd提示符启动appium节点服务器 - how to launch appium node server through cmd prompt in windows 7 machine 如何列出远程计算机文件夹的内容 - How I can list the contents of a folder of a remote machine 如何从远程 Windows 计算机检索机器名称? - How can I retrieve the machine name from a remote Windows computer? 如何在远程计算机上启动和停止服务? - How can I start and stop services on a remote machine? 如何要求远程Windows机器自动启动应用程序? - How to ask a remote windows machine to automatically launch an application? 如何将以下 cmd 用于多个文件? - How can I use the following cmd for multiple files? 我如何找到 cmd 中两个文件之间的区别 - How can i find the difference between two files in cmd EvtExportLog API 将远程机器的事件日志保存在远程 PC 本身中。 如何将其保存到主机 PC? - EvtExportLog API is saving eventlog of remote machine in remote PC itself. How can I save it to host PC? 如何强制Jenkins在客户端(用户)计算机上启动webpage / cmd提示符? - How to force Jenkins to launch webpage/ cmd prompt in client (user's) machine? 如何获取存储在主机中的文件? - How can I get the files stored in host machine?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM