VMware Cloud Community
richG
Contributor
Contributor

Help with script


I have started this script and I am trying to figure out method types

get-vmhost | sort name | get-view |

select name,

@{N="Datacenter";E={Get-Datacenter -VMHost $_.Name}},

@{N="Type";E={$_.hardware.systeminfo.vendor+" "+ $_.Hardware.systeminfo.model}},

@{N="CPU"; E={"PROC:" + $_.Hardware.CpuInfo.NumCPUPackages + "Cores:" +

$_.Hardware.CpuInfoNumCpuCores + "MHZ:" + [math]::round($_.Hardware.CpuInfo.Hz/1000000,0)}}

This part works like a charm.

I guess what I am trying to understand $_.hardware.systeminfo.model  I know it extracts the information I want and it is using the Typename but how does this work?

The next part works on its own

Get-vmhostnetworkadapter  <servername> -vmkernel | select name,ip,subnetmask

This works fine but when I try to do this I have issues.

get-vmhost | sort name | get-view |

select name,

@{N="NIC";E={$_.Host.Networking.Nic.HostVMKernelVirtualNicImpl.Name}},

@{N="IP";E={$_.Host.Networking.Nic.HostVMKernelVirtualNicImpl.IP}},

@{N="Subnetmask";E={$_.Host.Networking.Nic.HostVMKernelVirtualNicImpl.subnetmask}}

This is the type name I get when I do GM on the command Get-vmhostnetworkadapter  <servername> -vmkernel | select name,ip,subnetmask

TypeName: Selected.VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.Nic.HostVMKernelVirtualNicImpl

So can it be done?

My goal is the following..

get-vmhost | sort name | get-view |

select name,

@{N="Datacenter";E={Get-Datacenter -VMHost $_.Name}},

@{N="Type";E={$_.hardware.systeminfo.vendor+" "+ $_.Hardware.systeminfo.model}},

@{N="CPU"; E={"PROC:" + $_.Hardware.CpuInfo.NumCPUPackages + "Cores:" +

$_.Hardware.CpuInfoNumCpuCores + "MHZ:" + [math]::round($_.Hardware.CpuInfo.Hz/1000000,0)}},

@{N="MEM";E={"" +[math]::round($_.Hardware.MemorySize / 1GB,0) + "GB"}},

@{N="NIC";E={$_.Host.Networking.Nic.HostVMKernelVirtualNicImpl.Name}},

@{N="IP";E={$_.Host.Networking.Nic.HostVMKernelVirtualNicImpl.IP}},

@{N="Subnetmask";E={$_.Host.Networking.Nic.HostVMKernelVirtualNicImpl.subnetmask}} | Export-Csv c:\pscripts\hostinfo.csv

If someone can explain or point me in the direction of how to extract data from multiple cmdlets using a simple method it would be greatly appreciated.. I have just about every book out there so it is in a book please just point me to it and I will read it.

Thanks in advance.

0 Kudos
6 Replies
LucD
Leadership
Leadership

You should use a nested ForEach loop, where the inner loop runs over the VMKernel NICs for each host.

&{foreach($esx in (Get-VMHost | Sort-Object -Property Name)){
 
Get-VMHostNetworkAdapter -VMHost $esx -VMKernel |
 
Select @{N="Name,";E={$esx.Name}},
 
@{N="Datacenter";E={Get-Datacenter -VMHost $esx | Select -ExpandProperty Name}},
 
@{N="Type";E={$esx.ExtensionData.Hardware.SystemInfo.Vendor + " " + $esx.ExtensionData.Hardware.SystemInfo.Model}},
 
@{N="CPU"; E={"PROC:" + $esx.ExtensionData.Hardware.CpuInfo.NumCPUPackages + "Cores:" + $esx.ExtensionData.Hardware.CpuInfoNumCpuCores + "MHZ:" + [math]::round($esx.ExtensionData.Hardware.CpuInfo.Hz/1000000,0)}},
 
@{N="MEM";E={"" +[math]::round($esx.Hardware.MemorySize / 1GB,0) + "GB"}},
 
@{N="NIC";E={$_.Name}},
 
IP,Subnetmask
}}
| Export-Csv c:\hostinfo.csv -NoTypeInformation -UseCulture

