[英]Using Powershell invoke-expression to uninstall Java on all domain computers
我可以ps会话到远程计算机,运行以下命令,然后成功卸载Java: invoke-expression "msiexec /q /x '{26A24AE4-039D-4CA4-87B4-2F83218025F0}' "
我正在尝试创建一个将从所有域计算机上卸载的脚本:
Import-Module ActiveDirectory
function uninstallJava {
$badcomp = @()
$CompList = Get-ADComputer -Filter 'name -like "*"' | select -ExpandProperty Name
foreach ($c in $CompList) {
Try {
Enter-PSSession -ComputerName $computer
Invoke-expression "msiexec.exe /q /x '{26A24AE4-039D-4CA4-87B4-2F83218025F0}' "
}
Catch {
$badcomp += $c
}
}
}
uninstallJava
"the following servers could not be reached:"
$badcomp
我没有收到任何错误,但是它没有从远程计算机上卸载Java。
任何想法表示赞赏。
Import-Module ActiveDirectory
$badcomp = @()
Function uninstallJava {
$CompList = Get-ADComputer -Filter 'name -like "*"' | Select -ExpandProperty Name
ForEach ($c In $CompList) {
Try {
Invoke-Command -ComputerName $c {
C:\Windows\System32\cmd.exe /C msiexec.exe /q /x '{26A24AE4-039D-4CA4-87B4-2F83218025F0}'
}
} Catch {
$badcomp += $c
}
}
}
uninstallJava
Write-Host "the following servers could not be reached:"
$badcomp
您应该使用Invoke-Command
而不是Enter-PSSession
。 后者用于在另一台机器上进行交互工作。 前者用于在另一台机器上运行命令并获取结果(如果有)。
基本上,您的try块应如下所示:
Try {
Invoke-Command -ComputerName $c -ScriptBlock { msiexec.exe /q /x '{26A24AE4-039D-4CA4-87B4-2F83218025F0}' }
}
如果要获得更详细的控制和错误信息,请考虑使用WMI卸载产品,而不是直接使用msiexec
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.