VMware Cloud Community
Troy_Clavell
Immortal
Immortal
Jump to solution

Get-CDDrive Help

Hello,

I have the below simple code, however it's not producing what I would need to it produce.  Any help would be appreciated.  We have a handful of guests that don't have a CD/DVD Drive configured (meaning it's missing).  What I would like to find is guests that don't have a CD/DVD drive, take that output and export it to csv.

Here is the simple code I have.  It's get's only VM's that have a CD/DVD but skips the ones that don't

Get-VM | get-CDDrive |Export-Csv c:\scripts\cd.csv -NoTypeInformation

ESXi 4.1U2

vCenter 4.1 U2

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Sorry for being late Smiley Wink

To export only the VMs that do not have a CD/DVD, you could do

Get-VM | where {!(Get-CDDrive -VM $_)} | Select Name | Export-Csv c:\scripts\cd.csv -NoTypeInformation


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
7 Replies
a_p_
Leadership
Leadership
Jump to solution

Since Luc did not respond yet, you may try something like:

get-vm | format-table -wrap -autosize -property Name,CDDrives

found at http://waynes-world-it.blogspot.com/2009/08/vmware-vsphere-powercli-commands.html

André

LucD
Leadership
Leadership
Jump to solution

Sorry for being late Smiley Wink

To export only the VMs that do not have a CD/DVD, you could do

Get-VM | where {!(Get-CDDrive -VM $_)} | Select Name | Export-Csv c:\scripts\cd.csv -NoTypeInformation


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Troy_Clavell
Immortal
Immortal
Jump to solution

That works, but can you explain your logic for us non PowerCLI people

What does the ! do/mean in the code that you are using?

{!(Get-CDDrive -VM $_)}
0 Kudos
a_p_
Leadership
Leadership
Jump to solution

If I understand this correctly,

where {!(Get-CDDrive -VM $_)}

filters the result of Get-VM by running "Get-CDDrive" for each VM "-VM $_" and only returning those which don't "!" return a result.

André

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is correct, the '!' stands for NOT.

When the Get-CdDrive returns nothing, no CD/DVD drives, it will result in $null (which is converted to $False) being the result of the condition.

But since we want the inverse, we negate the result and '! $False' becomes $True.

So only the VM objects where there is no CD/DVD attached will pass through the Where-clause.

And the $_ represents the VM object that was passed through the pipeline to the Where-clause.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Troy_Clavell
Immortal
Immortal
Jump to solution

It does make sense.  Thank you again!  I guess my tab is already adding up for vBeers that I owe you at VMworld 2012Smiley Wink

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'll book a couple of extra days in SF Smiley Wink


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos