简体   繁体   English

PowerShell从远程计算机上我的计算机上的位置运行bat

[英]PowerShell run bat from location on my computer on remote computer

I need to run a BAT file on some servers Because of the amount of many servers I avoid copying the file on any server 我需要在某些服务器上运行BAT文件由于有许多服务器,因此我避免在任何服务器上复制文件

I have the file on my computer and I try to access it remotely 我的计算机上有文件,我尝试远程访问它

$CMDCOMMAND = "\\Mycomp\c$\file.bat"    
Invoke-Command -ComputerName $Hostname -Credential $Cred -ScriptBlock {
    Start-Process cmd.exe "/c $CMDCOMMAND
}
Invoke-Command -Computer $Hostname -Credential $Cred -FilePath $CMDCOMMAND

should work as @Ansgar Wiechers mentioned. 应该像@Ansgar Wiechers提到的那样工作。

I doubt you have set $CMDCOMMAND = "\\\\Mycomp\\c$\\file.bat" and tried. 我怀疑您已设置$CMDCOMMAND = "\\\\Mycomp\\c$\\file.bat"并尝试过。 It should be local path when you pass it to -FilePath 传递给-FilePath时,它应该是本地路径

$CMDCOMMAND = "c:\file.bat"

Invoke-Command does not work with a variable defined outside the ScriptBlock. Invoke-Command不适用于在ScriptBlock外部定义的变量。 You need to use the ArgumentList parameter. 您需要使用ArgumentList参数。 Something like this should work, although I have not tested it: 像这样的东西应该可以工作,尽管我还没有测试过:

$CMDCOMMAND = "\\Mycomp\c$\file.bat"    
Invoke-Command -ComputerName $Hostname -Credential $Cred -ScriptBlock {
param($ARGLIST) Start-Process cmd.exe "/c $ARGLIST" 
} -ArgumentList $CMDCOMMAND

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在本地执行powershell脚本以在远程计算机上运行 - Executing a powershell script locally to run on a remote computer 如何在远程计算机上运行 PowerShell 脚本? - How to run PowerShell script on a remote computer? Powershell 将计算机调用到在我的本地计算机重新启动后仍然存在的远程计算机? - Powershell invoke-computer to a remote that survives a reboot of my local computer? Powershell远程计算机会话 - Powershell Remote Computer Session 如何使用PowerShell脚本在远程计算机上运行另一个PowerShell脚本 - How to use a PowerShell script to run another PowerShell Script on a Remote Computer PowerShell:如何在“ CurrentUser”商店位置的远程计算机上安装PFX证书? - PowerShell: How to install a PFX certificate on a remote computer in 'CurrentUser' store location? powershell-如何在远程计算机上映射网络驱动器(如何在远程计算机上运行cmd命令) - powershell - how to map network drive on remote computer (how to run cmd commands on remote computer) 如何将参数传递到Powershell脚本并在远程计算机上运行 - how to pass parameters to Powershell script and run it on a remote computer 我应该如何在远程计算机上运行 Powershell 脚本? - How should I run a Powershell script on a remote computer? 传递凭据以使用PowerShell在远程计算机上运行批处理脚本 - Pass credentials to run batch script on remote computer with PowerShell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM