powershellvmware-tools

Powershell nested if / else command is not recognized as the name of a cmdlet


I am attempting to write a powershell script that removes VMware Tools off of physical workstations in my environment (don't ask) while ignoring VMs and I am having issues with the nested if / else statements in the "#Execute VMware Tools removal if physical, then write log" section of this code. Can anyone with more Powershell experience give me some pointers as to what I may have done wrong?

I receive the following error:

else : The term 'else' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

I apologize for the amateur formatting, I am still learning Powershell.

Thank you for any assistance.

#Create log path
$path = "D:\log\folder\location\"
If(!(test-path $path))
{
New-Item -ItemType Directory -Force -Path $path
}
#Gather required host info
$ComputerSystemInfo = Get-WmiObject -Class Win32_ComputerSystem -
ComputerName $env:COMPUTERNAME -ErrorAction Stop
switch ($ComputerSystemInfo.Model) {
# Check for VMware Machine Type
   "VMware Virtual Platform" {
    $MachineType = "VM"
    }
# Otherwise it is a physical Box
    default {
    $MachineType = "Physical"
    }
    }
#Execute VMware Tools removal if physical, then write log
if($MachineType -eq "Physical") {
   $regpath = "HKLM:\Software\Microsoft\Windows\CurrentVersion\uninstall"
   Get-childItem $regpath | % {$keypath = $_.pschildname 
   $key = Get-Itemproperty $regpath\$keypath}
  if($key.DisplayName -match "VMware Tools") 
   {$VMwareToolsGUID = $keypath} MsiExec.exe /x $VMwareToolsGUID /qn /norestart 
   {Write-output "VMware Tools Uninstalled" | Out-File -Encoding ascii -filepath "D:\log\folder\location\VMware_Uninstalled.txt"}
  else
   {Write-output "VMware Tools Not Present" | Out-File -Encoding ascii -filepath "D:\log\folder\location\VMware_Not_Present.txt"}
   } 
#Write output log if VM
if($MachineType -eq "VM")
 {Write-Output "Machine is virtual" | Out-File -Encoding ascii -filePath 
 "D:\log\folder\location\Virtual_Machine.txt"}
else
 {Write-Output "Error" | Out-File -Encoding ascii -FilePath 
 "D:\log\folder\location\Error.txt"}

Solution

  • I went ahead and made some edits for you so it looks cleaner and fixed your brackets (which was causing the error you are getting). Feel free to ask any questions! As a future reference, the easiest thing to do when you need to check if there is anything wrong with your script you can copy and paste it into PowerShell ISE and it will underline any errors it sees in red.

    #Create log path
    $path = "D:\log\folder\location"
    
    If(!(test-path $path)){
        New-Item -ItemType Directory -Force -Path $path
    }
    
    #Gather required host info
    $ComputerSystemInfo = Get-WmiObject -Class Win32_ComputerSystem
    $Computer = $env:COMPUTERNAME
    
    switch ($ComputerSystemInfo.Model) {
        # Check for VMware Machine Type
        "VMware Virtual Platform" {
        $Global:MachineType = "VM"
        }
        # Otherwise it is a physical Box
        default {
        $Global:MachineType = "Physical"
        }
    }
    
    #Execute VMware Tools removal if physical, then write log
    if($MachineType -eq "Physical") {
        $regpath = "HKLM:\Software\Microsoft\Windows\CurrentVersion\uninstall"
        Get-childItem $regpath | % {$Global:keypath = $_.pschildname 
        $Global:key = Get-Itemproperty $regpath\$keypath}
    }
    
    if($key.DisplayName -match "VMware Tools"){
        $VMwareToolsGUID = $keypath 
        MsiExec.exe /x $VMwareToolsGUID /qn /norestart -wait
        Write-output "VMware Tools Uninstalled" | Out-File -Encoding ascii -filepath 
        "D:\log\folder\location\VMware_Uninstalled.txt"
    }else{
        Write-output "VMware Tools Not Present" | Out-File -Encoding ascii -filepath 
        "D:\log\folder\location\VMware_Not_Present.txt"
    }
    
    #Write output log if VM
    if($MachineType -eq "VM"){
        Write-Output "Machine is virtual" | Out-File -Encoding ascii -filePath 
        "D:\log\folder\location\Virtual_Machine.txt"
    }else{
        Write-Output "Error" | Out-File -Encoding ascii -FilePath 
        "D:\log\folder\location\Error.txt"
    }