VMware Cloud Community
AGFlora
Enthusiast
Enthusiast
Jump to solution

CDrom info from Harware tab

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

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

Reply
0 Kudos
8 Replies
ctrople
Enthusiast
Enthusiast
Jump to solution

Try Get-VM | Get-CDDrive | Get-Member. That will show you the members that aren't displayed by default.

======================================

Chyna Trople, VCP

Monitor. Correlate. Act. | vWire.com

======================================

Chyna Trople, VCP Monitor. Correlate. Act. | vWire.com
harkamal
Expert
Expert
Jump to solution

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 
}

AGFlora
Enthusiast
Enthusiast
Jump to solution

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

}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
ctrople
Enthusiast
Enthusiast
Jump to solution

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

======================================

Chyna Trople, VCP Monitor. Correlate. Act. | vWire.com
Reply
0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

I forgot $Report += $out....DOH!

I want to be like you Luc when I grow up :)-

As always thanks for your help!

Reply
0 Kudos