PowerCLI

 View Only
Expand all | Collapse all

Script to uninstall VMware Tools

  • 1.  Script to uninstall VMware Tools

    Posted Jan 27, 2021 12:23 AM

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

     



  • 2.  RE: Script to uninstall VMware Tools

    Posted Jan 27, 2021 08:39 AM

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



  • 3.  RE: Script to uninstall VMware Tools

    Posted Jan 27, 2021 01:24 PM

    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



  • 4.  RE: Script to uninstall VMware Tools
    Best Answer

    Posted Jan 27, 2021 01:31 PM

    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
    

     



  • 5.  RE: Script to uninstall VMware Tools

    Posted Jan 27, 2021 02:02 PM

    Thanks, I will change it.  



  • 6.  RE: Script to uninstall VMware Tools

    Posted Jan 27, 2021 11:03 PM

    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

     



  • 7.  RE: Script to uninstall VMware Tools

    Posted Jan 28, 2021 08:17 AM

    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) - Stack Overflow



  • 8.  RE: Script to uninstall VMware Tools

    Posted Jan 28, 2021 12:36 PM

    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.



  • 9.  RE: Script to uninstall VMware Tools

    Posted Jan 28, 2021 12:59 PM

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



  • 10.  RE: Script to uninstall VMware Tools

    Posted Jan 28, 2021 06:40 PM

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



  • 11.  RE: Script to uninstall VMware Tools

    Posted Jan 28, 2021 06:49 PM

    Including the ComputerName parameter?



  • 12.  RE: Script to uninstall VMware Tools

    Posted Mar 02, 2021 12:46 AM

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



  • 13.  RE: Script to uninstall VMware Tools

    Posted Jan 28, 2021 08:42 AM

    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.

     



  • 14.  RE: Script to uninstall VMware Tools

    Posted Jan 28, 2021 08:45 AM

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



  • 15.  RE: Script to uninstall VMware Tools

    Posted Jan 28, 2021 08:55 AM

    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.



  • 16.  RE: Script to uninstall VMware Tools

    Posted Jan 31, 2024 10:29 AM

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

     

    } )