Hi All,
I'm running vSphere v6.7U1 and using Distributed vSwitch in all of my ESXi servers. I need to get some information in the .CSV file with the below column:
VM Name, IPv4, MAC Address, NetworkName, VLanID, NicType, VMHostname
However, the VLanID and the VMHostname is not listed properly or it is still empty with the below script:
Get-VM | Where-Object {($_.Name -like "PRD-SQL*")} | ForEach-Object {
$vm = $_
$NIC = $vm | Get-NetworkAdapter
$portGrp = $vm | Get-VirtualPortGroup
$vm.Guest.Nics |
ForEach-Object {
$mac = $_.MacAddress
$netName = $_.NetworkName
# Port group treatment depends on the switch type used (distributed etc.)
$portGrp | Where-Object { $_.Name -eq $netName } |
ForEach-Object {
if ($_ -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]) {
if ($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchPvlanSpec]) {
$_.ExtensionData.Config.DefaultPortConfig.Vlan.PvlanId
}
elseif ($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchVlanSpec]) {
if ($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId -is [VMware.Vim.NumericRange[]]) {
[string]::Join(',', ($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId | ForEach-Object {"$($_.Start)-$($_.End)"}))
}
else {
$_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId
}
}
}
else {
$_.VlanId
}
} | Set-Variable VLanID
[PSCustomObject] @{
Name = $vm.Name
IP = $_.IPAddress -join ','
MAC = $mac
NetworkName = $netName
VLanID = $VLanID
NicType = $NIC | Where-Object { $_.MacAddress -eq $mac } | Select-Object -Expand Type
VMHostName = $_.VMHostName
}
}
} | Export-CSV C:\VM-Results.csv -NoType
This is my PowerCLI configuration:
PowerCLI Version
----------------
VMware PowerCLI 11.0.0 build 10380590
---------------
Component Versions
---------------
VMware Cis Core PowerCLI Component PowerCLI Component 11.0 build 10335701
VMware VimAutomation VICore Commands PowerCLI Component PowerCLI Component 11.0 build 10336080
VMware VimAutomation Vds Commands PowerCLI Component PowerCLI Component 11.0 build 10336077
VMware VimAutomation Cloud PowerCLI Component PowerCLI Component 11.0 build 10379994
Any help would be greatly appreciated.
Thanks in advance.