首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过Invoke-Command运行CMD

如何通过Invoke-Command运行CMD
EN

Stack Overflow用户
提问于 2018-05-25 06:47:07
回答 2查看 9.3K关注 0票数 0
代码语言:javascript
复制
$remoteinst = "\Windows\Temp\MyFolder"
$remotecomp = ComputerName
$remotesess = New-PSSession $remotecomp
$remotedir = "C:" + $remoteinst + "\Install.cmd"
Invoke-Command -Session $remotesess -ScriptBlock {$remotedir}

我正在尝试在远程计算机上运行Install.cmd文件。我意识到我不能通过Enter-PSSession传递命令,但我正在努力解决这个问题。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-05-25 09:37:11

  • 无需创建显式会话:可以直接将目标计算机名传递给Invoke-Command -ComputerName <computerName>.
  • Invoking需要调用运算符&的命令。
  • 传递给Invoke-Command -ComputerName ...的脚本块是远程执行的,因此不能直接在其中使用局部变量;在PSv3+中,解决此问题的最简单方法是使用using作用域:$using:<localVarName>

记住所有这些要点,我们得到:

代码语言:javascript
复制
$remoteinst = "\Windows\Temp\MyFolder"
$remotecomp = ComputerName # Note: This syntax assumes that `ComputerName` is a *command*
$remotedir = "C:" + $remoteinst + "\Install.cmd"
Invoke-Command -ComputerName $remoteComp -ScriptBlock { & $using:remotedir }
票数 2
EN

Stack Overflow用户

发布于 2018-05-25 07:59:12

cmd /c添加到批处理文件路径的前面。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50519225

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档