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
Can you try like this
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
Can you try like this
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
I really cannot thank you enough. You have saved my bacon on several of these.
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
There is indeed an issue in that code.
Try with this updated version
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
this worked a treat - thanks once again
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
The following calculated property should do the trick,
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That did it! Thanks again! Cheers
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'} ?
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
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:
Get-View -ViewType VirtualMachine -Filter @{'Name'=$strReg;'Runtime.PowerState'='poweredOn'} |
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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.
Sorry I figured it out.
I forgot the PipelineVariable Parameter. Once added, everything worked. Thanks again LucD
The PipelineVariable parameter is missing
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Seems our entries crossed :smileygrin:
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The Filter parameter expects a hash table, so you can have multiple entries.
The right-hand side of each entry is a RegEx expression. In RegEx the '|' equals an OR .
So you could do
#From a file
$names = Get-Content -Path .\vmnames.txt
#From a variable
$names = 'vm1','vm2','vm3'
$regexNames = $names -join '|'
Get-View -ViewType VirtualMachine -Filter @{'Name'=$regexNames;'Runtime.PowerState'='poweredOn'}
But since we are dealing with a RegEx, the above would also return VM1, VM11, VM1XYZ ...
To fix that, use positioning indicators in the expression.
Something like this
$regexNames = $names.Foreach{"^$($_)$"} -join '|'
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks, this works great. I did not know about the gridview, that's a nifty output mechanism! How would the script be modified to retrieve all vNICS (and thus VLANs, IP, DNS, etc) for VMs?
Hello,
I don't work for me...
Here is the script that i use: (concatenated from every discuss about this post and based on content file)
$names = Get-Content -Path .\vmnames.txt
$regexNames = $names.Foreach{"^$($_)$"} -join '|'
Get-View -ViewType VirtualMachine -Filter @{'Name'=$regexNames;'Runtime.PowerState'='poweredOn'}
@{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','')}}} |
...But result is very strange :
See capture joined
Have an idea ?
Thanks
It looks like you might be missing a pipeline symbol and a Select cmdlet.
Get-View -ViewType VirtualMachine -Filter {'Name'=$regexNames;'Runtime.PowerState'='poweredOn'} |
Select @{N='IPAddress';E={$_.Guest.ipAddress}},
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference