I am trying to find a way to get information on what VMs have ISOs mounted, but given I have 4000 VMs, if I use the generic
Get-VM | Get-CDDrive | where { $_.ISOPath -like "*.iso*" } | Select-Object Parent,IsoPath
It takes forevery, and kicks the snott out of my vCenters.
I have spent a few days rolling through the idea of Get-View, but lack the syntax know-how. Any suggestions?
Due to the foreach statement, you have to use this little trick to capture the output in an array.
This updated version only reports mounted ISO files
$report = &{foreach($vm in (Get-View -ViewType VirtualMachine -Property Name,Config.Hardware.Device)){ $cds = @($vm.Config.Hardware.Device | where {$_ -is [VMware.Vim.VirtualCdrom]}) if($cds){ foreach($cd in $cds){ if($cd.Backing -is [VMware.Vim.VirtualCdromIsoBackingInfo]){ New-Object PSObject -Property @{ VM = $vm.Name
CD = $cd.DeviceInfo.Label
Info = $_.FileName
} } } } } } $report | ConvertTo-Html –Body "<div style='color:#089199'><H3>ISOs</H3></div>" | Set-Content C:\Temp\Output.htm
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try something like this
foreach($vm in (Get-View -ViewType VirtualMachine -Property Name,Config.Hardware.Device)){ $cds = @($vm.Config.Hardware.Device | where {$_ -is [VMware.Vim.VirtualCdrom]}) if($cds){ foreach($cd in $cds){ switch($cd.Backing){ {$_ -is [VMware.Vim.VirtualCdromIsoBackingInfo]}{ $Type = "Datastore ISO File"
$Info = $_.FileName
} {$_ -is [VMware.Vim.VirtualCdromAtapiBackingInfo]}{ $Type = "Host Device"
$Info = $_.DeviceName
} {$_ -is [VMware.Vim.VirtualCdromRemoteAtapiBackingInfo]}{ $Type = "Client Device"
$Info = $null
} } New-Object PSObject -Property @{ VM = $vm.Name
CD = $cd.DeviceInfo.Label
Type = $Type
Info = $Info
} } } }
It returns all connected CD/DVD, but it should be rather straightforward to use a where-clause to filter out what you want.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Fantastic work- the problem with this is that it obtains all VMs with a CDROM present, not just an ISO attached. So, the report sends me a result of all 4000 VMs.
Here is a result:
Also, I am having trouble formatting output to ConvertTo-HTML. I tried putting the results into an array, but no luck:
$ISOs = @()
foreach($vm in (Get-View -ViewType VirtualMachine -SearchRoot $Script:vmwSearchRoot -Property Name,Config.Hardware.Device)){
$cds = @($vm.Config.Hardware.Device | where {$_ -is [VMware.Vim.VirtualCdrom]})
if($cds){
foreach($cd in $cds){
switch($cd.Backing){
{$_ -is [VMware.Vim.VirtualCdromIsoBackingInfo]}{
$Type = "Datastore ISO File"
$Info = $_.FileName
}
{$_ -is [VMware.Vim.VirtualCdromAtapiBackingInfo]}{
$Type = "Host Device"
$Info = $_.DeviceName
}
{$_ -is [VMware.Vim.VirtualCdromRemoteAtapiBackingInfo]}{
$Type = "Client Device"
$Info = $null
}
}
New-Object PSObject -Property @{
VM = $vm.Name
CD = $cd.DeviceInfo.Label
Type = $Type
Info = $Info
}
}
}
}
$ISOs | ConvertTo-Html –Body "<div style='color:#089199'><H3>ISOs</H3></div>" | Set-Content C:\Temp\Output.htm
Due to the foreach statement, you have to use this little trick to capture the output in an array.
This updated version only reports mounted ISO files
$report = &{foreach($vm in (Get-View -ViewType VirtualMachine -Property Name,Config.Hardware.Device)){ $cds = @($vm.Config.Hardware.Device | where {$_ -is [VMware.Vim.VirtualCdrom]}) if($cds){ foreach($cd in $cds){ if($cd.Backing -is [VMware.Vim.VirtualCdromIsoBackingInfo]){ New-Object PSObject -Property @{ VM = $vm.Name
CD = $cd.DeviceInfo.Label
Info = $_.FileName
} } } } } } $report | ConvertTo-Html –Body "<div style='color:#089199'><H3>ISOs</H3></div>" | Set-Content C:\Temp\Output.htm
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That worked great! Thanks!!