使用 PowerShell 2.0 在远程计算机上运行批处理文件 [英] Running batch file on Remote Computers using PowerShell 2.0
问题描述
我正在尝试在远程机器上运行一个 exe,这基本上会卸载产品代理.代码如下:
I am trying to run a exe on remote machines which would basically uninstall a product agent. below is the code:
$test = Get-Content PC.txt
foreach ($a in $test)
{
$curr = Get-Location
Set-Location \\$a\Admin$\System32\CCMSetup
.\ccmsetup.exe /uninstall
Set-Location $curr
}
它不起作用.我最终从主机上删除了该程序:)
It doesn't work. I ended up removing the program from the host computer itself :)
替代选项:我使用命令行创建了一个批处理文件:
Alternate Option: I created a batch file with the command line:
cd C:\Windows\System32\ccmsetup
ccmsetup /uninstall
exit
使用 Invoke-Command 似乎也可以实现上述目的.
It seems the above can also be achieved using Invoke-Command.
Invoke-Command -ComputerName $client -FilePath UninstallCCM.cmd
显然,它不接受批处理文件.我想保持它尽可能简单.
Apparently, it does not accept batch file. I would like to keep it as simple as possible.
目前我使用 PSExec 来安装和卸载程序.我是否需要在需要使用 PowerShell 执行脚本的每台远程计算机上启用 PS Remoting (WinRM)?有人可以帮忙吗?提前致谢.
Currently I am using PSExec for installing and uninstalling the program. Do I need to enable PS Remoting (WinRM) on every remote machine on whom I need to execute scripts using PowerShell? Can someone please help? Thanks in advance.
推荐答案
这个命令应该能成功执行:
This command should execute successfully:
Invoke-Command -ComputerName $client -ScriptBlock { cd C:\Windows\System32\ccmsetup; ccmsetup /uninstall} -Credential $(Get-Credential) -Authentication CredSSP
但是您需要通过在每台机器上运行这两个命令来在所有机器上启用 CredSSP 身份验证:
but you will need to enable CredSSP authentication on all machines by running these two commands on each machine:
Enable-WsManCredSSP -Role Server -Force
Enable-WSManCredSSP -Role Client -DelegateComputer * -Force
这篇关于使用 PowerShell 2.0 在远程计算机上运行批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!