VMware Cloud Community
BrianDGeorge
Enthusiast
Enthusiast
Jump to solution

PowerCLI Script to Gather VM Guest Network Information Using get-view

Just can't get this to work right.  Name, IP, MacAddress, VlanID, and Network Adapter are fine but Gateway, SubnetMask, and DNS are all blanks. Any help would be appreciated.

Get-View -ViewType VirtualMachine -Filter @{'Runtime.PowerState'='poweredOn'} |

Select Name,

@{N='IPAddress';E={$_.Guest.ipAddress}},

@{N='Gateway';E={$nicInfo = Get-VMGuestNetworkInterface -VM $_ [string]::Join(',',($nicInfo | Select -ExpandProperty DefaultGateway))}},

@{N='SubnetMask';E={[string]::Join(‘,’,($subnetmask))}},

@{N='DNS';E={[string]::Join(',',($vm.ExtensionData.Guest.IpStack.DnsConfig.IpAddress))}},

@{N='MacAddress';E={($_.Config.Hardware.Device | where{$_ -is [VMware.Vim.VirtualEthernetCard]}).MacAddress -join '|'}},  

@{N='VlanId';E={$folder = Get-Folder VM

        $folderView = Get-View $folder -Property "[VirtualMachine]ChildEntity.Network.*"

        $folderView.LinkedView.ChildEntity[0].LinkedView.Network | Select-Object -ExpandProperty Name}},

@{N='Network Adapter';E={$_.Config.Hardware.Device  |  Where {$_ -is [VMware.Vim.VirtualEthernetCard]} | ForEach {$_.GetType().Name.Replace('Virtual','')}}} | Out-GridView

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Can you try like this

Get-View -ViewType VirtualMachine -Filter @{'Runtime.PowerState'='poweredOn'} |

Select Name,

@{N='IPAddress';E={$_.Guest.ipAddress}},

@{N='Gateway';E={($_.Guest.ipstack.iprouteconfig.iproute.gateway.ipaddress | where{$_ -ne $null}) -join '|'}},

@{N='SubnetMask';E={

    $ipAddr = $_.Guest.ipAddress

    @(($_.Guest.Net.ipconfig.ipaddress | where{$ipAddr -contains $_.IpAddress -and $_.PrefixLength -ne 0}).PrefixLength | %{

        [IPAddress]$ip = 0;

        $ip.Address = (([UInt32]::MaxValue) -shl (32 - $_) -shr (32 - $_))

        $ip.IPAddressToString

    }) -join '|'

}},

@{N='DNS';E={[string]::Join(',',($_.Guest.IpStack.DnsConfig.IpAddress))}},

@{N='MacAddress';E={($_.Config.Hardware.Device | where{$_ -is [VMware.Vim.VirtualEthernetCard]}).MacAddress -join '|'}},

@{N='VlanId';E={$folder = Get-Folder VM

        $folderView = Get-View $folder -Property "[VirtualMachine]ChildEntity.Network.*"

        $folderView.LinkedView.ChildEntity[0].LinkedView.Network | Select-Object -ExpandProperty Name}},

@{N='Network Adapter';E={$_.Config.Hardware.Device  |  Where {$_ -is [VMware.Vim.VirtualEthernetCard]} | ForEach {$_.GetType().Name.Replace('Virtual','')}}} |

Out-GridView

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

Was it helpful? Let us know by completing this short survey here.


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

View solution in original post

Reply
0 Kudos
39 Replies
LucD
Leadership
Leadership
Jump to solution

Can you try like this

Get-View -ViewType VirtualMachine -Filter @{'Runtime.PowerState'='poweredOn'} |

Select Name,

@{N='IPAddress';E={$_.Guest.ipAddress}},

@{N='Gateway';E={($_.Guest.ipstack.iprouteconfig.iproute.gateway.ipaddress | where{$_ -ne $null}) -join '|'}},

