Skip to content

Commit e2a39af

Browse files
committed
add fns Get-VNVSwitchByVMHostNetworkAdapter, Set-VNVMHostNetworkAdapterVDUplink
1 parent 5484819 commit e2a39af

File tree

6 files changed

+184
-18
lines changed

6 files changed

+184
-18
lines changed

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
### ChangeLog for vNugglets.VDNetworking PowerShell module
22

3+
#### v1.2.0, Jun 2018
4+
- \[enhancement] Added `Set-VNVMHostNetworkAdapterVDUplink` for setting the VDSwitch Uplink for a VMHost physical NIC ("VMNIC") on the VDSwitch of which the VMNIC is already a part
5+
- \[enhancement] Added `Get-VNVSwitchByVMHostNetworkAdapter` for getting the virtual switch (standard or distributed) with which the given VMHostNetworkAdapter physical NIC is associated, if any
6+
37
#### v1.1.0, Jan 2018
48
- \[update] Added `-WhatIf` support to `New-VNVDTrafficRuleAction`, `New-VNVDTrafficRuleQualifier`
59
- \[bugfix] `-Enabled` parameter on `Set-VNVDTrafficRuleSet` was not working as expected. Fixed

ToDoAndNotes.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
## Notes on Traffic Filtering and Marking code
1+
## Notes on vNugglets.VDNetworking PowerShell module
22

33

44
### Need to make:
55
- Tests (partial, currently, written for interactive test, not fully automated)
66

77
### Maybe eventually add/update:
8+
- `Set-VNVMHostNetworkAdapterVDUplink`: add check that, if all specified VMHostNetworkAdapters are already associated with the given UplinkNames, take no action and return a Verbose message to that effect ("already in desired state"); approximate start/finish points for this feature are noted in comments in the code
89
- `Copy-VNVDTrafficRule -Rule -Ruleset <rulesettowhichtocopy>`
910
- `Set-VNVDTrafficRule` -- to update a rule, maybe? (like change qualifier/action?)
1011
- `New-VNVDTrafficRule`

