[英]Using Powershell invoke-expression to uninstall Java on all domain computers
I can ps-session to a remote machine, run the following, and successfully uninstall Java: invoke-expression "msiexec /q /x '{26A24AE4-039D-4CA4-87B4-2F83218025F0}' "
我可以ps会话到远程计算机,运行以下命令,然后成功卸载Java: invoke-expression "msiexec /q /x '{26A24AE4-039D-4CA4-87B4-2F83218025F0}' "
I am trying to create a script that will uninstall from all domain computers: 我正在尝试创建一个将从所有域计算机上卸载的脚本:
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
I don't receive any errors, but it doesn't uninstall Java from the remote machines. 我没有收到任何错误,但是它没有从远程计算机上卸载Java。
Any ideas appreciated. 任何想法表示赞赏。
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
You should be using Invoke-Command
, not Enter-PSSession
. 您应该使用Invoke-Command
而不是Enter-PSSession
。 The latter is for interactively working in another machine. 后者用于在另一台机器上进行交互工作。 The former is for running a command in the other machine and getting back results (if any). 前者用于在另一台机器上运行命令并获取结果(如果有)。
Basically, your try block should look like this: 基本上,您的try块应如下所示:
Try {
Invoke-Command -ComputerName $c -ScriptBlock { msiexec.exe /q /x '{26A24AE4-039D-4CA4-87B4-2F83218025F0}' }
}
If you want more detailed control and error information, consider using WMI to uninstall the product instead of shelling out to msiexec
. 如果要获得更详细的控制和错误信息,请考虑使用WMI卸载产品,而不是直接使用msiexec
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.