russjar
Enthusiast
Enthusiast

$vm.extensionData and Get commands combined in the same script.

Jump to solution

Hi all


I really could do with some help. I've got the below script which I found and modified to suit what I'm trying to achieve but it isn't outputting the info in an expected way. I'm trying to put generic VM info, plus VM info exposed through extensiondata, then get that eventually into a CSV but I've really hit a road block. My sample code is below but I'm sure there has to be a better way, especially where there are multiple objects returned like harddisks, datastores etc the formatting is broken and I'm at my knowledge limit. Thank you in advance.


$result = @()
    $VMS = Get-VM "vmname"
    ForEach($VM in $VMS){
   
    $VMVirtualDisk = Get-VM $VM | Get-HardDisk
    $NetworkAdapters = Get-NetworkAdapter -vm $vm
    $SCSIControllers = Get-ScsiController -vm $vm
    $DataStores = get-vm $vm | get-datastore

    $obj = new-object psobject
    $obj | Add-Member -MemberType NoteProperty -Name VM_Name -Value $vm.ExtensionData.Name
    $obj | Add-Member -MemberType NoteProperty -Name CPUSocket -Value $vm.ExtensionData.config.hardware.NumCPU
    $obj | Add-Member -MemberType NoteProperty -Name Corepersocket -Value $vm.ExtensionData.config.hardware.NumCoresPerSocket
    $obj | Add-Member -MemberType NoteProperty -Name MemoryMB $vm.ExtensionData.config.hardware.MemoryMB
    $obj | Add-member -MemberType Noteproperty -Name Firmware -value $vm.ExtensionData.Config.Firmware
    $obj | Add-Member -MemberType Noteproperty -Name NetworkAdapterType -value $NetworkAdapters.Type
    $obj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $vm.ExtensionData.summary.guest.IpAddress
    $obj | Add-Member -MemberType Noteproperty -Name NetworkLabel -value $NetworkAdapters.NetworkName
    $obj | Add-Member -MemberType Noteproperty -Name DataStore -value $DataStores.Name
    $obj | Add-member -MemberType Noteproperty -Name ScsiAdapterType -value $SCSIControllers.Type
    $obj | Add-Member -MemberType Noteproperty -Name VirtualDiskCapacity -value $VMVirtualDisk_Capacity
   
    $result += $obj

}

$result | Format-Table -Autosize

VCP,MCSE NT4/W2k/W2k3, MCSA W2k3
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

Indeed, that explains why those properties are not working.

That notation, i.e. $NetworkAdapters.Type is not known in PS v2.

Try like this

$result = @()

$VMS = Get-VM "vmname"

ForEach($VM in $VMS){

    $VMVirtualDisk = Get-VM $VM | Get-HardDisk

    $NetworkAdapters = Get-NetworkAdapter -vm $vm

    $SCSIControllers = Get-ScsiController -vm $vm

    $DataStores = get-vm $vm | get-datastore

    $obj = new-object psobject

    $obj | Add-Member -MemberType NoteProperty -Name VM_Name -Value $vm.ExtensionData.Name

    $obj | Add-Member -MemberType NoteProperty -Name CPUSocket -Value $vm.ExtensionData.config.hardware.NumCPU

    $obj | Add-Member -MemberType NoteProperty -Name Corepersocket -Value $vm.ExtensionData.config.hardware.NumCoresPerSocket

    $obj | Add-Member -MemberType NoteProperty -Name MemoryMB $vm.ExtensionData.config.hardware.MemoryMB

    $obj | Add-member -MemberType Noteproperty -Name Firmware -value $vm.ExtensionData.Config.Firmware

    $obj | Add-Member -MemberType Noteproperty -Name NetworkAdapterType -value (($NetworkAdapters | %{$_.Type}) -join '|')

    $obj | Add-Member -MemberType NoteProperty -Name IPAddress -Value ($vm.ExtensionData.summary.guest.IpAddress -join '|')

    $obj | Add-Member -MemberType Noteproperty -Name NetworkLabel -value (($NetworkAdapters | %{$_.NetworkName}) -join '|')

    $obj | Add-Member -MemberType Noteproperty -Name DataStore -value (($DataStores | %{$_.Name}) -join '|')

    $obj | Add-member -MemberType Noteproperty -Name ScsiAdapterType -value (($SCSIControllers | %{$_.Type}) -join '|')

    $obj | Add-Member -MemberType Noteproperty -Name VirtualDiskCapacity -value (($VMVirtualDisk | %{$_.Capacity}) -join '|')

 

    $result += $obj

}

