VMware Cloud Community
stanj
Enthusiast
Enthusiast
Jump to solution

Need VMs and Portgroups and the associated vSwitch's

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

0 Kudos
3 Solutions

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

0 Kudos
29 Replies
LucD
Leadership
Leadership
Jump to solution

Yes, Get-VirtualSwitch and Get-VirtualPortgroup, provided you are talking about VSS and not VDS


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
stanj
Enthusiast
Enthusiast
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

How would you handle a VM that is connected to multiple vSwitches?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
stanj
Enthusiast
Enthusiast
Jump to solution

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 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
stanj
Enthusiast
Enthusiast
Jump to solution

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?

 

 

https://raw.githubusercontent.com/TheSleepyAdmin/Scripts/master/VMware/Network/VMware_PortGroupRepor...

 

stanj_1-1660574233562.png

 

0 Kudos
stanj
Enthusiast
Enthusiast
Jump to solution

I replaced  Get-VDPortgroup with Get-VirtualPortGroup and am now seeing a list,,

I still need to tie in the vSwitch's..

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
stanj
Enthusiast
Enthusiast
Jump to solution

 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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
stanj
Enthusiast
Enthusiast
Jump to solution

Thanks..

Will this work on a standalone ESXi host?

We only need to list the info for one ESXi host

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
stanj
Enthusiast
Enthusiast
Jump to solution

Yes,,there is a vCEnter,, but i only need the info off one ESXi host

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
stanj
Enthusiast
Enthusiast
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You have to replace <MyEsx> with the name of your ESXi node.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
stanj
Enthusiast
Enthusiast
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
stanj
Enthusiast
Enthusiast
Jump to solution

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

0 Kudos