@{N='SubnetMask';E={

    $ipAddr = $_.Guest.ipAddress

    @(($_.Guest.Net.ipconfig.ipaddress | where{$ipAddr -contains $_.IpAddress -and $_.PrefixLength -ne 0}).PrefixLength | %{

        [IPAddress]$ip = 0;

        $ip.Address = (([UInt32]::MaxValue) -shl (32 - $_) -shr (32 - $_))

        $ip.IPAddressToString

    }) -join '|'

}},

@{N='DNS';E={[string]::Join(',',($_.Guest.IpStack.DnsConfig.IpAddress))}},

@{N='MacAddress';E={($_.Config.Hardware.Device | where{$_ -is [VMware.Vim.VirtualEthernetCard]}).MacAddress -join '|'}},

@{N='VlanId';E={$folder = Get-Folder VM

        $folderView = Get-View $folder -Property "[VirtualMachine]ChildEntity.Network.*"

        $folderView.LinkedView.ChildEntity[0].LinkedView.Network | Select-Object -ExpandProperty Name}},

@{N='Network Adapter';E={$_.Config.Hardware.Device  |  Where {$_ -is [VMware.Vim.VirtualEthernetCard]} | ForEach {$_.GetType().Name.Replace('Virtual','')}}} |

Out-GridView

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

Was it helpful? Let us know by completing this short survey here.


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

Reply
0 Kudos
BrianDGeorge
Enthusiast
Enthusiast
Jump to solution

I really cannot thank you enough.  You have saved my bacon on several of these.

Reply
0 Kudos
markshannon
Enthusiast
Enthusiast
Jump to solution

Hi,

this script is great - the only issue seems to be that it displays the same vlanid for all the vms?

how do i overcome this please?

thanks in advance

mark

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

There is indeed an issue in that code.

Try with this updated version

Get-View -ViewType VirtualMachine -Filter @{'Runtime.PowerState'='poweredOn'} -PipelineVariable vm |

Select Name,

@{N='IPAddress';E={$_.Guest.ipAddress}},

@{N='Gateway';E={($_.Guest.ipstack.iprouteconfig.iproute.gateway.ipaddress | where{$_ -ne $null}) -join '|'}},

@{N='SubnetMask';E={

    $ipAddr = $_.Guest.ipAddress

    @(($_.Guest.Net.ipconfig.ipaddress | where{$ipAddr -contains $_.IpAddress -and $_.PrefixLength -ne 0}).PrefixLength | %{

        [IPAddress]$ip = 0;

        $ip.Address = (([UInt32]::MaxValue) -shl (32 - $_) -shr (32 - $_))

        $ip.IPAddressToString

    }) -join '|'

}},

@{N='DNS';E={[string]::Join(',',($_.Guest.IpStack.DnsConfig.IpAddress))}},

@{N='MacAddress';E={($_.Config.Hardware.Device | where{$_ -is [VMware.Vim.VirtualEthernetCard]}).MacAddress -join '|'}},

@{N='VlanId';E={

        $folder = Get-View -Id $_.Parent -Property "[VirtualMachine]ChildEntity.Network.*"

        ($folder.LinkedView.ChildEntity.where({$vm.MoRef -eq $_.MoRef})).LinkedView.Network.Name}},

@{N='Network Adapter';E={$_.Config.Hardware.Device  |  Where {$_ -is [VMware.Vim.VirtualEthernetCard]} | ForEach {$_.GetType().Name.Replace('Virtual','')}}} |

Out-GridView


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

markshannon
Enthusiast
Enthusiast
Jump to solution

this worked a treat - thanks once again Smiley Happy

Reply
0 Kudos
bloomk
VMware Employee
VMware Employee
Jump to solution

Hey there!

First off, thank you so much for all these scripts you put together for all of us!  It is greatly appreciated!  With this script below you provided, would it be difficult to add what host the VM is currently running on as well?

Thanks again,

Kevin

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The following calculated property should do the trick,

@{N='VMHost';E={(Get-View -Id $_.Runtime.Host -Property Name).Name}}


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

bloomk
VMware Employee
VMware Employee
Jump to solution

That did it!  Thanks again!  Cheers

Reply
0 Kudos
winsolo
Enthusiast
Enthusiast
Jump to solution

Hey LucD

Could we add another hashtable with "networkname" of the filtered VM with its network in which the vNIC is connected with? I can't find a way to pull that info from the VM ExtensionData.Network Property. Although I can get the network name from Get-NetworkAdapter -VM $myvm, I don't know how this can be added to the existing script. Could we also do foreach to get the entire script run only for certain VMs, instead of running it against all the powered on VMs with -Filter @{'Runtime.PowerState'='poweredOn'} ?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The NetworkName that you get with Get-NetworkAdapter should be present in the VlanId property in the output.

Just be aware that this script will only work correctly when a VM has only 1 vNIC.

There is no need to have a ForEach to report on specific VMs.

You can do that with the Filter hash table.

For example

Get-View -ViewType VirtualMachine -Filter @{'Name'='^VM1$|^VM2$';'Runtime.PowerState'='poweredOn'} |

will only return the VMs whose names match exactly VM1 and VM2.

The right-hand side of an entry in the Filter hash table is a RegEx expression.

So you can use Anchors (^ and $) and the 'or' operator.

You can even construct that expression automatically.

Assume you have a .txt file with VM names, one per line.

Then you could do:

$strReg = (Get-Content -Path .\vmnames.txt | %{"^$_$"}) -join '|'

Get-View -ViewType VirtualMachine -Filter @{'Name'=$strReg;'Runtime.PowerState'='poweredOn'} |


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

winsolo
Enthusiast
Enthusiast
Jump to solution

Get-View -ViewType VirtualMachine -Filter @{'Name'='^testvm$';'Runtime.PowerState'='poweredOn'} | Select Name,

@{N='VMHost';E={(Get-View -Id $_.Runtime.Host -Property Name).Name}},

@{N='IPAddress';E={$_.Guest.ipAddress}},

@{N='Gateway';E={($_.Guest.ipstack.iprouteconfig.iproute.gateway.ipaddress | where{$_ -ne $null}) -join '|'}},

@{N='SubnetMask';E={

    $ipAddr = $_.Guest.ipAddress

    @(($_.Guest.Net.ipconfig.ipaddress | where{$ipAddr -contains $_.IpAddress -and $_.PrefixLength -ne 0}).PrefixLength | %{

        [IPAddress]$ip = 0;

        $ip.Address = (([UInt32]::MaxValue) -shl (32 - $_) -shr (32 - $_))

        $ip.IPAddressToString

    }) -join '|'

}},

@{N='DNS';E={[string]::Join(',',($_.Guest.IpStack.DnsConfig.IpAddress))}},

@{N='MacAddress';E={($_.Config.Hardware.Device | where{$_ -is [VMware.Vim.VirtualEthernetCard]}).MacAddress -join '|'}},

@{N='Network Adapter';E={$_.Config.Hardware.Device  |  Where {$_ -is [VMware.Vim.VirtualEthernetCard]} | ForEach {$_.GetType().Name.Replace('Virtual','')}}},

@{N='VlanId';E={

        $folder = Get-View -Id $_.Parent -Property "[VirtualMachine]ChildEntity.Network.*"

        ($folder.LinkedView.ChildEntity.where({$vm.MoRef -eq $_.MoRef})).LinkedView.Network.Name}} |

FL *

The above code produces the output with "VlanId" being empty, although the target VM has only 1 vNIC. However, I removed all the other hashtables and ran the following code and this one worked. This code is able to give me the network name.

Get-View -ViewType VirtualMachine -Filter @{'Name'='^testvm$';'Runtime.PowerState'='poweredOn'} -PipelineVariable vm | Select Name,

@{N='VlanId';E={

        $folder = Get-View -Id $_.Parent -Property "[VirtualMachine]ChildEntity.Network.*"

        ($folder.LinkedView.ChildEntity.where({$vm.MoRef -eq $_.MoRef})).LinkedView.Network.Name}}

| FL *

I don't know where the problem is.

Reply
0 Kudos
winsolo
Enthusiast
Enthusiast
Jump to solution

Sorry I figured it out.

I forgot the PipelineVariable Parameter. Once added, everything worked. Thanks again LucD

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The PipelineVariable parameter is missing

Get-View -ViewType VirtualMachine -Filter @{'Name'='^testvm$';'Runtime.PowerState'='poweredOn'} -PipelineVariable vm |


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

LucD
Leadership
Leadership
Jump to solution

Seems our entries crossed :smileygrin:


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

Reply
0 Kudos
Chf_WOW
Contributor
Contributor
Jump to solution

Outstanding script.  I'm new at this.  Question.  I used the command Get-VM to collect specific VMs to a text file.  How would the script be modified to read the text file or except a variable to produce the info for several VMs.
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The Filter parameter expects a hash table, so you can have multiple entries.
The right-hand side of each entry is a RegEx expression. In RegEx the '|' equals an OR .

So you could do 

#From a file
$names = Get-Content -Path .\vmnames.txt

#From a variable
$names = 'vm1','vm2','vm3'


$regexNames = $names -join '|'

Get-View -ViewType VirtualMachine -Filter @{'Name'=$regexNames;'Runtime.PowerState'='poweredOn'} 

 

But since we are dealing with a RegEx, the above would also return VM1, VM11, VM1XYZ ...
To fix that, use positioning indicators in the expression.
Something like this

$regexNames = $names.Foreach{"^$($_)$"} -join '|'


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

Reply
0 Kudos
jsmithn
Contributor
Contributor
Jump to solution

Thanks, this works great. I did not know about the gridview, that's a nifty output mechanism! How would the script be modified to retrieve all vNICS (and thus VLANs, IP, DNS, etc) for VMs?

Reply
0 Kudos
Chris_95
Contributor
Contributor
Jump to solution

Hello,

I don't work for me...

Here is the script that i use: (concatenated from every discuss about this post and based on content file)

$names = Get-Content -Path .\vmnames.txt
$regexNames = $names.Foreach{"^$($_)$"} -join '|'
Get-View -ViewType VirtualMachine -Filter @{'Name'=$regexNames;'Runtime.PowerState'='poweredOn'}

@{N='IPAddress';E={$_.Guest.ipAddress}},

@{N='Gateway';E={($_.Guest.ipstack.iprouteconfig.iproute.gateway.ipaddress | where{$_ -ne $null}) -join '|'}},

@{N='SubnetMask';E={

$ipAddr = $_.Guest.ipAddress

@(($_.Guest.Net.ipconfig.ipaddress | where{$ipAddr -contains $_.IpAddress -and $_.PrefixLength -ne 0}).PrefixLength | %{

[IPAddress]$ip = 0;

$ip.Address = (([UInt32]::MaxValue) -shl (32 - $_) -shr (32 - $_))

$ip.IPAddressToString

}) -join '|'

}},

@{N='DNS';E={[string]::Join(',',($_.Guest.IpStack.DnsConfig.IpAddress))}},

@{N='MacAddress';E={($_.Config.Hardware.Device | where{$_ -is [VMware.Vim.VirtualEthernetCard]}).MacAddress -join '|'}},

@{N='VlanId';E={

$folder = Get-View -Id $_.Parent -Property "[VirtualMachine]ChildEntity.Network.*"

($folder.LinkedView.ChildEntity.where({$vm.MoRef -eq $_.MoRef})).LinkedView.Network.Name}},

@{N='Network Adapter';E={$_.Config.Hardware.Device | Where {$_ -is [VMware.Vim.VirtualEthernetCard]} | ForEach {$_.GetType().Name.Replace('Virtual','')}}} |

...But result is very strange :

See capture joined

Have an idea ?

Thanks

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

It looks like you might be missing a pipeline symbol and a Select cmdlet.

Get-View -ViewType VirtualMachine -Filter {'Name'=$regexNames;'Runtime.PowerState'='poweredOn'} |
Select @{N='IPAddress';E={$_.Guest.ipAddress}},

 


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

Reply
0 Kudos