Note that the script uses the call operator (&) to get the objects, produced by the Select-Object cmdlet, on the pipeline.


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

0 Kudos
richG
Contributor
Contributor

HI LucD,

Thank you very much for explaining it.. I have some other questions which I would like to post here later today or tomorrow.. Thanks very much..

Power Shell is great, but one of my problems is one Cmdlet pulls some objects, another cmdlet pulls some other information, which is needed. When I try to pull this all together it when I run into issues.. I hope this make sense, kind of hard to explain..

Rich

0 Kudos
LucD
Leadership
Leadership

Sure does, PS can be daunting in the beginning.


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

0 Kudos
gamename
Enthusiast
Enthusiast

LucD wrote:

You should use a nested ForEach loop, where the inner loop runs over the VMKernel NICs for each host.

&{foreach($esx in (Get-VMHost | Sort-Object -Property Name)){
 
Get-VMHostNetworkAdapter -VMHost $esx -VMKernel |
 
Select @{N="Name,";E={$esx.Name}},
 
@{N="Datacenter";E={Get-Datacenter -VMHost $esx | Select -ExpandProperty Name}},
 
@{N="Type";E={$esx.ExtensionData.Hardware.SystemInfo.Vendor + " " + $esx.ExtensionData.Hardware.SystemInfo.Model}},
 
@{N="CPU"; E={"PROC:" + $esx.ExtensionData.Hardware.CpuInfo.NumCPUPackages + "Cores:" + $esx.ExtensionData.Hardware.CpuInfoNumCpuCores + "MHZ:" + [math]::round($esx.ExtensionData.Hardware.CpuInfo.Hz/1000000,0)}},
 
@{N="MEM";E={"" +[math]::round($esx.Hardware.MemorySize / 1GB,0) + "GB"}},
 
@{N="NIC";E={$_.Name}},
 
IP,Subnetmask
}}
| Export-Csv c:\hostinfo.csv -NoTypeInformation -UseCulture

Note that the script uses the call operator (&) to get the objects, produced by the Select-Object cmdlet, on the pipeline.

LucD: What browser/tools do you use to post such beautifully formatted code? Smiley Happy

0 Kudos
LucD
Leadership
Leadership

0 Kudos
richG
Contributor
Contributor

Hi LucD,

I have some questions for you. I really hoping you can help. I have read a bunch of PS books trying to figure out Looping namely ForEach-Object  or the ForEach.

The problem I have is when folks try to explain how to use it they do not use a Virtual examples.

So you have helped me in the past and I really appreciate it, but I am trying to understand nesting and how to configure this for the right output.
Here is one that I have built and it does work.

get-cluster | foreach-object { write "Datacenter: $($_.name) `n"
                $_ | Get-vmhost | foreach-object {
                write "ESX Host: $($_.Name) `n`n"
                $_ | get-vm | foreach-object {
                write "$($_.name)"
                }
                }
                write-output ""
                }
Here is another one that I am unable to work.  Now I know it is wrong but this is what I am trying to understand..
get-cluster | foreach-object { write "Datacenter: $($_.name) `n"
                $_ | Get-vmhost | foreach-object {
                write "ESX Host: $($_.Name) `n`n"
                $_ | get-vm | get-networkadapter | where {$_.type -ne "Vmxnet3"}   #it works up till here after that is fails.
                foreach-object { write "$($_.parent)" write "$($_.name)" Write "$($_.type)"
                }
                }
                write-output ""
                }

Here is some of the things I am trying to do and understand.
Pull the Datacenter, VMHost, Virtual Machine. The pull some information from the Guest system. But I would still need to be able to print out the Datacenter name, Vmhost name, and guest name plus other information.

Another case would be say print out host and then drill down into the guest and pull out information.

So what I am looking to understand is nesting.

I have look at different websites but still not found anything to help.

Hal Rottenberg wrote a book and that has helped me some, but I feel things have changed since his book came out. I have your book as well, plus all of Don Jones book.. I will admit I work so much better off of examples as long as I can understand them.

So I know you are very busy but if you could find some time it would be greatly appreciated.

Thanks
Kind regards,
RichG

0 Kudos