Alvm427
Contributor
Contributor

Script to uninstall VMware Tools

Jump to solution

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)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

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

14 Replies
LucD
Leadership
Leadership

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

0 Kudos
Alvm427
Contributor
Contributor

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

0 Kudos
LucD
Leadership
Leadership

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

Alvm427
Contributor
Contributor

Thanks, I will change it.  

0 Kudos
Alvm427
Contributor
Contributor

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

 

0 Kudos
LucD
Leadership
Leadership

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

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.
0 Kudos
LucD
Leadership
Leadership

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


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

0 Kudos
jburen
Expert
Expert

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.
0 Kudos
Alvm427
Contributor
Contributor

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.

0 Kudos
LucD
Leadership
Leadership

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

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

0 Kudos
LucD
Leadership
Leadership

Including the ComputerName parameter?


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

0 Kudos
Alvm427
Contributor
Contributor

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

0 Kudos