VMware Cloud Community
vin01
Expert
Expert
Jump to solution

guest os ipaddress,subnet and gateway information

Hello

Can Some one correct me on below script...

&{foreach($vm in (Get-VM)){
$vm.ExtensionData.Guest.Net |Select
@{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 -ExpandProperty Type))},
@{N="VLAN";E={[string]::Join(',',(Get-NetworkAdapter -Vm $vm | Select -ExpandProperty NetworkName))},
@{N="IP";E={[string]::Join(',',$vm.Guest.IPAddress)},
@{N="Gateway";E={[string]::Join(',',$vm.ExtensionData.Guest.IpStack.IpRouteConfig.IpRoute.Gateway.IpAddress )| where {$_ -ne $null}},
{
foreach ($iproute in $vm.ExtensionData.Guest.IpStack.IpRouteConfig.IpRoute) {

    if (($vm.Guest.IPAddress -replace "[0-9]$|[1-9][0-9]$|1[0-9][0-9]$|2[0-4][0-9]$|25[0-5]$", "0" | select -uniq) -contains $iproute.Network)
    }
    }
@{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(',',$vm.MacAddress)}}
}
}
}
}
}

Regards Vineeth.K
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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)}}

  }

}

   


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

View solution in original post

22 Replies
LucD
Leadership
Leadership
Jump to solution

What is the issue ?

Missing data, error messages .... ?


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

Reply
0 Kudos
vin01
Expert
Expert
Jump to solution

I didn't tried showing errors Some where in the script...

Line 13  if (($vm.Guest.IPAddress -replace "[0-9]$|[1-9][0-9]$|1[0-9][0-9]$|2[0-4][0-9]$|25[0-5]$", "0" | select -uniq) -contains $iproute.Network) 

missing statement block after if ( Condition)

===

Line 24 }}, 

missing '=' operator after key in hash literal.

The hash literal was incomplete.

Regards Vineeth.K
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

There were a lot of curly braces missing in the calculated properties.

I corrected those.

I have no clue what the purpose of the code I commented out would be.

&{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)}}, 

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

#    foreach ($iproute in $vm.ExtensionData.Guest.IpStack.IpRouteConfig.IpRoute) { 

#        if (($vm.Guest.IPAddress -replace "[0-9]$|[1-9][0-9]$|1[0-9][0-9]$|2[0-4][0-9]$|25[0-5]$", '0' | Select-Object -uniq) -contains $iproute.Network)

#        } 

#        } 

    @{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)}} 

  } 

}  


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

Reply
0 Kudos
vin01
Expert
Expert
Jump to solution

Thanks luc for correcting..I'm trying to pull guest OS IP,Subnet,gateway & DNS where guest tools are installed the below code works but only thing i am missing is gateway can you help me to get correct gateway.

Regards Vineeth.K
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

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


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

Reply
0 Kudos
vin01
Expert
Expert
Jump to solution

now it is writing gateway for most of the servers.But its fails to write for non-windows vms..What could be the reason? For these vms guest tools are Ok any way i will execute on complete datacenter and revert you back.A small query can we filter ips and exclude ipv6..

like below ..

Get-vm | Where-Object {$_.split(".").length -eq 4}) -join ","  -- correct me pls this is not working.

Regards Vineeth.K
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Perhaps try like this

Get-vm | Where-Object {($_.Guest.IPAddress.Split(".")).length -eq 4}


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

Reply
0 Kudos
vin01
Expert
Expert
Jump to solution

Thanks Luc With the above script i can retain more that 85% servers IP,GW,Netmask.Here I'm looking to exclude IPV6 information in output (10.50.37.146,fe80::250:56ff:fe99:264a) as we don't use IPV6.Could it be possible with above code.If yes where should i need to keep the filter.

Regards Vineeth.K
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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)}}

  }

}

   


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

vin01
Expert
Expert
Jump to solution

Thanks for quick replySmiley HappyYup it worked.

Regards Vineeth.K
Reply
0 Kudos
AlbertWT
Virtuoso
Virtuoso
Jump to solution

So if the Gateway and DNS fields are not populated, what could be the issue ?

the script can be executed successfully.

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

That would mean that the Guest properties of the VM are not all there.

This can have multiple reasons: the VMware Tools are not installed, the VM is powered off for some time...


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

amitkadam10
Contributor
Contributor
Jump to solution

Hi Luc ,

How do we put all this data in a excel or direct the output to a excel ?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Add | Export-Csv report.csv -NoTypeInformation -UseCulture to the end.

Don't forget the pipe symbol (vertical bar)


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

Reply
0 Kudos
amitkadam10
Contributor
Contributor
Jump to solution

Hi Luke ,

I got the output working in excel, when checked ahead on the gateway tab it has many entries .

Ideally gateway should have one IP address but in the output it is taking it multiple times because of the routes added.

Is there a way to amend that ? let me know.

Reply
0 Kudos
bikashyadav
Contributor
Contributor
Jump to solution

Hi LucD,

My output is fine except the subnet is blank. How can we make it work to get the subnet info as well?

Thanks

Bikash

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

What does return for the VMs on which you don't get a subnetmask?

Get-View -ViewType VirtualMachine -PipelineVariable vm |

ForEach-Object -Process {

    $vm.Guest.Net |

    ForEach-Object -Process {

        $_.IpConfig.IpAddress |

        Select @{N='VM';E={$vm.Name}},IpAddress,PrefixLength

    }

}


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

Reply
0 Kudos
bikashyadav
Contributor
Contributor
Jump to solution

I just found that. Just changing the value from [0] to [1] showed me the subnet. Marked in bold.

How do i modify this script to get same info for a VM with 2 nics ?

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

                $dec = [Convert]::ToUInt32($(('1' * $_.IpConfig.IpAddress[1].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)

            }}

Reply
0 Kudos
bikashyadav
Contributor
Contributor
Jump to solution

Hi LucD,

Below script works fine for me with multiple nics. But i am not able to add the gateway & dns parameter to it for each nic. Can you please help me with this ?

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

    $vm.ExtensionData.Guest.Net | Select @{N="VM";E={$vm.Name}},MacAddress,Network,

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

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

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

    @{N="DHCP";E={$_.IpConfig.Dhcp.Ipv4.Enable}},

   

  

    @{N="IP-1";E={$_.IpAddress[0]}},

    @{N="Subnet Mask-1";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='NicType1';E={(Get-NetworkAdapter -Vm $vm | Select-Object -ExpandProperty Type)[0]}},

    @{N="IP-2";E={$_.IpAddress[1]}},

    @{N="Subnet Mask-2";E={

            $dec = [Convert]::ToUInt32($(("1" * $_.IpConfig.IpAddress[1].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='NicType2';E={(Get-NetworkAdapter -Vm $vm | Select-Object -ExpandProperty Type)[1]}}

        }

Reply
0 Kudos