Hi,
I want to list all VM and vmKernel PortGroup Names, IP Addresses, Subnet Masks and VLANs for the first host in a list of clusters:
1. Read in a list of vCenters and associated clusters from a CSV or text file e.g.
<InputFile.csv>
myvCenter1,myCluster1
myvCenter2,myCluster2
myvCenter3,myCluster3
<End of file>
2. Connect to vCenter and output First Cluster Host and output to CSV the following columns:
| vCenterName | ClusterName | VMHostName | vSwitchName | VM/vmKernel PortGroupName | PortGroupType (VM/vmKernel | vmKermel IPAddress (If any) | SubnetMask (If any) | VLAN (if any) |
Currently I can generate the following from Get-VMHostNetworkAdapter:
| VMhost | Name | IPAddress| SubnetMask | PortGroupName |
_________________________________________________
foreach ($vCenter in $vCenters){
Connect-viserver $vCenter
$FirstClusterHost = Get-Cluster $myCluster[n] | Get-VMHost | Select-Object -first 1
Get-VMHost $FirstClusterHost | Get-VMHostNetworkAdapter | select VMhost, Name, IP, SubnetMask, PortGroupName
| Export-CSV C:\Scripts\${vCenter}"_"${myCluster}_"-PortGroup-Inventory".csv -NoTypeInformation -UseCulture
Disconnect-viserver $vCenter
}
__________________________________________________
I can pull the following from Get-VirtualPortGroup:
| vSwitchName | PortGroupType (Virtual Machine) | VLAN |
__________________________________________________
Get-VMHost $FirstClusterHost | Get-VirtualPortGroup | Select Name,VirtualSwitchName,Port,VlanId
__________________________________________________
3. I don't know how to combine the 2 commands to output to the same CSV file
Yup, you're right, there was a flaw in the logic I was using.
In this new version I also added the kind of traffic that runs over a vmkernel.
Try with this one.
Connect-VIServer -Server $row.vCenterName | Out-Null
Get-Cluster -Name $row.ClusterName -PipelineVariable cluster |
ForEach-Object -Process {
Get-VMHost -Location $cluster -PipelineVariable esx | Select -First 1 |
ForEach-Object -Process {
Get-VirtualSwitch -VMHost $esx -PipelineVariable vSwitch |
ForEach-Object -Process {
Get-VirtualPortGroup -VMHost $esx | where{$_.VirtualSwitchName -eq $vSwitch.Name} |
ForEach-Object -Process {
$obj = [ordered]@{
vCenter = ([uri]$global:defaultviserver.ExtensionData.Client.ServiceUrl).Host
Cluster = $cluster.Name
VMhost = $esx.Name
vSwitch = $vSwitch.Name
Portgroup = $_.Name
VLanId = $_.VLanId
IP = 'na'
Mask = 'na'
VMK = 'na'
vMotion = 'na'
FTLogging = 'na'
Management = 'na'
Vsan = 'na'
}
$vmk = Get-VMHostNetworkAdapter -VMHost $esx -VirtualSwitch $vSwitch -PortGroup $_ -VMKernel
if($vmk){
$vmk | % {
$obj['IP'] = $_.IP
$obj['Mask'] = $_.SubnetMask
$obj['VMK'] = $_.Name
$obj['vMotion'] = $_.VMotionEnabled
$obj['FTLogging'] = $_.FaultToleranceLoggingEnabled
$obj['Management'] = $_.ManagementTrafficEnabled
$obj['Vsan'] = $_.VsanTrafficEnabled
New-Object psobject -Property $obj
}
}
else{
New-Object psobject -Property $obj
}
}
}
}
Disconnect-VIServer -Server $row.vCenterName -Confirm:$false
}
$report | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That's one hell of a script Luc - I'm sorry for putting through that amount of coding!
Just one last question - The "vCenter = ([uri]$_.ExtensionData.Client.ServiceUrl).Host" column provides no values for me. Any idea why that could be?
That should have said ([uri]$global:defaultviserver.ExtensionData.Client.ServiceUrl).Host
I corrected above.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks again Luc. If you're ever in Dublin, let me know. I owe you quite a few craft beers at this stage!
Will do.
From my past visits to Ireland I vaguely remember a night in Waterford that involved Paddy & Guinness :smileygrin:
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference