在远程计算机上使用PowerShell卸载软件?

时间:2018-01-12 10:16:03

标签: powershell

我正在使用此脚本卸载软件(例如Java)。但是,我的计算机上有两个Java包,Java 8 Update和Java Auto Updater。

我理解当我在这样一台机器上运行我的脚本时,这台机器有两个与我的正则表达式相匹配的软件(在这种情况下为Java*),它无法生成一个对于远程机器而言唯一的$classkey #39; s WMI,它就是这样的情况。

function Uninstall-Application($computer, $target) {
    $productInfo = Get-WmiObject Win32_Product -Filter "Name LIKE '%$target%'" -ComputerName $computer
    $pin = $productInfo.IdentifyingNumber
    $pn = $productInfo.Name
    $pv = $productInfo.Version

    if ($pn -ne $null) {
        for ($i = 0; $i -lt $Productinfo.length; $i++) {
            $classKey = "IdentifyingNumber=`"$pin[$i]`",Name=`"$pn[$i]`",version=`"$pv[$i]`""
            $uninstallReturn = ([wmi]"\\$computer\root\cimv2:Win32_Product.$classKey").uninstall()
            if ($uninstallReturn.ReturnValue -ge 0) { Write-Host "Uninstall complete"}
            else { $uninstallReturn | Out-Host }
        } 
    }
    else {
        Throw "Product not found"
    }
}

uninstall-application "RemoteServer" "Java"

此代码有效,如果只有一个软件与我的正则表达式匹配。

1 个答案:

答案 0 :(得分:1)

您可以使用Private Sub Call_Qualifier() Dim Match As Integer Match = Qualifier("This is my lmn trial code") Select Case Match Case 1 Debug.Print """ABC"" was found" Case 2 Debug.Print """DEF"" was found" Case 3 Debug.Print """LMN"" was found" Case 0 Debug.Print "No match was found" End Select End Sub Function Qualifier(ByVal Code As String) As Integer ' 12 Jan 2018 ' return 0 if not found Dim Fun As Integer ' function return value Dim Crits() As String Crits = Split("ABC,DEF,LMN,RST,MIN,MAX,SDR,USA", ",") For Fun = UBound(Crits) To 0 Step -1 If InStr(1, Code, Crits(Fun), vbTextCompare) Then Exit For Next Fun Qualifier = Fun + 1 End Function 循环针对多个项目运行卸载代码。

foreach

全功能:

$products = Get-WmiObject Win32_Product -Filter "Name LIKE '%$target%'" -ComputerName $computer
foreach ($productInfo in $products) {
    #uninstall code
}