vin01
Expert
Expert

guest os ipaddress,subnet and gateway information

Jump to solution

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

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

20 Replies
LucD
Leadership
Leadership

What is the issue ?

Missing data, error messages .... ?


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

0 Kudos
vin01
Expert
Expert

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
0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
vin01
Expert
Expert

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
0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
vin01
Expert
Expert

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
0 Kudos
LucD
Leadership
Leadership

Perhaps try like this

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


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

0 Kudos
vin01
Expert
Expert

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

  }

}

   


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

View solution in original post

vin01
Expert
Expert

Thanks for quick replySmiley HappyYup it worked.

Regards Vineeth.K
0 Kudos
AlbertWT
Virtuoso
Virtuoso

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

the script can be executed successfully.

/* Any kind of comment or input would be greatly appreciated */
0 Kudos
LucD
Leadership
Leadership

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

Hi Luc ,

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

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
amitkadam10
Contributor
Contributor

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.

0 Kudos
bikashyadav
Contributor
Contributor

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

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
bikashyadav
Contributor
Contributor

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)

            }}

0 Kudos
bikashyadav
Contributor
Contributor

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

        }

0 Kudos