gpt4 book ai didi

powershell - 使用 PowerShell 2.0 在远程计算机上运行批处理文件

转载 作者:行者123 更新时间:2023-12-04 22:21:53 25 4
gpt4 key购买 nike

我正在尝试在远程机器上运行一个 exe,这基本上会卸载产品代理。下面是代码:

$test = Get-Content PC.txt

foreach ($a in $test)
{
$curr = Get-Location
Set-Location \\$a\Admin$\System32\CCMSetup
.\ccmsetup.exe /uninstall
Set-Location $curr
}

它不起作用。我最终从主机上删除了该程序:)

替代选项:我使用命令行创建了一个批处理文件:
cd C:\Windows\System32\ccmsetup
ccmsetup /uninstall
exit

似乎上述内容也可以使用 Invoke-Command 来实现。
Invoke-Command -ComputerName $client -FilePath UninstallCCM.cmd

显然,它不接受批处理文件。我想保持它尽可能简单。

目前我正在使用 PSExec 来安装和卸载程序。我是否需要在需要使用 PowerShell 执行脚本的每台远程计算机上启用 PS Remoting (WinRM)?
有人可以帮忙吗?提前致谢。

最佳答案

此命令应成功执行:

Invoke-Command -ComputerName $client -ScriptBlock { cd C:\Windows\System32\ccmsetup; ccmsetup /uninstall} -Credential $(Get-Credential) -Authentication CredSSP

但是您需要通过在每台机器上运行以下两个命令来在所有机器上启用 CredSSP 身份验证:
Enable-WsManCredSSP -Role Server -Force
Enable-WSManCredSSP -Role Client -DelegateComputer * -Force

关于powershell - 使用 PowerShell 2.0 在远程计算机上运行批处理文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15336336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com