VMware Cloud Community
JohnMcKeown
Enthusiast
Enthusiast
Jump to solution

What is the best way to combine 2 cmdlets?

Hi, I would like to ask a generic basic question to resolve an issue I am constantly having.

If I have some simple one liners like :

Get-VirtualSwitch  | Select VMHost, Name, @{N="MacChanges";E={if ($_.ExtensionData.Spec.Policy.Security.MacChanges) { "Accept" } Else { "Reject"} }},

But I want to combine the results with something like

Get-VirtualPortGroup | Select Name,vlanid,

I know that the members of the Get-VirtualSwitch are:

ConvertToVersion  Method     T ConvertToVersion[T]()

Equals            Method     bool Equals(System.Object obj)

GetHashCode       Method     int GetHashCode()

GetType           Method     type GetType()

IsConvertableTo   Method     bool IsConvertableTo(type toType)

ToString          Method     string ToString()

ExtensionData     Property   System.Object ExtensionData {get;}

Id                Property   System.String Id {get;}

Key               Property   System.String Key {get;}

Mtu               Property   System.Nullable`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] Mtu {get;}

Name              Property   System.String Name {get;}

Nic               Property   System.String[] Nic {get;}

NumPorts          Property   System.Int32 NumPorts {get;}

NumPortsAvailable Property   System.Int32 NumPortsAvailable {get;}

Uid               Property   System.String Uid {get;}

VMHost            Property   VMware.VimAutomation.ViCore.Types.V1.Inventory.VMHost VMHost {get;}

VMHostId          Property   System.String VMHostId {get;}

VMHostUid         Property   System.String VMHostUid {get;


and the members of Get-VirtualPortGroup are:

ConvertToVersion  Method     T ConvertToVersion[T]()

Equals            Method     bool Equals(System.Object obj)

GetHashCode       Method     int GetHashCode()

GetType           Method     type GetType()

IsConvertableTo   Method     bool IsConvertableTo(type toType)

ToString          Method     string ToString()

ExtensionData     Property   System.Object ExtensionData {get;}

Key               Property   System.String Key {get;}

Name              Property   System.String Name {get;}

Port              Property   VMware.VimAutomation.ViCore.Types.V1.Host.Networking.PortGroupPort[] Port {get;}

Uid               Property   System.String Uid {get;}

VirtualSwitch     Property   VMware.VimAutomation.ViCore.Types.V1.Host.Networking.VirtualSwitchBase VirtualSwitch {get;}

VirtualSwitchId   Property   System.String VirtualSwitchId {get;}

VirtualSwitchName Property   System.String VirtualSwitchName {get;}

VirtualSwitchUid  Property   System.String VirtualSwitchUid {get;}

VLanId            Property   System.Int32 VLanId {get;}

VMHostId          Property   System.String VMHostId {get;}

VMHostUid         Property   System.String VMHostUid {get;}

I dont know if I should just declare what I want to retrieve, or write a foreach.

0 Kudos
1 Solution

Accepted Solutions
mattboren
Expert
Expert
Jump to solution

Hello, -

I am not quite sure what you mean by "declare what I want to retrieve".

As for the foreach[-object], yeah, that's probably a fine way to go.  Something like:

Get-VirtualSwitch | %{$oThisSwitch = $_; Get-VirtualPortGroup -VirtualSwitch $oThisSwitch |
   
select @{n="VMHost"; e={$oThisSwitch.VMHost}},
        @{n
="vSwitchName"; e={$oThisSwitch.Name}},
        @{n
="MacChanges"; e={if ($oThisSwitch.ExtensionData.Spec.Policy.Security.MacChanges) {"Accept"} else {"Reject"} }},
        Name, vlanid
}
## end foreach-object

Though, that's not quite a one-liner that you are just going to whip out when you want the info quickly.  Probably something suitable to put in a function for its periodic use.

View solution in original post

0 Kudos
2 Replies
mattboren
Expert
Expert
Jump to solution

Hello, -

I am not quite sure what you mean by "declare what I want to retrieve".

As for the foreach[-object], yeah, that's probably a fine way to go.  Something like:

Get-VirtualSwitch | %{$oThisSwitch = $_; Get-VirtualPortGroup -VirtualSwitch $oThisSwitch |
   
select @{n="VMHost"; e={$oThisSwitch.VMHost}},
        @{n
="vSwitchName"; e={$oThisSwitch.Name}},
        @{n
="MacChanges"; e={if ($oThisSwitch.ExtensionData.Spec.Policy.Security.MacChanges) {"Accept"} else {"Reject"} }},
        Name, vlanid
}
## end foreach-object

Though, that's not quite a one-liner that you are just going to whip out when you want the info quickly.  Probably something suitable to put in a function for its periodic use.

0 Kudos
JohnMcKeown
Enthusiast
Enthusiast
Jump to solution

Thanks for that Matt,

I have to get better using foreach statements.

0 Kudos