Is there a set of commands or a PowerCLI script that can connect to an ESXi 6.0 host and provide all VM names along with associated Portgroups and the associated vSwitch's
thanks
Sure, like this
$esxName = Read-Host -Prompt "Enter the name of the ESX host:"
Get-VMHost -Name $esxName -PipelineVariable esx |
Get-VM -PipelineVariable vm |
Get-NetworkAdapter -PipelineVariable nic |
Select @{N='VMHost';E={$esx.Name}},
@{N='VM';E={$vm.Name}},
@{N='vSwitch';E={(Get-VirtualSwitch -RelatedObject (Get-VirtualPortGroup -Name $_.NetworkName) ).Name}},
@{N='Portgroup';E={$_.NetworkName}},
@{N='Nic';E={$_.Name}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try setting the PowerCLI configuration to ignore certificate warnings.
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Scope User -Confirm:$false
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Did you add the pipeline symbol (|) at the end of last calculated property?
Also, pipe the output of the Connect-VIServer and Disconnect-VIServer to Out-Null
Otherwise, the output of those cmdlets will also end up in the pipeline.
$esxName = Read-Host -Prompt "Enter the name of the ESX host:"
Connect-VIServer -Server <yourVC> | Out-Null
Get-VMHost -Name $esxName -PipelineVariable esx |
Get-VM -PipelineVariable vm |
Get-NetworkAdapter -PipelineVariable nic |
Select @{N='VMHost';E={$esx.Name}},
@{N='VM';E={$vm.Name}},
@{N='vSwitch';E={(Get-VirtualSwitch -RelatedObject (Get-VirtualPortGroup -Name $_.NetworkName) ).Name}},
@{N='Portgroup';E={$_.NetworkName}},
@{N='Nic';E={$_.Name}} |
Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture
Disconnect-VIServer -Server <your-VC> -Confirm:$false | Out-Null
.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, Get-VirtualSwitch and Get-VirtualPortgroup, provided you are talking about VSS and not VDS
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
ok,,
Any examples that can be put into a script and executed from Win10 to produce output similar to?
VM1 , vSwitch1, PortGroupA, PortGroupB, etc
thanks
How would you handle a VM that is connected to multiple vSwitches?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Good point...
Based on a quick look on the host, 95% of the VMs are on a single vSwitch
Possibly for the few that are not, i can annotate the output
I understand, but the issue is that it is not obvious to have a variable number of columns in the output.
Especially if you want to export the results to a CSV file.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Ok, I can use a report for the PortGroups and VMs.
I tried the below script. However, when I enter the account and password, it accepts the creds and just exits an provided no list?
I replaced Get-VDPortgroup with Get-VirtualPortGroup and am now seeing a list,,
I still need to tie in the vSwitch's..
That script you linked to is for VDS, not VSS.
You can't just replace 1 cmdlet and then hope that it works I'm afraid.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
by replacing Get-VDPortgroup with Get-VirtualPortGroup, the script did and produced the output
VMName : RHEL7-TEST-DEMO
PortGroup : Outside-Net
Host : esx-03.test.org
Cluster : Testing
PowerStatus : poweredOff
VMName : win10
PortGroup : Outside-Net
Host : esx-03.test.org
Cluster : Testing
PowerStatus : poweredOff
VMName : win2016-svr
PortGroup : DMZ
Host : esx-03.test.org
Cluster : Testing
PowerStatus : poweredOff
Since you seem to have dropped your original request for output in a specific format, you could also just do
Get-Cluster -PipelineVariable cluster |
Get-VMHost -PipelineVariable esx |
Get-VM -PipelineVariable vm |
Get-NetworkAdapter -PipelineVariable nic |
Select @{N='Cluster';E={$cluster.Name}},
@{N='VMHost';E={$esx.Name}},
@{N='VM';E={$vm.Name}},
@{N='vSwitch';E={(Get-VirtualSwitch -RelatedObject (Get-VirtualPortGroup -Name $_.NetworkName) ).Name}},
@{N='Portgroup';E={$_.NetworkName}},
@{N='Nic';E={$_.Name}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks..
Will this work on a standalone ESXi host?
We only need to list the info for one ESXi host
Is that ESXi connected to a VCSA?
And there obviously will not be a Cluster value
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes,,there is a vCEnter,, but i only need the info off one ESXi host
Then you could do
Get-VMHost -Name <MyEsx> -PipelineVariable esx |
Get-VM -PipelineVariable vm |
Get-NetworkAdapter -PipelineVariable nic |
Select @{N='VMHost';E={$esx.Name}},
@{N='VM';E={$vm.Name}},
@{N='vSwitch';E={(Get-VirtualSwitch -RelatedObject (Get-VirtualPortGroup -Name $_.NetworkName) ).Name}},
@{N='Portgroup';E={$_.NetworkName}},
@{N='Nic';E={$_.Name}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
ok,,
thanks for the update...
I ran the script but now see an error ......
+ Get-VMHost -Name <MyEsx> -PipelineVariable esx |
+ ~
The '<' operator is reserved for future use.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : RedirectionNotSupported
You have to replace <MyEsx> with the name of your ESXi node.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I replaced MyESX with the ESXi node IP and it ran.
Is there a way to prompt or pass in the ESXi IP / node similar to how the cmdlet below will ask for the login and password of the host...
$cred = Get-Credential
Sure, like this
$esxName = Read-Host -Prompt "Enter the name of the ESX host:"
Get-VMHost -Name $esxName -PipelineVariable esx |
Get-VM -PipelineVariable vm |
Get-NetworkAdapter -PipelineVariable nic |
Select @{N='VMHost';E={$esx.Name}},
@{N='VM';E={$vm.Name}},
@{N='vSwitch';E={(Get-VirtualSwitch -RelatedObject (Get-VirtualPortGroup -Name $_.NetworkName) ).Name}},
@{N='Portgroup';E={$_.NetworkName}},
@{N='Nic';E={$_.Name}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Excellent..
I added the connect and disconnect below and it works great!
thanks again for the help...
$esxName = Read-Host -Prompt "Enter the name of the ESX host:"
Connect-VIServer -Server $esxName
Get-VMHost -Name $esxName -PipelineVariable esx |
Get-VM -PipelineVariable vm |
Get-NetworkAdapter -PipelineVariable nic |
Select @{N='VMHost';E={$esx.Name}},
@{N='VM';E={$vm.Name}},
@{N='vSwitch';E={(Get-VirtualSwitch -RelatedObject (Get-VirtualPortGroup -Name $_.NetworkName) ).Name}},
@{N='Portgroup';E={$_.NetworkName}},
@{N='Nic';E={$_.Name}}
Disconnect-VIServer