PowerCLI checks on VM’s

I needed to make a automated way to check vm’s for mounted ISO’s, existing snapshots and running vmware tools for a cross vcenter migration. so i started to look for the neccesary one liners that got me the info i needed to make a complete check around all the info and report back in the console if there were any errors which you can see in red or if everything is “green”. My script needs a single file with txt extension without headers and a single colum with vmnames.

param ($vcenter, $batchtextfile)

Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $true -Confirm:$false | Out-Null

if ($vcenter -eq $null) {
    [void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
    $vcenter = [Microsoft.VisualBasic.Interaction]::InputBox('Enter vCenter FQDN...', 'vCenter FQDN')
    #Connect to vCenter
    Try {
        #$Credential = Get-Credential -UserName "username" -Message "Provide password."
        Connect-VIServer $vcenter -ErrorAction Stop | Out-Null
        Write-Host Connected to $vcenter -ForegroundColor Green
    }
    Catch {
        $Error[0]
        Write-Host Unable to connect to $vcenter -ForegroundColor Red
        Exit
    }
} else {
    Try {
        #$Credential = Get-Credential -UserName "username" -Message "Provide password."
        Connect-VIServer $vcenter -ErrorAction Stop | Out-Null
        Write-Host Connected to $vcenter -ForegroundColor Green
    }
    Catch {
        $Error[0]
        Write-Host Unable to connect to $vcenter -ForegroundColor Red
        Exit
    }
}

if ($batchtextfile -eq $null) {
    Write-Host Please select your input textfile -ForegroundColor Green
    Add-Type -AssemblyName System.Windows.Forms
    $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{ 
        InitialDirectory = [Environment]::GetFolderPath('MyDocuments')
        Filter           = 'TXT (*.txt) | *.txt'
    }
    $FileBrowser.ShowDialog()
    $textfile = $FileBrowser.FileName    
} else {
    Write-Host Textfile : $batchtextfile -ForegroundColor Green
    $textfile = $batchtextfile
}
$vmlist = Get-Content $textfile

foreach ($selectedvm in $vmlist) {
    Write-Host "Checking for Mounted ISO's on $selectedvm" -ForegroundColor Green
    try {
        if (((Get-VM $selectedvm | Get-CDDrive).IsoPath).Equals("[]")) {
            Write-Host No Mounts found! -ForegroundColor Green
        }
        else {
            Write-Host Mounts found! -ForegroundColor Red
        }
    }
    catch {
        Write-Host No Mounts found! -ForegroundColor Green
    }

    Write-Host "Checking for Snapshots on $selectedvm" -ForegroundColor Green
    if (Get-VM $selectedvm | Get-Snapshot) {
        write-host Snaps found! -ForegroundColor Red
    }
    else {
        Write-Host No Snaps found -ForegroundColor Green
    }

    Write-Host "Checking for Status of VMware Tools on $selectedvm" -ForegroundColor Green
    $vminfo = Get-VM $selectedvm
    if ($vminfo.ExtensionData.Guest.ToolsRunningStatus -eq "guestToolsRunning") {
        Write-Host Tools Running! -ForegroundColor Green
    }
    else {
        Write-Host Tools not Running! -ForegroundColor Red
    }
}

Disconnect-VIServer * -Confirm:$false

Leave a Reply