简体   繁体   English

如何使用 PowerCLI 和 PowerShell 远程卸载应用程序

[英]How to remote uninstall applications with PowerCLI and PowerShell

I created this script to remotely uninstall VMware tools from a group of Vm's.我创建了这个脚本来从一组 Vm 中远程卸载 VMware 工具。

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?我需要做些什么来让它提示我输入 vm 凭据吗? 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.由于您已禁用防火墙,我猜您传递给 Get-WmiObject 的 VM 名称无法由 DNS 解析,这反过来又会导致 RPC 服务器不可用错误。 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.一个简单的检查方法是将 VM 的 DNS 名称手动输入到 Get-WmiObject 行并检查它是否有效。 See here for more info: Get-WmiObject: The RPC server is unavailable.有关详细信息,请参阅此处: Get-WmiObject:RPC 服务器不可用。 (Exception from HRESULT: 0x800706BA) (来自 HRESULT 的异常:0x800706BA)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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