Script: How to check VMs with ISO or CD-Drive Connected Using PowerShell and PowerCLI – VirtuallyThatGuy

This is a quick blog post on how to report on VMs connected with ISO or CD Drive. Refer to my previous post on how remove ISO or cd-drive from VMs in your vmware estate.

## VMware Find VMs with ISO Connected 

param
(
   [Parameter(Mandatory=$true)]
   [VMware.VimAutomation.ViCore.Types.V1.Inventory.Cluster]
   $vParam
);

[Array] $vmList = @( Get-VM -Location $vParam | Sort Name );

foreach ( $vmItem in $vmList )
{
    [Array] $vmCdDriveList = @( Get-CDDrive -VM $vmItem );

    foreach ( $vmCdDriveItem in $vmCdDriveList )
    {
        [String] $insertedElement = "";
        [String] $connectionType  = "";

        switch ( $vmCdDriveItem )
        {
            { $_.IsoPath      } { $insertedElement = $_.IsoPath;      $connectionType = "ISO";           break; }
            { $_.HostDevice   } { $insertedElement = $_.HostDevice;   $connectionType = "Host Device";   break; }
            { $_.RemoteDevice } { $insertedElement = $_.RemoteDevice; $connectionType = "Remote Device"; break; }
            default             { $insertedElement = "None";          $connectionType = "Client Device"; break; }
        }

        $output = New-Object -TypeName PSObject;

        $output | Add-Member -MemberType NoteProperty -Name "VM"                -Value $vmItem
        $output | Add-Member -MemberType NoteProperty -Name "CD-Drive"          -Value $vmCdDriveItem.Name;
        $output | Add-Member -MemberType NoteProperty -Name "Connection"        -Value $connectionType;
        $output | Add-Member -MemberType NoteProperty -Name "Inserted"          -Value $insertedElement;
        $output | Add-Member -MemberType NoteProperty -Name "Connected"         -Value $vmCdDriveItem.ConnectionState.Connected;
        $output | Add-Member -MemberType NoteProperty -Name "StartConnected"    -Value $vmCdDriveItem.ConnectionState.StartConnected;
        $output | Add-Member -MemberType NoteProperty -Name "AllowGuestControl" -Value $vmCdDriveItem.ConnectionState.AllowGuestControl;
        $output;
    }
}

You May Also Like

About the Author: VirtuallyThatGuy

Leave a Reply

Your email address will not be published. Required fields are marked *