VMware Cloud Community
AlbertWT
Virtuoso
Virtuoso

Need assistance in modifying script to display VM Network Information

Hi,

I need some assistance from anyone here to modify the script so that the Gateway, Subnet Mask and both DNS servers result can be listed or exported to the CSV file. See the below partially working script:

&{foreach($vm in (Get-VM)) {

    $vm.ExtensionData.Guest.Net | select -Property @{N='VM';E={$vm.Name}},

    @{N='Host';E={$vm.VMHost.Name}},

    @{N='OS';E={$vm.Guest.OSFullName}},

    @{N='Tools';E={$vm.ExtensionData.Guest.ToolsRunningStatus}},

    @{N='NicType';E={[string]::Join(',',(Get-NetworkAdapter -Vm $vm | Select-Object -ExpandProperty Type))}},

    @{N='VLAN';E={[string]::Join(',',(Get-NetworkAdapter -Vm $vm | Select-Object -ExpandProperty NetworkName))}},

    @{N='IP';E={[string]::Join(',',($vm.Guest.IPAddress | Where {($_.Split(".")).length -eq 4}))}},

    @{N='Gateway';E={[string]::Join(',',($vm.ExtensionData.Guest.IpStack.IpRouteConfig.IpRoute | %{if($_.Gateway.IpAddress){$_.Gateway.IpAddress}}))}},

    @{N='Subnet Mask';E={

                $dec = [Convert]::ToUInt32($(('1' * $_.IpConfig.IpAddress[0].PrefixLength).PadRight(32, '0')), 2)

                $DottedIP = $( For ($i = 3; $i -gt -1; $i--) {

                        $Remainder = $dec % [Math]::Pow(256, $i)

                        ($dec - $Remainder) / [Math]::Pow(256, $i)

                        $dec = $Remainder

                    } )

                [String]::Join('.', $DottedIP)

            }},

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

    @{N='MAC';E={[string]::Join(',',$_.MacAddress)}}

  } Export-Csv -NoTypeInformation -Path C:\temp\VMIPDetails.csv

}

Thanks

/* Please feel free to provide any comments or input you may have. */
6 Replies
LucD
Leadership
Leadership

Try like this

&{foreach($vm in (Get-VM)) { 

    $vm.ExtensionData.Guest.Net |

    select -Property @{N='VM';E={$vm.Name}}, 

      @{N='Host';E={$vm.VMHost.Name}}, 

      @{N='OS';E={$vm.Guest.OSFullName}}, 

      @{N='Tools';E={$vm.ExtensionData.Guest.ToolsRunningStatus}}, 

     @{N='NicType';E={[string]::Join(',',(Get-NetworkAdapter -Vm $vm | Select-Object -ExpandProperty Type))}}, 

      @{N='VLAN';E={[string]::Join(',',(Get-NetworkAdapter -Vm $vm | Select-Object -ExpandProperty NetworkName))}}, 

      @{N='IP';E={[string]::Join(',',($vm.Guest.IPAddress | Where {($_.Split(".")).length -eq 4}))}}, 

      @{N='Gateway';E={[string]::Join(',',(

                $vm.ExtensionData.Guest.IpStack.IpRouteConfig.IpRoute |%{

                if($_.Gateway.IpAddress){

                  $_.Gateway.IpAddress

                }}))}}, 

      @{N='Subnet Mask';E=

                $dec = [Convert]::ToUInt32($(('1' * $_.IpConfig.IpAddress[0].PrefixLength).PadRight(32, '0')), 2

                $DottedIP = $( For ($i = 3; $i -gt -1; $i--) { 

                        $Remainder = $dec % [Math]::Pow(256, $i

                        ($dec - $Remainder) / [Math]::Pow(256, $i

                        $dec = $Remainder 

                    }) 

                [String]::Join('.', $DottedIP)  

            }}, 

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

     @{N='MAC';E={[string]::Join(',',$_.MacAddress)}} 

}} | Export-Csv -NoTypeInformation -Path C:\temp\VMIPDetails.csv -UseCulture 

 


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

AlbertWT
Virtuoso
Virtuoso

Thanks for the reply Luc,

However, still it doesn't show the Gateway, Subnet and the DNS entry for me. I have verified that VMware tools is running in all VMs and updated. There is no offline VM in my environment.

PowerCLI Version

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

   VMware vSphere PowerCLI 5.8 Release 1 build 2057893

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

Snapin Versions

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

   VMWare AutoDeploy PowerCLI Component 5.5 build 1983942

   VMWare ImageBuilder PowerCLI Component 5.5 build 1983942

   VMware License PowerCLI Component 5.5 build 1265954

   VMware Storage PowerCLI Component 5.8 build 2057894

   VMware VDS PowerCLI Component 5.8 build 2031581

   VMware vSphere PowerCLI Component 5.8 build 2031581

/* Please feel free to provide any comments or input you may have. */
Reply
0 Kudos
LucD
Leadership
Leadership

Just tested again, and it works for me.

What is in the resulting CSV, empty cells under these columns ?

The columns are missing altogether ?

Any error messages when you run the script ?

Can you perhaps attach the script you are using as a file to this thread ?

There might have been an issue during the copy/paste.


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

Reply
0 Kudos
anil_linux
Contributor
Contributor

Hello Lukd,

how to join your group & post blog can you let me know please?

Reply
0 Kudos
LucD
Leadership
Leadership

Which group do you mean ? On FaceBook ?


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

Reply
0 Kudos
anil_linux
Contributor
Contributor

Thank you for responding me,

Want to follow your blogpost on vmware site as you replied me earlier if

possible.....

Reply
0 Kudos