Update-ThisModuleManifest.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ begin {
1515
$hshManifestParams = @{
1616
# Confirm = $true
1717
Path = $strFilespecForPsd1
18-
ModuleVersion = "1.1.0"
18+
ModuleVersion = "1.2.0"
1919
Author = "Matt Boren (@mtboren)"
2020
CompanyName = 'vNugglets for the VMware PowerCLI community'
2121
Copyright = "MIT License"
2222
Description = "Module with functions for managing VMware vSphere Virtual Distributed Networking components like traffic filtering and marking"
2323
# AliasesToExport = @()
2424
FileList = Write-Output "${strModuleName}.psd1" "${strModuleName}_ModRoot.psm1" "en-US\about_${strModuleName}.help.txt" GetItems.ps1 NewItems.ps1 RemoveItems.ps1 SetItems.ps1 "${strModuleName}_SupportingFunctions.ps1" "${strModuleName}.format.ps1xml" "${strModuleName}_init.ps1" "${strModuleName}_ClassDefinition.ps1"
2525
FormatsToProcess = "${strModuleName}.format.ps1xml"
26-
FunctionsToExport = Write-Output Get-VNVDTrafficFilterPolicyConfig Get-VNVDTrafficRuleSet Get-VNVDTrafficRule Get-VNVDTrafficRuleQualifier Get-VNVDTrafficRuleAction New-VNVDTrafficRuleQualifier New-VNVDTrafficRuleAction New-VNVDTrafficRule Remove-VNVDTrafficRule Set-VNVDTrafficRuleSet
26+
FunctionsToExport = Write-Output Get-VNVDTrafficFilterPolicyConfig Get-VNVDTrafficRuleSet Get-VNVDTrafficRule Get-VNVDTrafficRuleQualifier Get-VNVDTrafficRuleAction Get-VNVSwitchByVMHostNetworkAdapter New-VNVDTrafficRuleQualifier New-VNVDTrafficRuleAction New-VNVDTrafficRule Remove-VNVDTrafficRule Set-VNVDTrafficRuleSet Set-VNVMHostNetworkAdapterVDUplink
2727
IconUri = "https://avatars0.githubusercontent.com/u/10615837"
2828
LicenseUri = "https://github.com/vNugglets/vNuggletsPSMod_vDNetworking/blob/master/License"
2929
NestedModules = Write-Output GetItems.ps1 NewItems.ps1 RemoveItems.ps1 SetItems.ps1 "${strModuleName}_SupportingFunctions.ps1"
@@ -33,7 +33,7 @@ begin {
3333
RequiredModules = "VMware.VimAutomation.Vds"
3434
RootModule = "${strModuleName}_ModRoot.psm1"
3535
ScriptsToProcess = "${strModuleName}_init.ps1", "${strModuleName}_ClassDefinition.ps1"
36-
Tags = Write-Output vNugglets VMware vSphere PowerCLI VDPortGroup TrafficFiltering Filter Filtering TrafficMarking Mark Marking VDSwitch
36+
Tags = Write-Output vNugglets VMware vSphere PowerCLI VDPortGroup TrafficFiltering Filter Filtering TrafficMarking Mark Marking VDSwitch Uplink VDUplink VMHostNetworkAdapater VMNIC
3737
# Verbose = $true
3838
} ## end hashtable
3939

vNugglets.VDNetworking/GetItems.ps1

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,46 @@ function Get-VNVDTrafficRuleAction {
201201
$_.TrafficRule.Action
202202
} ## end foreach-object
203203
} ## end process
204-
} ## end function
204+
} ## end function
205+
206+
207+
208+
function Get-VNVSwitchByVMHostNetworkAdapter {
209+
<# .Description
210+
Get the virtual switch (standard or distributed) with which the given VMHostNetworkAdapter physical NIC is associated, if any.
211+
212+
.Example
213+
Get-VMHost myVMHost0.dom.com | Get-VMHostNetworkAdapter -Name vmnic2 | Get-VNVSwitchByVMHostNetworkAdapter
214+
Get the vSwitch with which VMNIC2 on myVMHost0.dom.com is associated
215+
216+
.Outputs
217+
Virtual standard- or distributed switch with which given physical VMHost network adapter is associated, if any
218+
#>
219+
[CmdletBinding()]
220+
param(
221+
## The VMHostNetworkAdapter (physical NIC) for which to get the vSwitch
222+
[parameter(Mandatory=$true, ValueFromPipeline=$true)][VMware.VimAutomation.Types.Host.NIC.PhysicalNic[]]$VMHostNetworkAdapter
223+
) ## end param
224+
225+
process {
226+
$VMHostNetworkAdapter | Foreach-Object {
227+
$oThisVMHostNetworkAdapter = $_
228+
if ($oAssociatedVSwitch = $oThisVMHostNetworkAdapter.VMHost.ExtensionData.Config.Network.Vswitch, $oThisVMHostNetworkAdapter.VMHost.ExtensionData.Config.Network.ProxySwitch | Foreach-Object {$_} | Where-Object {$_.Pnic -contains $oThisVMHostNetworkAdapter.Id}) {
229+
switch ($oAssociatedVSwitch) {
230+
## vSS
231+
{$_ -is [VMware.Vim.HostVirtualSwitch]} {
232+
$oThisVMHostNetworkAdapter.VMHost | Get-VirtualSwitch -Standard -Name $oAssociatedVSwitch.Name
233+
break
234+
} ## end case
235+
## vDSwitch
236+
{$_ -is [VMware.Vim.HostProxySwitch]} {
237+
$oThisVMHostNetworkAdapter.VMHost | Get-VDSwitch -Name $oAssociatedVSwitch.DvsName
238+
break
239+
} ## end case
240+
default {Write-Warning "vSwitch not of expected type of either [VMware.Vim.HostVirtualSwitch] or [VMware.Vim.HostProxySwitch]. What kind of vSwitch is it? $_"}
241+
} ## end switch
242+
} ## end if
243+
else {Write-Verbose "No vSwitch associated with VMNIC '$($oThisVMHostNetworkAdapter.Name)' found on VMHost '$($oThisVMHostNetworkAdapter.VMHost.Name)'"}
244+
} ## end Foreach-Object
245+
} ## end process
246+
} ## end fn

