简体   繁体   中英

How to remote uninstall applications with PowerCLI and PowerShell

I created this script to remotely uninstall VMware tools from a group of Vm's.

Get-Module -ListAvailable PowerCLI* | Import-Module

Connect-VIServer -Server 192.168.42.218 -User administrator@vsphere.local -Password mypassword

$GetVm=(Get-VM).where{$_.ExtensionData.Config.GuestFullname -match 'Windows'} | select -expand Name | Out-File -FilePath .\vms.txt

$source = "vms.txt"

$vms = Get-Content -Path $source

foreach ($vmName in $vms) {
$vm = Get-VM -Name $vmName

$app = Get-WmiObject -Query "SELECT * FROM Win32_Product WHERE Name LIKE '%VMware%'"  -ComputerName $vmName


    $app.Uninstall()
    }

I get the error shown below just in case I turned the firewall of on all vm's but still got this error. I'm also enable to start and shutdown the remote Vm's with the same loop. Here is the error:

Get-WmiObject : The RPC server is unavailable.
At C:\work\unins1.ps1:15 char:8
+ $app = Get-WmiObject -Query "SELECT * FROM Win32_Product WHERE Name L ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

You cannot call a method on a null-valued expression.
At C:\work\unins1.ps1:18 char:5
+ $app.Uninstall()
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

Do I need to do something to make it prompt me for the vm credentials? All the services are running on the vm's

Since you've disabled the firewall, I'd guess that the name of the VM you're passing to Get-WmiObject isn't resolvable by DNS, which in turn causes the RPC Server unavailable error. An easy way to check this is to manually input the DNS name of the VM into your Get-WmiObject line and check if it works. See here for more info: Get-WmiObject: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM