Using PowerCLI to find all the VM’s with ISO’s attached


#Create an instance of the Excel.Application
$xl = New-Object -comobject excel.application
$xl.Visible = $true
$xl.DisplayAlerts = $False
$wb = $xl.Workbooks.Add()
$ws = $wb.Worksheets.Item(1)
$ws1 = $wb.worksheets | where {$_.name -eq "Sheet1"} #<------- Selects sheet 1

$ws1.Cells.Item(1,1) = “VM”
$ws1.Cells.Item(1,2) = “CD Drive ISOPath”
$row = 2

$vms = get-vm | select Name | sort
foreach ($i in $vms) {
$Item = [string]$i
$g = $Item.Split(“=”)
$Name = $g[1].Trimend(“}”)
$CDConnected = Get-CDDrive $Name | where { ($_.ConnectionState.Connected -eq “true”) -and ($_.ISOPath -like “*.ISO*”)}
If ($CDConnected -ne $null) {
$ws1.Cells.Item($row,1) = $Name
$ws1.Cells.Item($row,2) = $CDConnected.IsoPath
$row++
[void]$ws1.cells.entireColumn.Autofit()
}
}

[\sourcecode]

Leave a comment