vNugglets.VDNetworking/SetItems.ps1

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,127 @@ function Set-VNVDTrafficRuleSet {
3838
} ## end foreach-object
3939
} ## end process
4040
} ## end function
41+
42+
43+
function Set-VNVMHostNetworkAdapterVDUplink {
44+
<# .Description
45+
Set the VDSwitch Uplink for a VMHost physical NIC ("VMNIC") on the VDSwitch of which the VMNIC is already a part
46+
47+
.Example
48+
Get-VMHost myVMHost0.dom.com | Get-VMHostNetworkAdapter -Name vmnic3 | Set-VNVMHostNetworkAdapterVDUplink -UplinkName Uplinks-02
49+
Set the VMNIC "vminic3" from VMHost myVMHost0.dom.com to be in VDUplink "Uplinks-02" on VDS myVDSwitch0 (the vDSwitch of which VMNIC3 is a part)
50+
51+
.Example
52+
Set-VNVMHostNetworkAdapterVDUplink -VMHostNetworkAdapter (Get-VMHost myVMHost0.dom.com | Get-VMHostNetworkAdapter -Name vmnic2, vmnic3) -UplinkName Uplinks-01, Uplinks-02
53+
Set the VMNICs "vminic2", "vminic3" from VMHost myVMHost0.dom.com to be in VDUplinks "Uplinks-01", "Uplinks-02" on VDS myVDSwitch0 (the vDSwitch of which VMNIC2 and VMNIC3 are a part)
54+
Could then check out the current status like:
55+
Get-VDSwitch myVDSwitch0 | Get-VDPort -Uplink | Where-Object {$_.ProxyHost.Name -eq "myVMHost0.dom.com"} | Select-Object key, ConnectedEntity, ProxyHost, Name | Sort-Object ProxyHost, Name
56+
57+
.Notes
58+
One cannot put two VMNICs from a VMHost in the same vDUplink -- they should go into separate/unique vDUplinks
59+
Requires that VMHostNetworkAdapter(s) are all associated with a single vDSwitch (not VMNICs from multiple vDSwitches) and that the vSwitch type is _Distributed_ (not Standard)
60+
61+
Function checks that:
62+
- all VMHostNetworkAdapters specified are on same VMHost and same vDSwitch
63+
- all UplinkNames specified are on same vDSwitch
64+
65+
The core NetworkSystem and config spec syntax is based on LucD's post (of course) at https://code.vmware.com/forums/2530/vsphere-powercli#576477?start=15&tstart=0
66+
67+
.Outputs
68+
VMware.VimAutomation.Vds.Types.V1.VDPort for the Uplink VDPort with which the VMNIC(s) are now affiliated
69+
#>
70+
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact="High")]
71+
[OutputType([VMware.VimAutomation.Vds.Types.V1.VDPort])]
72+
param(
73+
## The VMHost Network Adapter(s) ("VMNIC") to set in a given vDUplink. If more than one specified, then specify the same number of -UplinkName values, too. The first VMHostNetworkAdapter will be set to the first UplinkName, the second to the second UplinkName, and so on
74+
[parameter(Mandatory=$true, ValueFromPipeline=$true)][VMware.VimAutomation.Types.Host.NIC.PhysicalNic[]]$VMHostNetworkAdapter,
75+
76+
## The name(s) of the vDUplink with which to associate the VMNIC. If more than one specified, then specify the same number of -VMHostNetworkAdapter values, too
77+
[parameter(Mandatory=$true)][String[]]$UplinkName
78+
) ## end param
79+
80+
process {
81+
## make sure that the same number of VMNICs and UplinkNames were provided
82+
if (($VMHostNetworkAdapter | Measure-Object).Count -eq ($UplinkName | Measure-Object).Count) {
83+
## the VMHost(s) of these VMNICs
84+
$oTargetVMHost = $VMHostNetworkAdapter.VMHost | Select-Object -Unique
85+
## if the VMNICs are from more than one VMHost
86+
if (($oTargetVMHost | Measure-Object).Count -gt 1) {Write-Error "VMHostNetworkAdapters provided are from more than one VMHost. Specify VMNICs from just a single VMHost"}
87+
## else, the VMNICs are from the same VMHost
88+
else {
89+
## get VDSwitch(es) of which VMNIC is a part (uses another function in this module)
90+
$arrTargetVSwitches = $VMHostNetworkAdapter | Get-VNVSwitchByVMHostNetworkAdapter
91+
## get the unique vSwitches associated with these VMNICs (should be only one vSwitch)
92+
$oTargetVDSwitch = $arrTargetVSwitches | Select-Object -Unique
93+
94+
## if all VMHostNetworkAdapters are associated with vSwitches (num VMNICs is different than num of retrieved vSwitches), and all VMNICs are from same vSwitch
95+
if ((($arrTargetVSwitches | Measure-Object).Count -eq $VMHostNetworkAdapter.Count) -and (($oTargetVDSwitch | Measure-Object).Count -eq 1)) {
96+
## get the DistributedVirtualSwitchHostMember object for this VMHost and vDSwitch; this object has things like the VDPorts that are the Uplink ports for this VMHost on this vDSwitch, the current PNIC backing info for this VMHost/vDSwitch (if any), etc.
97+
$oVDSwitchHostMember = $oTargetVDSwitch.ExtensionData.Config.Host | Where-Object {$_.Config.Host.ToString() -eq $oTargetVMHost.Id}
98+
## get the vDUplink ports for this VDSwitch and this VMHost -- the <vDSwitch>.ExtensionData.Config.Host objects have subsequent property ".Config.Host" (yes, same property names again) from which to determine the corresponding item by VMHost ID
99+
$arrVDUplinks_thisVDS_thisVMHost = Get-VDPort -Key $oVDSwitchHostMember.UplinkPortKey -VDSwitch $oTargetVDSwitch
100+
101+
## get the UplinkName(s) specified that are not defined on the target vDSwitch (as returned by Compare-Object with a property of SideIndicator with a value of "=>" -- meaning, they were in the DifferenceObject and not the ReferenceObject)
102+
$arrUplinkNamesNameOnVDSwitch = Compare-Object -ReferenceObject $arrVDUplinks_thisVDS_thisVMHost.Name -DifferenceObject $UplinkName | Where-Object {$_.SideIndicator -eq "=>"}
103+
## if all values of $UplinkName are valid for this VDSwitch
104+
if ($null -eq $arrUplinkNamesNameOnVDSwitch) {
105+
## the TODO of "check that there are any arrVDUplinks_thisVDS_thisVMHost where the VMNIC <--> UplinkName correlation needs changed" would start about here:
106+
107+
## make the messages for ShouldProcess()
108+
$strShouldProcessMsg_target = "vDSwitch '{0}' for VMHost '{1}'" -f $oTargetVDSwitch.Name, $oTargetVMHost.Name
109+
$strShouldProcessMsg_action = "Set VMNIC{0} '{1}' to be in vDUplink{0} '{2}'" -f $(if ($VMHostNetworkAdapter.Count -ne 1) {"s"}), ($VMHostNetworkAdapter.Name -join ", "), ($UplinkName -join ", ")
110+
if ($PSCmdlet.ShouldProcess($strShouldProcessMsg_target, $strShouldProcessMsg_action)) {
111+
## get NetworkSystem for the VMHost for this VMNIC
112+
$viewNetworkSystem_thisVMHost = Get-View -Id $oTargetVMHost.ExtensionData.ConfigManager.NetworkSystem -Property NetworkConfig, NetworkInfo
113+
114+
## the existing PnicSpec objects for this VDSwitchHostMemeber.Config object
115+
$arrExistingPnicSpec = $oVDSwitchHostMember.Config.Backing.PnicSpec | Where-Object {$VMHostNetworkAdapter.Name -notcontains $_.PnicDevice}
116+
$arrNewPnicSpec = $VMHostNetworkAdapter | Foreach-Object -begin {$intI = 0} -process {
117+
## the UplinkName that corresponds positionally in the $UplinkName param to the position in the $VMHostNetworkAdapter param that we currently are
118+
$strUplinkNameToUseForThisVMNic = $UplinkName | Select-Object -First 1 -Skip $intI
119+
New-Object -Type VMware.Vim.DistributedVirtualSwitchHostMemberPnicSpec -Property @{
120+
PnicDevice = $_.Name
121+
UplinkPortKey = ($arrVDUplinks_thisVDS_thisVMHost | Where-Object {$_.Name -eq $strUplinkNameToUseForThisVMNic}).Key
122+
} ## end New-Object
123+
$intI++
124+
} ## end Foreach-Object
125+
126+
## make reconfigSpec to use to UpdateNetworkConfig() on NetworkSystem
127+
$oHostNetworkConfig_toUse = New-Object -Type VMware.Vim.HostNetworkConfig -Property @{
128+
ProxySwitch = @(
129+
New-Object -Type VMware.Vim.HostProxySwitchConfig -Property @{
130+
Uuid = $oTargetVDSwitch.ExtensionData.Uuid
131+
ChangeOperation = [VMware.Vim.HostConfigChangeOperation]::edit
132+
Spec = New-Object -Type VMware.Vim.HostProxySwitchSpec -Property @{
133+
Backing = New-Object -Type VMware.Vim.DistributedVirtualSwitchHostMemberPnicBacking -Property @{
134+
## the PnicSpecs from above -- the existing "other" ones for other VMNICs on this vDS for this VMHost, and the new PnicSpec(s) for the VMHostNetworkAdapter(s)
135+
PnicSpec = $arrExistingPnicSpec, $arrNewPnicSpec | Where-Object {$null -ne $_} | Foreach-Object {$_}
136+
} ## end New-Object
137+
} ## end New-Object
138+
} ## end New-Object
139+
) ## end array
140+
} ## end New-Object
141+
try {
142+
## do the UpdateNetworkConfig()
143+
$oHostNetworkConfigResult = $viewNetworkSystem_thisVMHost.UpdateNetworkConfig($oHostNetworkConfig_toUse, [VMware.Vim.HostConfigChangeMode]::modify)
144+
## return an object with the VMNIC and vDUplink info for vDUplinks for this VMHost on this vDSwitch
145+
Get-VDPort -Key $oVDSwitchHostMember.UplinkPortKey -VDSwitch $oTargetVDSwitch | Sort-Object ProxyHost, Name
146+
} ## end try
147+
catch {$PSCmdlet.ThrowTerminatingError($_)}
148+
} ## end if ShouldProcess()
149+
150+
## the TODO of "check that there are any arrVDUplinks_thisVDS_thisVMHost where the VMNIC <--> UplinkName correlation needs changed" would end about here
151+
} ## end if all values of $UplinkName are valid for this VDSwitch
152+
153+
else {
154+
$intNumSpecifiedUplinkNamesNotOnThisVSwitch = ($arrUplinkNamesNameOnVDSwitch | Measure-Object).Count
155+
Write-Error ("Uplink{0} '{1}' {2} not in '{3}' for vDSwitch '{4}', in which '{5}' take part. Please specify only Uplink names that are in use on this vSwitch." -f $(if ($intNumSpecifiedUplinkNamesNotOnThisVSwitch -ne 1) {"s"}), ($arrUplinkNamesNameOnVDSwitch.InputObject -join ", "), $(if ($intNumSpecifiedUplinkNamesNotOnThisVSwitch -ne 1) {"are"} else {"is"}), ($arrVDUplinks_thisVDS_thisVMHost.Name -join ", "), $oTargetVDSwitch.Name, ($VMHostNetworkAdapter.Name -join ", "))
156+
} ## end else
157+
} ## end if all VMHostNetworkAdapters are associated with vSwitches (num VMNICs is different than num of retrieved vSwitches), and all VMNICs are from same vSwitch
158+
159+
else {Write-Error "Either the VMNICs specified are not all associated with a vSwitch, or are not all are associated with the same vSwitch. Please check that all VMNICs are a part of the same vDSwitch"}
160+
} ## end else the VMNICs are from the same VMHost
161+
} ## end if same number of VMNICs and UplinkNames were provided
162+
else {Write-Error ("A different number of VMNICs ({0}) and UplinkNames ({1}) were specified. Please specify the same number of values for -VMHostNetworkAdapter and -UplinkName" -f $VMHostNetworkAdapter.Count, $UplinkName.Count)}
163+
} ## end process
164+
} ## end fn

