Uninstalling using Get-WmiObject

后端 未结 2 1636
轮回少年
轮回少年 2021-01-25 08:32

I\'m trying to run a PowerShell command in a batch script. Im trying to remove all traces of an old RMM tool from client PCs and for the life of me can\'t get this line to run c

2条回答
  •  旧时难觅i
    2021-01-25 09:03

    Check this snippet, it is using WMI but in another way, and has almost never failed me :

    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) {
            $classKey = "IdentifyingNumber=`"$pin`",Name=`"$pn`",version=`"$pv`""
    
            $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"
        }
    }
    

    Example usage :

    Uninstall-Application "127.0.0.1" "firefox"
    

提交回复
热议问题