VMware Cloud Community
brittonv
Contributor
Contributor
Jump to solution

PowerCLI script to list all hardware

Greetings,

I am looking for a script that will output all hardware settings for my vm's including floppy, USB, Serial and paralle ports, etc..

Found this script for serial ports:

$vms = Get-VM

foreach($vm in $vms){

Get-View -ViewType VirtualMachine | %{

if($_.Config.Hardware.Device | where{$_.gettype().Name -eq

"VirtualSerialPort"}){

$_.Name

}

}

}

However how do I modify it for all hardware or do I need to specify each potential device? How do I get a list of options, for example where do I find that "VirtualSerialPort" is an option?

Also how do i output it to a csv file.

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try this

Get-View -ViewType VirtualMachine | %{
	$vm = $_
	$_.Config.Hardware.Device | Select @{N="VM name";E={$vm.Name}},@{N="HW name";E={$_.GetType().Name}},@{N="Label";E={$_.DeviceInfo.Label}}
} | Export-Csv "C:\VM-HW.csv" -NoTypeInformation

____________

Blog: LucD notes

Twitter: lucd22


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

View solution in original post

17 Replies
LucD
Leadership
Leadership
Jump to solution

Try this

Get-View -ViewType VirtualMachine | %{
	$vm = $_
	$_.Config.Hardware.Device | Select @{N="VM name";E={$vm.Name}},@{N="HW name";E={$_.GetType().Name}},@{N="Label";E={$_.DeviceInfo.Label}}
} | Export-Csv "C:\VM-HW.csv" -NoTypeInformation

____________

Blog: LucD notes

Twitter: lucd22


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

brittonv
Contributor
Contributor
Jump to solution

Thank you so much for this.

Can you tell me, how could I have figured out this myself.

Assuming I found the "get-view -viewtype VirtualMachine"

Is there a command I could have run to see the config.hardware.device options so I could have selected them?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The Get-View -ViewType VirtualMachine is just a (faster) way of doing the same as Get-VM | Get-View.

To know what is available in the Managed Object you can look at the VMware vSphere API Reference Documentation.

But this can be quite overwhelming if you look at it for the first time Smiley Wink

There is also the Onyx tool that will show you PowerShell code for most of the action you do in the vSphere client.

But again it requires at least a bit of background knowledge to interprete what you're seeing.

There are a number of good blog posts on the vSphere SDK.

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

You can use the PowerShell Get-Member cmdlet to list all the properties and methods of an object:

Get-View -ViewType VirtualMachine | `
  ForEach-Object { $_.Config.hardware.Device} | `
  Get-Member

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
brittonv
Contributor
Contributor
Jump to solution

However if I do a:

get-view -viewtype virtualmachine | get-member

I don't see anything identifiying "config.hardware.device as an option....

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

If you do:

Get-View -ViewType VirtualMachine | Get-Member

You will see a line with:

Config                           Property   VMware.Vim.VirtualMachineConfigInfo Config {get;}

Then you know there is a property Config. So you can do:

Get-View -ViewType VirtualMachine | ForEach-Object { $_.Config}  | Get-Member

Now you will see a line with:

Hardware                   Property   VMware.Vim.VirtualHardware Hardware {get;set;}

So you know there is a property Config.Hardware. The next step is:

Get-View -ViewType VirtualMachine | ForEach-Object { $_.Config.Hardware}  | Get-Member

wich gives a line:

Device          Property   VMware.Vim.VirtualDevice[] Device {get;set;}

At last you know there is a property Config.Hardware.Device. The last step is:

Get-View -ViewType VirtualMachine | ForEach-Object { $_.Config.Hardware.Device}  | Get-Member

Of course you can find this also via the documentation. But as you see you can explore all the properties also from PowerCLI.

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The Managed Objects most of the time have nested properties (various levels).

Hugo wrote a very useful function to list all the nested properties.

See his List ALL properties and subproperties of a variable in Powershell post.

Be sure to read the comments to know how to use the function.

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
DanMan3395
Enthusiast
Enthusiast
Jump to solution

i know this is an old post but I was wondering if you could say what api's will add the CPU socket/cores and the memory to this output?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

This should do the trick