vNugglets.VDNetworking/vNugglets.VDNetworking.psd1

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
# Generated by: Matt Boren (@mtboren)
55
#
6-
# Generated on: 1/10/2018
6+
# Generated on: 6/18/2018
77
#
88

99
@{
@@ -12,7 +12,7 @@
1212
RootModule = 'vNugglets.VDNetworking_ModRoot.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '1.1.0'
15+
ModuleVersion = '1.2.0'
1616

1717
# Supported PSEditions
1818
# CompatiblePSEditions = @()
@@ -76,9 +76,10 @@ NestedModules = @('GetItems.ps1',
7676
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
7777
FunctionsToExport = 'Get-VNVDTrafficFilterPolicyConfig', 'Get-VNVDTrafficRuleSet',
7878
'Get-VNVDTrafficRule', 'Get-VNVDTrafficRuleQualifier',
79-
'Get-VNVDTrafficRuleAction', 'New-VNVDTrafficRuleQualifier',
80-
'New-VNVDTrafficRuleAction', 'New-VNVDTrafficRule',
81-
'Remove-VNVDTrafficRule', 'Set-VNVDTrafficRuleSet'
79+
'Get-VNVDTrafficRuleAction', 'Get-VNVSwitchByVMHostNetworkAdapter',
80+
'New-VNVDTrafficRuleQualifier', 'New-VNVDTrafficRuleAction',
81+
'New-VNVDTrafficRule', 'Remove-VNVDTrafficRule',
82+
'Set-VNVDTrafficRuleSet', 'Set-VNVMHostNetworkAdapterVDUplink'
8283

8384
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
8485
CmdletsToExport = @()
@@ -111,7 +112,7 @@ PrivateData = @{
111112
PSData = @{
112113

113114
# Tags applied to this module. These help with module discovery in online galleries.
114-
Tags = 'vNugglets','VMware','vSphere','PowerCLI','VDPortGroup','TrafficFiltering','Filter','Filtering','TrafficMarking','Mark','Marking','VDSwitch'
115+
Tags = 'vNugglets','VMware','vSphere','PowerCLI','VDPortGroup','TrafficFiltering','Filter','Filtering','TrafficMarking','Mark','Marking','VDSwitch','Uplink','VDUplink','VMHostNetworkAdapater','VMNIC'
115116

116117
# A URL to the license for this module.
117118
LicenseUri = 'https://github.com/vNugglets/vNuggletsPSMod_vDNetworking/blob/master/License'
@@ -125,14 +126,8 @@ PrivateData = @{
125126
# ReleaseNotes of this module
126127
ReleaseNotes = 'See ReadMe and other docs at https://github.com/vNugglets/vNuggletsPSMod_vDNetworking'
127128

128-
# Prerelease string of this module
129-
# Prerelease = ''
130-
131-
# Flag to indicate whether the module requires explicit user acceptance for install/update
132-
# RequireLicenseAcceptance = $false
133-
134129
# External dependent modules of this module
135-
# ExternalModuleDependencies = @()
130+
# ExternalModuleDependencies = ''
136131

137132
} # End of PSData hashtable
138133

0 commit comments

Comments
 (0)