Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

vmware - 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 [email protected] -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:workunins1.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:workunins1.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

question from:https://stackoverflow.com/questions/65929167/how-to-remote-uninstall-applications-with-powercli-and-powershell

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...