Hey,
I'm having some issues with retrieving multiple pieces of information via powercli. We're managing a couple of vCenters and for easier debuging, i'm creating a webpage that will display certain pieces of information regarding the environments. I'm currently having issues with getting the vCenter hostname with a oneliner via Get-View. So far I've got this:
Get-View -Viewtype VirtualMachine | Select-Object -Property name, @{N='CPU';E={$_.Config.Hardware.numCPU}}, @{N='Memory';E={$_.Config.Hardware.memoryMB}}, @{N='DNSHostname';E={$_.guest.hostname}}, @{N='IP';E={$_.Guest.Net.ipconfig.ipaddress.ipaddress -join '|'}}, @{N='VMHost';E={Get-View -Id $_.Runtime.Host -Property Name | Select -ExpandProperty Name}}
But I'm unable to get the vCenter hostname via ExtensionData (probably because I'm not reaching that property the correct way?).
Also, while we're at it, the
@{N='VMHost';E={Get-View -Id $_.Runtime.Host -Property Name | Select -ExpandProperty Name}}
is also kinda slow. Is there any way to speed this up?
I'm running
PowerCLI Version
----------------
VMware PowerCLI 11.5.0 build 14912921
---------------
Component Versions
---------------
VMware Common PowerCLI Component 11.5 build 14898112
VMware Cis Core PowerCLI Component PowerCLI Component 11.5 build 14898113
VMware VimAutomation VICore Commands PowerCLI Component PowerCLI Component 11.5 build 14899560
Thanks in advance!
Try like this.
Btw, the latest version is 12.0.
Select-Object -Property name,
@{N='CPU';E={$_.Config.Hardware.numCPU}},
@{N='Memory';E={$_.Config.Hardware.memoryMB}},
@{N='DNSHostname';E={$_.guest.hostname}},
@{N='IP';E={$_.Guest.Net.ipconfig.ipaddress.ipaddress -join '|'}},
@{N='VMHost';E={(Get-View -Id $_.Runtime.Host -Property Name).Name}},
@{N='vCenter';E={([uri]$_.Client.ServiceUrl).Host}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try like this.
Btw, the latest version is 12.0.
Select-Object -Property name,
@{N='CPU';E={$_.Config.Hardware.numCPU}},
@{N='Memory';E={$_.Config.Hardware.memoryMB}},
@{N='DNSHostname';E={$_.guest.hostname}},
@{N='IP';E={$_.Guest.Net.ipconfig.ipaddress.ipaddress -join '|'}},
@{N='VMHost';E={(Get-View -Id $_.Runtime.Host -Property Name).Name}},
@{N='vCenter';E={([uri]$_.Client.ServiceUrl).Host}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks for the quick reply! It works like a charm!
I've got one more question though - If i try to get the VM's networks via
Get-View -Viewtype VirtualMachine | Select-Object -Property name, @{Name = 'Network'; Expression = {$_.guest.net.Network}}
I sometimes get results like this
OPC-T {$null, $null, $null}
VM1 {$null, VLAN25%2f24, VLAN25%2f24}
The %2f part is fine as the port group contains slashes and that's how they appear, but why am i getting $null where sometimes, it's the same exact network as the next adapter, and that one's parsed fine?
Thanks!
Are you running that on a Linux box perhaps?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, that's correct. I'm running Powershell 7.0.3 on a CentOS 7.8.2003 VM
This is probably related to an encoding issue.
PS on Linux defaults to UTF-8.
You could try to use for example the Export-Csv with the Encoding parameter to check if that saves the values correctly.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
After trying all of the encoding formats, none of them seem to make a difference. It is strange as for some VM's that have 2 adapters in the same network, the first one might appear as $null but the second one renders just fine
Just tried with PSv7.0.3 on Ubuntu and Photon.
The $null in the output seems to be normal.
I couldn't get the %2f in my output.
Do you have a slash in one of the network names?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The $null isn't expected in my case. For example, this VM has 1 network adapter
but the output shows
VM1 {$null, $null, $null}
Regarding the slashes, i've already mentioned above that the names include slashes and that that is expected in this case. It's just the $null that makes no sense, since some VM's have their network parsed fine, but other VM's (with the exact same network) show $null
I've also now updated pwsh and the powercli modules. I'm now running pwsh 7.0.3 and
Script 7.0.0.159… VMware.Vim
Script 12.0.0.15… VMware.VimAutomation.Cis.Core {Connect-CisServer, Disconnect-CisServer, Get-CisService}
Script 12.0.0.15… VMware.VimAutomation.Common {Get-Task, New-OAuthSecurityContext, Stop-Task, Wait-Task}
Script 12.0.0.15… VMware.VimAutomation.Core {Add-PassthroughDevice, Add-VirtualSwitchPhysicalNetworkAdapter, Add-VMHost, Add-VMHostNtpServer…}
Script 12.0.0.15… VMware.VimAutomation.Sdk {Get-ErrorReport, Get-InstallPath, Get-PSVersion}
Looks to be completely off, since it is showing 3 vNICs while that VM is only showing 1.
Just to make sure, you are looking at the same VM?
What does this return?
Select Name, @{N='NIC#';E={$_.Guest.Net.Count}},
@{N='PGName';E={$_.Guest.Net.Network -join '|'}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, I'm looking at the same VM's via powercli and web ui
the command returns
Name NIC# PGName
---- ---- ------
OPC-T 3 ||
screenshot of the VM:
This looks to be something in the Guest OS or in the interaction with the VMware Tools and the Guest OS.
What does an 'ifconfig' inside the Guest OS return.
And does this, which looks purely at the VM not the Guest OS, return the correct network info?
Select Name,
@{N='PGName';E={(Get-View -Id $_.Network -Property Name).Name -join '|'}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The command returns
Name PGName
---- ------
OPC-T VLAN182_Podpecan-DHCP
So that looks like it's working perfectly so far
Regarding the ifconfig command, it returns
That seems to confirm that from the vSphere side of things all looks alright.
It must be something in the Guest OS or in the interaction with the VMware Tools.
It does recognise there are 3 interfaces, but doesn't seem to be able to determine the portgroup to which the eno* interface is connected.
For the other 2 that is normal.
The virbr0 interface is a switch that you could use if you use virtualisation from the Guest OS.
It can be disabled and removed if you don't intend to do that.
The lo interface is the loopback device.
And you shouldn't remove it.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Guess I'll use the second comand then, even if it's a little slower. Overall, adding the VMHost part and now the portgroup parameter it adds around 1-2min to the running time of the script. It is listing around 1100 VMs though, so i guess it's not that bad. Thanks for all the help!!
Hey,
I'm having a strange issue with the port group parsing part of the code. It seems to sometimes parse port groups from other VM's
The code is
Get-View -Viewtype VirtualMachine | Select-Object -Property name,
@{N='CPU';E={$_.Config.Hardware.numCPU}},
@{N='Memory';E={$_.Config.Hardware.memoryMB}},
@{N='DNSHostname';E={$_.guest.hostname}},
@{N='IP';E={$_.Guest.Net.ipconfig.ipaddress.ipaddress -join '|'}},
@{N='VMHost';E={(Get-View -Id $_.Runtime.Host -Property Name).Name}},
@{N='vCenter';E={([uri]$_.Client.ServiceUrl).Host}},
@{N='PGName';E={(Get-View -Id $_.Network -Property Name).Name -join '|'}},
@{N='PowerState';E={$_.Runtime.PowerState}}
and what it sometimes does is:
The first line has VLAN590|VLAN570, which are not even in the same vCenter, but a different one, that I'm also parsing. The VLAN5 is correct
The second line has VLAN540, 542, which are also not corrent and not in the same vCenter, VLAN174 is correct.
Any ideas on why this is happening?
Did you check $global:DefaultVIServers to make sure you only have 1 connection open?
If there is only one connection open the Network property states "A collection of references to the subset of network objects in the datacenter that is used by this virtual machine."
So I find it hard to believe that a VM would be linked to other portgroups.
Unless that VM had those connections before and they still might be present on Snapshots for that VM.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks for the fast reply!
Huh, the issue might be in how i'm doing this whole thing then. In the beginning of the script, i open all of the connectons to all of the vcenters and start parsing data when they're all connected
foreach ($vi in $viCntInfo)
{
Write-Output $vi.server
$convi = connect-viserver -server $vi.server -username $vi.username -password $vi.password
}
Then I would suggest using the Server parameter on most of the PowerCLI cmdlets.
That way you can limit a cmdlet to only look at that specific vCenter
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference