Highlighted
Enthusiast
Enthusiast

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

Jump to solution

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
Highlighted
User Moderator
User Moderator

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

0 Kudos
14 Replies
Highlighted
User Moderator
User Moderator

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

0 Kudos
Highlighted
Enthusiast
Enthusiast

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

0 Kudos
Highlighted
Enthusiast
Enthusiast

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

0 Kudos
Highlighted
User Moderator
User Moderator

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

Highlighted
Enthusiast
Enthusiast

this worked a treat - thanks once again Smiley Happy

0 Kudos
Highlighted
VMware Employee
VMware Employee

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

0 Kudos
Highlighted
User Moderator
User Moderator

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

Highlighted
VMware Employee
VMware Employee

That did it!  Thanks again!  Cheers

0 Kudos
Highlighted
Enthusiast
Enthusiast

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'} ?

0 Kudos
Highlighted
User Moderator
User Moderator

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

0 Kudos
Highlighted
Enthusiast
Enthusiast

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.

0 Kudos
Highlighted
Enthusiast
Enthusiast

Sorry I figured it out.

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

0 Kudos
Highlighted
User Moderator
User Moderator

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

Highlighted
User Moderator
User Moderator

Seems our entries crossed :smileygrin:


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

0 Kudos