PowerCLI: Find VMs with Any Independent Disks

I recently had a request come through to see if there was a way to quickly find any/all virtual machines with Independent Disks attached. In this particular scenario, I’ll be looking for these types of VMs within a specific datacenter object. However, this script could easily be changed to scan an entire vCenter object, if needed. This script will look for VMs with both independent persistent as well as independent non-persistent disks.

Like some of my other scripts, this one also utilizes the try/catch when first connecting to a vCenter Server. For instance, if you attempt to connect to a vCenter and enter the wrong credentials or wrong server name/IP, it will stop the script and state that it “Could not connect to the vCenter Server <name>. In addition, I also have some logic built in to track the progress of the scan, as well as the option to export the results to CSV if preferred.

Here’s the script as it’s currently written, but be sure to check out my GitHub page for the latest revisions and edits:

########################################
# Script: find-vms-with-any-independent-disks-in-a-datacenter.ps1
# Author: Doug DeFrank
# Date: 2018-10-17
#
# Purpose: Scan a datacenter for VMs with any (persistent or non-persistent) independent disks.
########################################

Write-Host -ForegroundColor Yellow `n "This script will scan the a datacenter object for VMs any (persistent or non-persistent) independent disks." `n

### Define the date in the yyyyMMdd format
$date = Get-Date -format "yyyyMMdd"

### Prompt user for vCenter Server name, and connect to it
$vCenterServer = Read-Host -Prompt 'Enter the FQDN of the vCenter Server you want to connect to (ex. vcenter.domain.com)'

### This Try/Catch statement will stop the script if a vCenter Server doesn't exist, a bad username/password is entered, etc.
Try {
    Connect-VIServer -Server $vCenterServer -ErrorAction Stop | Out-Null
}

Catch {
    Write-Host -ForegroundColor Red -BackgroundColor Black "Could not connect to the vCenter Server [$vCenterServer]." `n
    Exit
}

### Get all VMs in the chosen datacenter
$DatacenterName = Get-Datacenter | Out-GridView -passthru -Title "Select a datacenter"

### Get all Vms in the chosen datacenter
$vms = $DatacenterName | Get-VM | Sort-Object

### Set the loop variable to 1
$loop = 1

$report = foreach ($vm in $vms) {
    ### Display a progress bar during VM checks
    Write-Progress -Activity "Scanning for VMs with Independent Disks" -Status "Checking $vm" -PercentComplete ($loop/$vms.count * 100)

    ### If the VM boot firmware is set to BIOS, add it to the report
    $vm | Get-HardDisk | Where-Object {$_.Persistence -like "Independent*"}

    $loop++
}

### Check to see if the report is empty
if (!$report) {
    Write-Host -ForegroundColor Red `n "Report is empty. No VMs with independent disks were found."
}

else {

    ### If the report is not empty, ask the user if they want to export the results to a CSV file or not
    Do {
        Write-Host `n "Do you want to export the results to a CSV file?"
        Write-Host "1.) Yes"
        Write-Host "2.) No"
        $csvexportyn = Read-Host "Please enter your selection"
        Switch ($csvexportyn) {

            ### If user chooses 1.) Yes, export to a CSV file in the same location as the script itself
            1 {
                Write-Host `n "Generating CSV > .\$DatacenterName-VMs-with-Independent-Disks-$date.csv"
                $report | Export-CSV -path ".\$DatacenterName-VMs-with-Independent-Disks-$date.csv" -NoTypeInformation
                $yn = $true
            }

            ### If user chooses 2.) No, display a separate window with the results, and exit the script
            2 {
                $report | Out-GridView
                $yn = $true
            }

            ### Validate the input. If it's not a 1 or a 2, repeat the question
            default {
                Write-Host -ForegroundColor Red ">>> Invalid input. Please enter a [1] for Yes or a [2] for No."
                $yn = $false
            }
        }
    }

    ### Loop through the "Export CSV" question until a valid choice is made
    Until ($yn)

}

### Disconnect from the vCenter Server
Write-Host -ForegroundColor Green `n "Disconnecting from the vCenter Server, $vCenterServer." `n
Disconnect-VIServer -Server $vCenterServer -confirm:$false | Out-Null

I hope you found this script helpful, and as always, thank you for stopping by!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s