VMware Cloud Community
Alvm427
Contributor
Contributor
Jump to solution

Script to uninstall VMware Tools

I created this script to uninstall VMware tools but cannot get it to work. I'm new to PowerShell.  I have to uninstall the tools and later reinstall them also using a script. Here is my script. Thanks.

 

Get-Module -ListAvailable PowerCLI* | Import-Module


Connect-VIServer -Server serverone -User administrator@vsphere.local -Password mypasword

$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 -ComputerName "$vmName" WHERE Name LIKE '%VMware%'"
          
    $app.Uninstall()
	}
	

 

Labels (1)
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I suspect there is an issue with the quotes and the placement of the parameters.
Did you already try like this

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

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

15 Replies
LucD
Leadership
Leadership
Jump to solution

Are you sure the VM's name corresponds with the FQDN in the Guest OS?
Are you getting any error messages?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Alvm427
Contributor
Contributor
Jump to solution

Thanks for your response this are the errors I get. The names for the computers are Win7_2 and Win7_1 and so on. The script is getting
the name for all machines but I think is not passing it the right way to Wmi.

Get-WmiObject : A positional parameter cannot be found that accepts argument 'Win7_1 WHERE Name LIKE '%VMware%''.
At C:\work\unins1.ps1:12 char:8
+ $app = Get-WmiObject -Query "SELECT * FROM Win32_Product -ComputerNam ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-WmiObject], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetWmiObjectCommand

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

Get-WmiObject : A positional parameter cannot be found that accepts argument 'Win7_2 WHERE Name LIKE '%VMware%''.
At C:\work\unins1.ps1:12 char:8
+ $app = Get-WmiObject -Query "SELECT * FROM Win32_Product -ComputerNam ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-WmiObject], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetWmiObjectCommand

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I suspect there is an issue with the quotes and the placement of the parameters.
Did you already try like this

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

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Alvm427
Contributor
Contributor
Jump to solution

Thanks, I will change it.  

Reply
0 Kudos
Alvm427
Contributor
Contributor
Jump to solution

Ok, I changed the script and followed your suggestion now it looks like this:

 

 

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()
	}

 

 

Now I get this error just in case I turned the firewall of on all vm's but still got this 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

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

This is most probably due to a (missing) FW rule in the Guest OS.
See for example powershell - Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) - S...


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

jburen
Expert
Expert
Jump to solution

I would try a manual approach first. So start with 

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

And see if that gives you the expected output. If it does you can try to do the uninstall with $app.Uninstall().

On the other hand, this is not specific to VMware Tools but more a general Windows question. So you can look at more general approaches: https://gallery.technet.microsoft.com/scriptcenter/Uninstall-Any-Software-2c1eb344.

 

Consider giving Kudos if you think my response helped you in any way.
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Isn't that exactly what the user's script is doing 🙄


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jburen
Expert
Expert
Jump to solution

My point was to start with a small portion of the script and see if that works... Then add all the other stuff for selecting the VMs. So yes, in the end, it is what the user's script is doing but for some reason, it doesn't.

Consider giving Kudos if you think my response helped you in any way.
Reply
0 Kudos
Alvm427
Contributor
Contributor
Jump to solution

Thanks I thought about that I disabled the firewall on all the machines and still get the same errors. I've been able to restart the Vm's, start them and get info from them.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Did you enable remote management on those machines?
Did you run winrm quickconfig  on those machines?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Alvm427
Contributor
Contributor
Jump to solution

Yes, I did and I can run the command from within the machine itself.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Including the ComputerName parameter?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Alvm427
Contributor
Contributor
Jump to solution

Is been a while the problem was  a third party firewall application blocking winrm ports. Thanks

Reply
0 Kudos
system_noida
Enthusiast
Enthusiast
Jump to solution

Hi All,

I have migrated some of the vmware VMs from vmware to Hyper V. after migration we are unable to uninstall the vmware tool from the VMs. while manually  uninstallation the pop up appears and then close and nothing happens . I tried to use below code using the script but its also not working.

 

$ServerName = Get-Content ".\servers.txt"

 

$Tool = Get-Content ".\Application.txt"

 

$PSCredential = Get-Credential

 

 

@(
foreach ($Server in $ServerName) {

 

$app = Get-WmiObject -Query "SELECT * FROM Win32_Product WHERE Name LIKE '%VMware Tools%'" -ComputerName $server -Credential $PSCredential

 

$app.Uninstall()

 

} )

Reply
0 Kudos