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

39 Replies
Chris_95
Contributor
Contributor
Jump to solution

 
0 Kudos
Chris_95
Contributor
Contributor
Jump to solution

Hello LucD

Thank you very much for the quick reply, but it still doesn't work...

I repeat that I did not invent this script but just copy/paste from YOUR script!

So if you wrote that there is an error, this script is not functional.

Regards,

See attach file

0 Kudos
LucD
Leadership
Leadership
Jump to solution

From that error alone I can't really see what is wrong with your code.
Please add the code as you are currently using it.


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

0 Kudos
Chris_95
Contributor
Contributor
Jump to solution

Here is the script that i post previously... the same

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

My code is functional, but it looks like you made some copying errors.
The Filter is not a hash table (you forgot the @) and the last calculated property has a pipeline symbol at the end that shouldn't be there.

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

Get-View -ViewType VirtualMachine -Filter @{'Name' = $regexNames; 'Runtime.PowerState' = 'poweredOn' } |
Select-Object @{N = 'IPAddress'; E = { $_.Guest.ipAddress } },
@{N = 'Gateway'; E = { ($_.Guest.ipstack.iprouteconfig.iproute.gateway.ipaddress | Where-Object { $_ -ne $null }) -join '|' } },
@{N = 'SubnetMask'; E = {
    $ipAddr = $_.Guest.ipAddress
    @(($_.Guest.Net.ipconfig.ipaddress | Where-Object { $ipAddr -contains $_.IpAddress -and $_.PrefixLength -ne 0 }).PrefixLength | ForEach-Object {
        [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-Object { $_ -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-Object { $_ -is [VMware.Vim.VirtualEthernetCard] } | ForEach-Object { $_.GetType().Name.Replace('Virtual', '') } } }

 


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

0 Kudos
Chris_95
Contributor
Contributor
Jump to solution

Thanks so much LucD !

Dont' know why some special characters have disappeared.

Have a nice day

0 Kudos
Chris_95
Contributor
Contributor
Jump to solution

...just a remark before closing the subject, the vLAN area is empty whereas it exists well .

See attach file

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That was mentioned in this thread, you need to add the PipelineVariable parameter

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


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

0 Kudos
Chris_95
Contributor
Contributor
Jump to solution

Sorry LucD to be a little bit perfectionist but when i have multiple network card, informations doesn't appear.

I can have up to 8 network cards.

Thanks for reply

 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try changing that calculated property to

@{N = 'VlanId'; E = {
    $folder = Get-View -Id $_.Parent -Property "[VirtualMachine]ChildEntity.Network.*"
($folder.LinkedView.ChildEntity.where({ $vm.MoRef -eq $_.MoRef })).LinkedView.Network.Name -join '|' }


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

0 Kudos
Chris_95
Contributor
Contributor
Jump to solution

Hello LucD,

Thanks so much to help me ! 

I modified the script with the command line that you writed but i obtained error message

 

0 Kudos
Chris_95
Contributor
Contributor
Jump to solution

...sorry for the last message where i I did not post the error message.

See attach file

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I assume you modified the calculated property like this?
From the error message, it looks like you dropped the curly brace on the last line.

@{N = 'VlanId'; E = {
    $folder = Get-View -Id $_.Parent -Property "[VirtualMachine]ChildEntity.Network.*"
($folder.LinkedView.ChildEntity.where({ $vm.MoRef -eq $_.MoRef })).LinkedView.Network.Name -join '|' }
},

 


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

0 Kudos
Chris_95
Contributor
Contributor
Jump to solution

OK but vLANID area is still empty 😞 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sorry, but I'm going to stop replying.
My script is probably not functional as you already kindly reminded me before.


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

0 Kudos
Chris_95
Contributor
Contributor
Jump to solution

Sorry LucD, I'm taking too much of your time, it's already a lot of solicitation and exchange !

Thanks so much 😉

0 Kudos
myVonage
Contributor
Contributor
Jump to solution

I have a need to also gather the vmnic that is being used by the guest.

I pulled info from this https://github.com/vmware/PowerCLI-Example-Scripts/blob/master/Scripts/Get-VMNetworkPortId.ps1

and put together this piece, but I am not great at making things look pretty but get the job done.

$Servers=Get-VMHost | ? {$_.ConnectionState -ne "Maintenance"}
Foreach($esx in $Servers){
$esxcli=Get-EsxCli -VMHost $esx -V2
$VMs=Get-VMHost -Name $esx | Get-VM | ? {$_.PowerState -eq "PoweredOn"} | sort
foreach($v in $VMs){
$vmobj = Get-VM -Name $v.Name -erroraction silentlycontinue
if (!$vmobj) {Write-Verbose "No VM found by the name $v."}
else {
$vmNetInfo = $esxcli.network.vm.list.Invoke() | ?{$_.Name -eq $vmobj.Name}
$portArgs = $esxcli.network.vm.port.list.CreateArgs()
$portArgs.worldid = $vmNetInfo.WorldID
$tempout= $esxcli.network.vm.port.list.Invoke($portArgs)
write-host $v.Name, $tempout.TeamUplink
}
}
}

This does give me what I need, but would like to incorporate with the script in this thread as that data is useful as well, but I have not had much luck so far

0 Kudos
wetnose88
Enthusiast
Enthusiast
Jump to solution

Hi LucD,

I observed something which I can't explain. When I use Export-Excel, most of the columns are same as export-csv, but VlanId and Network Adapter columns changed to System.Object[] for most of the VM. I tested on 6.7 and 7.0.3.

The ONLY difference is use Export-Excel and Export-Csv

The code is put here is just the last part of your script.

@{N = 'VlanId'; E = {
    $folder = Get-View -Id $_.Parent -Property "[VirtualMachine]ChildEntity.Network.*"
    ($folder.LinkedView.ChildEntity.where({ $vm.MoRef -eq $_.MoRef })).LinkedView.Network.Name -join '|' }},

@{N='Network Adapter';E={$_.Config.Hardware.Device  |  Where {$_ -is [VMware.Vim.VirtualEthernetCard]} | ForEach {$_.GetType().Name.Replace('Virtual','')}}} |
  Export-Excel "$outputfile " -WorkSheetname "VM_$($vcenter)"
VlanIdNetwork Adapter
System.Object[]System.Object[]
System.Object[]System.Object[]
System.Object[]System.Object[]
System.Object[]System.Object[]
System.Object[]System.Object[]
System.Object[]System.Object[]
System.Object[]System.Object[]
System.Object[]System.Object[]
System.Object[]System.Object[]
System.Object[]System.Object[]
System.Object[]System.Object[]
System.Object[]System.Object[]

When I use export-csv, the results are working as I have expected

@{N = 'VlanId'; E = {
    $folder = Get-View -Id $_.Parent -Property "[VirtualMachine]ChildEntity.Network.*"
    ($folder.LinkedView.ChildEntity.where({ $vm.MoRef -eq $_.MoRef })).LinkedView.Network.Name -join '|' }},

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

 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That means you are passing an array to that cell (and it is handled the same in CSV as in XLSX).

The VlanId property works for as you posted it.
The 'Network Adapter property is missing the -join operator

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


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

0 Kudos
wetnose88
Enthusiast
Enthusiast
Jump to solution

Thanks a lot, it did the track.

#@{N='Network Adapter';E={$_.Config.Hardware.Device  |  Where {$_ -is [VMware.Vim.VirtualEthernetCard]} | ForEach {$_.GetType().Name.Replace('Virtual','')}}} |
@{N='Network Adapter';E={($_.Config.Hardware.Device  |Where {$_ -is [VMware.Vim.VirtualEthernetCard]} | ForEach {$_.GetType().Name.Replace('Virtual','')}) -join '|'}}|
#export-csv $outputfile -NoTypeInformation -UseCulture 
Export-Excel "$outputfile " -WorkSheetname "VM_$($vcenter)"
0 Kudos