Hi
I need the following CDROM info listed in the hardware tab from the VIC.
Device Status:
Datastore ISO file:
This is what I have so far:
SVM = Get-VM | Sort Name
$VM | Where { $_ | Get-CDDrive | Where { $_.ConnectionState.Connected -eq "true" } } | Select Name
Thanks
Capture the output, line by line, in an array.
Then pipe that array to the Export-Csv cmdlet.
$report = @() Get-VM | Sort | % { $vm = $_ $out = "" | Select VmName, CdRomStatus, IsoFile $out.VmName = $vm.Name $vm.CDDrives | where {$_.ConnectionState.Connected -or $_.IsoPath -ne $null} | % { $out.CdRomStatus = $_.ConnectionState.Connected $out.IsoFile = $_.IsoPath $report += $out } } $report | Export-Csv "C:\test.csv" -noTypeInformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Here you go mate...
Get-VM | Sort | % { $vm = $_ $out = "" | Select VmName, CdRomStatus, IsoFile $out.VmName = $vm.Name $vm.CDDrives | % { $out.CdRomStatus = $_.ConnectionState.Connected $out.IsoFile = $_.IsoPath } $out }
How would you modify this script to report only those vm's that have thier cdrom connected or iso file set?
I've try to modify your code but I'm not getting the desired results..
Here's what I have tried:
Get-VM | Sort | % {
$vm = $_
if( Where { $_.ConnectionState.Connected -eq "true" }){
$Report = "" | Select VmName, CdRomStatus, IsoFile
$Report.VmName = $vm.Name
$vm.CDDrives | % {
$Report.CdRomStatus = $_.ConnectionState.Connected
$Report.IsoFile = $_.IsoPath
}
}
$Report
}
You can use the Where-Object cmdlet, which allows you to filter on specific properties of the objects on the pipeline.
Btw, I moved the line that outputs the $out variable inside the loop, otherwise you wouldn't see all the CDROM drives for a guest that has more than one.
Get-VM | Sort | % { $vm = $_ $out = "" | Select VmName, CdRomStatus, IsoFile $out.VmName = $vm.Name $vm.CDDrives | where {$_.ConnectionState.Connected -or $_.IsoPath -ne $null} | % { $out.CdRomStatus = $_.ConnectionState.Connected $out.IsoFile = $_.IsoPath $out } }
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I think your existing code will work if you change the Where to:
Where { $_.ConnectionState.Connected -eq "true" -or $_.IsoPath -ne $null}
I suspect you want the connected -eq "true" -and $IsoPath -ne "" though if you are trying to ensure your VMs can VMotion.
======================================
Chyna Trople, VCP
Monitor. Correlate. Act. | vWire.com
Your code gives me the deired results but the output isn't redirected to my csv file?
Here's what I've tried:
##Variables
#$Report = @()
$csvfile = "D:\CDROM_Report.csv"
#$VM = Get-VM | Sort Name
#######################################################################################################################
Get-VM | Sort | % {
$vm = $_
$Report = "" | Select VmName, CdRomStatus, IsoFile
$Report.VmName = $vm.Name
$vm.CDDrives | where {$_.ConnectionState.Connected -or $_.IsoPath -ne $null} | % {
$Report.CdRomStatus = $_.ConnectionState.Connected
$Report.IsoFile = $_.IsoPath
$Report
$Report | Export-Csv $csvfile -NoTypeInformation
}
}
#$Report | Export-Csv $csvfile -NoTypeInformation
Capture the output, line by line, in an array.
Then pipe that array to the Export-Csv cmdlet.
$report = @() Get-VM | Sort | % { $vm = $_ $out = "" | Select VmName, CdRomStatus, IsoFile $out.VmName = $vm.Name $vm.CDDrives | where {$_.ConnectionState.Connected -or $_.IsoPath -ne $null} | % { $out.CdRomStatus = $_.ConnectionState.Connected $out.IsoFile = $_.IsoPath $report += $out } } $report | Export-Csv "C:\test.csv" -noTypeInformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I forgot $Report += $out....DOH!
I want to be like you Luc when I grow up :)-
As always thanks for your help!