$result |

Export-Csv report.csv -NoTypeInformation -UseCulture


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

View solution in original post

0 Kudos
11 Replies
LucD
Leadership
Leadership

I corrected a couple of issues.

The VirtualDiskCapacity value had an underscore instead of a dot.

Some fields can return multiple values, that is not handled by the Export-Csv, so I used the -join operator to make it one string

Be aware that if the Get-VM on the 2nd line doesn't return VM(s), you will get an empty CSV

Try like this

$result = @()

$VMS = Get-VM "vmname"

ForEach($VM in $VMS){

    $VMVirtualDisk = Get-VM $VM | Get-HardDisk

    $NetworkAdapters = Get-NetworkAdapter -vm $vm

    $SCSIControllers = Get-ScsiController -vm $vm

    $DataStores = get-vm $vm | get-datastore

    $obj = new-object psobject

    $obj | Add-Member -MemberType NoteProperty -Name VM_Name -Value $vm.ExtensionData.Name

    $obj | Add-Member -MemberType NoteProperty -Name CPUSocket -Value $vm.ExtensionData.config.hardware.NumCPU

    $obj | Add-Member -MemberType NoteProperty -Name Corepersocket -Value $vm.ExtensionData.config.hardware.NumCoresPerSocket

    $obj | Add-Member -MemberType NoteProperty -Name MemoryMB $vm.ExtensionData.config.hardware.MemoryMB

    $obj | Add-member -MemberType Noteproperty -Name Firmware -value $vm.ExtensionData.Config.Firmware

    $obj | Add-Member -MemberType Noteproperty -Name NetworkAdapterType -value ($NetworkAdapters.Type -join '|')

    $obj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $vm.ExtensionData.summary.guest.IpAddress

    $obj | Add-Member -MemberType Noteproperty -Name NetworkLabel -value ($NetworkAdapters.NetworkName -join '|')

    $obj | Add-Member -MemberType Noteproperty -Name DataStore -value ($DataStores.Name -join '|')

    $obj | Add-member -MemberType Noteproperty -Name ScsiAdapterType -value ($SCSIControllers.Type -join '|')

    $obj | Add-Member -MemberType Noteproperty -Name VirtualDiskCapacity -value ($VMVirtualDisk.Capacity -join '|')

  

    $result += $obj

}

$result |

Export-Csv report.csv -NoTypeInformation -UseCulture


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

0 Kudos
russjar
Enthusiast
Enthusiast

Awesome thanks for you help.

Still got an issue though. the below doesn't return anything and when checking via a FL in stead of a FT the value is empty. Also when doing a GM on $VMVirtualDisk all the properties are there. I'm also wanting to add the file names too so if the below isn't doing as expected the filenames wont work either.

    $obj | Add-Member -MemberType Noteproperty -Name VirtualDiskCapacity -value ($VMVirtualDisk.CapacityKB -join '|')

    $obj | Add-Member -MemberType Noteproperty -Name VirtualDiskName -value ($VMVirtualDisk.FileName -join '|')


Thanks again this one is stretching me.

VCP,MCSE NT4/W2k/W2k3, MCSA W2k3
0 Kudos
LucD
Leadership
Leadership

Which PowerShell version are you running?

Do a $PSVersionTable


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

