[英]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.