Get-View -ViewType VirtualMachine | %{

   $vm = $_

   $_.Config.Hardware.Device |

  Select @{N="VM name";E={$vm.Name}},

   @{N='CpuSockets';E={$vm.Config.Hardware.NumCPU}},

   @{N='Cores/Socket';E={$vm.Config.Hardware.NumCoresPerSocket}},

   @{N='MemoryMB';E={$vm.Config.Hardware.MemoryMB}},

   @{N="HW name";E={$_.GetType().Name}},

   @{N="Label";E={$_.DeviceInfo.Label}}

}


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

DanMan3395
Enthusiast
Enthusiast
Jump to solution

I take it that the way the api's are set up, there is no way to get cpu/mem to return in this script in the same format as the other components?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

How do you mean?


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

Reply
0 Kudos
DanMan3395
Enthusiast
Enthusiast
Jump to solution

out put from the original script:

VM name   HW name                      Label           

-------   -------                      -----           

xxxxxxx      VirtualIDEController         IDE 0           

xxxxxxx      VirtualIDEController         IDE 1           

xxxxxxx      VirtualPS2Controller         PS2 controller 0

xxxxxxx      VirtualPCIController         PCI controller 0

xxxxxxx      VirtualSIOController         SIO controller 0

xxxxxxx      VirtualKeyboard              Keyboard        

xxxxxxx      VirtualPointingDevice        Pointing device 

xxxxxxx      VirtualMachineVideoCard      Video card      

xxxxxxx      VirtualMachineVMCIDevice     VMCI device     

xxxxxxx      VirtualLsiLogicSASController SCSI controller 0

xxxxxxx      VirtualCdrom                 CD/DVD drive 1  

xxxxxxx      VirtualDisk                  Hard disk 1     

xxxxxxx      VirtualDisk                  Hard disk 2     

xxxxxxx      VirtualDisk                  Hard disk 3     

xxxxxxx      VirtualDisk                  Hard disk 4     

xxxxxxx      VirtualFloppy                Floppy drive 1  

xxxxxxx      VirtualVmxnet3 Network adapter 1

Output from the new one:

VM name      : apsed2313

CpuSockets   : 8

Cores/Socket : 1

MemoryMG     : 32768

HW name      : VirtualIDEController

Label        : IDE 0

VM name      : xxxxxxx

CpuSockets   : 8

Cores/Socket : 1

MemoryMG     : 32768

HW name      : VirtualIDEController

Label        : IDE 1

VM name      : xxxxxxx

CpuSockets   : 8

Cores/Socket : 1

MemoryMG     : 32768

HW name      : VirtualPS2Controller

Label        : PS2 controller 0

VM name      : xxxxxxx

CpuSockets   : 8

Cores/Socket : 1

MemoryMG     : 32768

HW name      : VirtualPCIController

Label        : PCI controller 0

VM name      : xxxxxxx

CpuSockets   : 8

Cores/Socket : 1

MemoryMG     : 32768

HW name      : VirtualSIOController

Label        : SIO controller 0

VM name      : xxxxxxx

I concatenated this one as it goes on for a long while.

For some reason the CPU information reformats the table output. Additionally the memory is not grabbed on a per vm basis but rather is re-grabbed for the same vm each iteration of the loop through devices.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The fact is that are too many columns to display on the screen, hence the PS output engine switches to listview instead of the defult tableview.

The memory is per VM.
Just checked and it seems to be ok for me.


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

DanMan3395
Enthusiast
Enthusiast
Jump to solution

yeah I understand, but it's not the tabular format I am referring too. Its the fact that CPU and memory info are redisplayed for the same vm on each iteration through the hardware loop, not the vm loop. Thus you end up with a redundant copy of the memory and cpu info for each other hardware item reported by the script. I wonder why VMware designed the api's so that the cpu and memory information is not in the normal hardware list.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can always open a RFC :smileygrin:

Basically the issue is how to report information while limiting the redundancy.

If you want to list the CPU and memory information as a device, it is not too hard to do that.
But would all the devices than have a property CPUCores for example?


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

DanMan3395
Enthusiast
Enthusiast
Jump to solution

Haha, maybe I should Smiley Wink I am surprised there isn't an built in commandlet for this yet as it seems like somethin that would be used a lot.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can submit ideas over here.


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