VMWare PowerCLI to check compliance in v7



  • A little something for v7 VCenter to use the semi-new Lifecycle Manager.

    ###### Variables
    # Change this to your VSphere server name:
    $viserver = "server"
    # Change os search base, ex: windows, linux, etc.
    $os = "windows"
    # Use your favorite format or leave as is:
    $date=get-date -format g
    ###### End Variables
    
    
    try {$vmlist = Get-VM |where {$_.guestid -like "*$($os)*" -and $_.Name -notlike "*replica*"}
    }
    
    catch [VMware.VimAutomation.Sdk.Types.V1.ErrorHandling.VimException.ViServerConnectionException]{
        write-host -ForegroundColor red "Error: No VIServer connection present. Please wait..."
        Connect-VIServer -Server $viserver -verbose:$false -WarningAction 0
        $vmlist = Get-VM |where {$_.guestid -like "*$($os)*" -and $_.Name -notlike "*replica*"}
        }
    
    ###### Tools status only
    $vmout=$vmlist|select Name,
        @{N="Tools Status";E={
            if($_.Guest.Extensiondata.GuestState -eq "notRunning"){
                "Not running"}
            else{
                $_.Guest.Extensiondata.ToolsVersionStatus.Replace("guestToolsNeedUpgrade","Out of date").Replace("guestToolsNotInstalled","Not installed").Replace("guestToolsCurrent","OK").Replace("guestToolsUnmanaged","Unmanaged")
            }
        }}
        
    ###### Get the most up-to-date info by testing the environment
    $vmlist |Test-Compliance -ErrorAction SilentlyContinue
    
    ###### Display in desired format
    write-Host -ForegroundColor Cyan "View in Terminal, Gridview, Excel, Notepad, or quick tools status only?"
    do {$vmview = (read-host "T/g/e/n/q").ToLower()} while ($vmview -notin @('t','g','e','n',"$($null)","q"))
    If ($vmview -eq '') {$vmview = 't'}
    
    If ($vmview -eq 't') {$vmlist |get-compliance |sort status
    }
    
    If ($vmview -eq 'g') {$vmlist |get-compliance |out-gridview -Title "VM Compliance Status $($date)"
    }
    
    If ($vmview -eq 'e') {$vmlist |get-compliance |export-csv $env:TEMP\vmtools.csv -Force -NoTypeInformation
    & "C:\Program Files\Microsoft Office\Office16\EXCEL.EXE" $env:TEMP\vmtools.csv
    }
    
    If ($vmview -eq 'n') {$vmlist |get-compliance |export-csv $env:TEMP\vmtools.csv -Force -NoTypeInformation
    & notepad $env:TEMP\vmtools.csv
    }
    
    If ($vmview -eq 'q') {$vmout |sort "Tools Status",Name
    }
    

Log in to reply