0 Kudos
russjar
Enthusiast
Enthusiast

I'm not at work anymore, I've knocked off for the weekend, but I can tell you it's PowerShell v2.0 if that's what you were after?

VCP,MCSE NT4/W2k/W2k3, MCSA W2k3
0 Kudos
LucD
Leadership
Leadership

Indeed, that explains why those properties are not working.

That notation, i.e. $NetworkAdapters.Type is not known in PS v2.

Try like this

$result = @()

$VMS = Get-VM "vmname"

ForEach($VM in $VMS){

    $VMVirtualDisk = Get-VM $VM | Get-HardDisk

    $NetworkAdapters = Get-NetworkAdapter -vm $vm

    $SCSIControllers = Get-ScsiController -vm $vm

    $DataStores = get-vm $vm | get-datastore

    $obj = new-object psobject

    $obj | Add-Member -MemberType NoteProperty -Name VM_Name -Value $vm.ExtensionData.Name

    $obj | Add-Member -MemberType NoteProperty -Name CPUSocket -Value $vm.ExtensionData.config.hardware.NumCPU

    $obj | Add-Member -MemberType NoteProperty -Name Corepersocket -Value $vm.ExtensionData.config.hardware.NumCoresPerSocket

    $obj | Add-Member -MemberType NoteProperty -Name MemoryMB $vm.ExtensionData.config.hardware.MemoryMB

    $obj | Add-member -MemberType Noteproperty -Name Firmware -value $vm.ExtensionData.Config.Firmware

    $obj | Add-Member -MemberType Noteproperty -Name NetworkAdapterType -value (($NetworkAdapters | %{$_.Type}) -join '|')

    $obj | Add-Member -MemberType NoteProperty -Name IPAddress -Value ($vm.ExtensionData.summary.guest.IpAddress -join '|')

    $obj | Add-Member -MemberType Noteproperty -Name NetworkLabel -value (($NetworkAdapters | %{$_.NetworkName}) -join '|')

    $obj | Add-Member -MemberType Noteproperty -Name DataStore -value (($DataStores | %{$_.Name}) -join '|')

    $obj | Add-member -MemberType Noteproperty -Name ScsiAdapterType -value (($SCSIControllers | %{$_.Type}) -join '|')

    $obj | Add-Member -MemberType Noteproperty -Name VirtualDiskCapacity -value (($VMVirtualDisk | %{$_.Capacity}) -join '|')

 

    $result += $obj

}

$result |

Export-Csv report.csv -NoTypeInformation -UseCulture


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

0 Kudos
russjar
Enthusiast
Enthusiast

Thanks LucD you are a champion.

I'll test it next week when I'm back at work.

BTW as a side note, is there a $vm.ExtensionData explorer available to view all the available properties?

VCP,MCSE NT4/W2k/W2k3, MCSA W2k3
0 Kudos
LucD
Leadership
Leadership

No, I'm afraid not.

You have the API Reference of course.

Then there is the Format-Custom cmdlet, but make sure to use the Depth parameter, some of these objects are huge and deeply nested.

A useful alternative is the Show-Object function from Lee Holmes

And some editors have this functionality built in (PowerShell Plus, ISESteroids)


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

0 Kudos
russjar
Enthusiast
Enthusiast

Cheers LucD that did the trick.

Next challenge is to separate out multiple entries onto separate lines.

VCP,MCSE NT4/W2k/W2k3, MCSA W2k3
0 Kudos
LucD
Leadership
Leadership

That would require adding additional ForEach loops


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

0 Kudos
russjar
Enthusiast
Enthusiast

Hey LucD

I'm having some trouble getting the split onto a new line working, care to throw me a hint?

Thanks LucD.

VCP,MCSE NT4/W2k/W2k3, MCSA W2k3
0 Kudos
LucD
Leadership
Leadership

Not sure I get the question I'm afraid.

What exactly do you mean?


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

0 Kudos