Skip to content

Commit

Permalink
add fns Get-VNVSwitchByVMHostNetworkAdapter, Set-VNVMHostNetworkAdapt…
Browse files Browse the repository at this point in the history
…erVDUplink
  • Loading branch information
mtboren committed Jun 19, 2018
1 parent 5484819 commit e2a39af
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 18 deletions.
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
### ChangeLog for vNugglets.VDNetworking PowerShell module

#### v1.2.0, Jun 2018
- \[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
- \[enhancement] Added `Get-VNVSwitchByVMHostNetworkAdapter` for getting the virtual switch (standard or distributed) with which the given VMHostNetworkAdapter physical NIC is associated, if any

#### v1.1.0, Jan 2018
- \[update] Added `-WhatIf` support to `New-VNVDTrafficRuleAction`, `New-VNVDTrafficRuleQualifier`
- \[bugfix] `-Enabled` parameter on `Set-VNVDTrafficRuleSet` was not working as expected. Fixed
Expand Down
3 changes: 2 additions & 1 deletion ToDoAndNotes.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
## Notes on Traffic Filtering and Marking code
## Notes on vNugglets.VDNetworking PowerShell module


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

### Maybe eventually add/update:
- `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
- `Copy-VNVDTrafficRule -Rule -Ruleset <rulesettowhichtocopy>`
- `Set-VNVDTrafficRule` -- to update a rule, maybe? (like change qualifier/action?)
- `New-VNVDTrafficRule`
Expand Down
6 changes: 3 additions & 3 deletions Update-ThisModuleManifest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ begin {
$hshManifestParams = @{
# Confirm = $true
Path = $strFilespecForPsd1
ModuleVersion = "1.1.0"
ModuleVersion = "1.2.0"
Author = "Matt Boren (@mtboren)"
CompanyName = 'vNugglets for the VMware PowerCLI community'
Copyright = "MIT License"
Description = "Module with functions for managing VMware vSphere Virtual Distributed Networking components like traffic filtering and marking"
# AliasesToExport = @()
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"
FormatsToProcess = "${strModuleName}.format.ps1xml"
FunctionsToExport = Write-Output Get-VNVDTrafficFilterPolicyConfig Get-VNVDTrafficRuleSet Get-VNVDTrafficRule Get-VNVDTrafficRuleQualifier Get-VNVDTrafficRuleAction New-VNVDTrafficRuleQualifier New-VNVDTrafficRuleAction New-VNVDTrafficRule Remove-VNVDTrafficRule Set-VNVDTrafficRuleSet
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
IconUri = "https://avatars0.githubusercontent.com/u/10615837"
LicenseUri = "https://github.com/vNugglets/vNuggletsPSMod_vDNetworking/blob/master/License"
NestedModules = Write-Output GetItems.ps1 NewItems.ps1 RemoveItems.ps1 SetItems.ps1 "${strModuleName}_SupportingFunctions.ps1"
Expand All @@ -33,7 +33,7 @@ begin {
RequiredModules = "VMware.VimAutomation.Vds"
RootModule = "${strModuleName}_ModRoot.psm1"
ScriptsToProcess = "${strModuleName}_init.ps1", "${strModuleName}_ClassDefinition.ps1"
Tags = Write-Output vNugglets VMware vSphere PowerCLI VDPortGroup TrafficFiltering Filter Filtering TrafficMarking Mark Marking VDSwitch
Tags = Write-Output vNugglets VMware vSphere PowerCLI VDPortGroup TrafficFiltering Filter Filtering TrafficMarking Mark Marking VDSwitch Uplink VDUplink VMHostNetworkAdapater VMNIC
# Verbose = $true
} ## end hashtable

Expand Down
44 changes: 43 additions & 1 deletion vNugglets.VDNetworking/GetItems.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,46 @@ function Get-VNVDTrafficRuleAction {
$_.TrafficRule.Action
} ## end foreach-object
} ## end process
} ## end function
} ## end function



function Get-VNVSwitchByVMHostNetworkAdapter {
<# .Description
Get the virtual switch (standard or distributed) with which the given VMHostNetworkAdapter physical NIC is associated, if any.
.Example
Get-VMHost myVMHost0.dom.com | Get-VMHostNetworkAdapter -Name vmnic2 | Get-VNVSwitchByVMHostNetworkAdapter
Get the vSwitch with which VMNIC2 on myVMHost0.dom.com is associated
.Outputs
Virtual standard- or distributed switch with which given physical VMHost network adapter is associated, if any
#>
[CmdletBinding()]
param(
## The VMHostNetworkAdapter (physical NIC) for which to get the vSwitch
[parameter(Mandatory=$true, ValueFromPipeline=$true)][VMware.VimAutomation.Types.Host.NIC.PhysicalNic[]]$VMHostNetworkAdapter
) ## end param

process {
$VMHostNetworkAdapter | Foreach-Object {
$oThisVMHostNetworkAdapter = $_
if ($oAssociatedVSwitch = $oThisVMHostNetworkAdapter.VMHost.ExtensionData.Config.Network.Vswitch, $oThisVMHostNetworkAdapter.VMHost.ExtensionData.Config.Network.ProxySwitch | Foreach-Object {$_} | Where-Object {$_.Pnic -contains $oThisVMHostNetworkAdapter.Id}) {
switch ($oAssociatedVSwitch) {
## vSS
{$_ -is [VMware.Vim.HostVirtualSwitch]} {
$oThisVMHostNetworkAdapter.VMHost | Get-VirtualSwitch -Standard -Name $oAssociatedVSwitch.Name
break
} ## end case
## vDSwitch
{$_ -is [VMware.Vim.HostProxySwitch]} {
$oThisVMHostNetworkAdapter.VMHost | Get-VDSwitch -Name $oAssociatedVSwitch.DvsName
break
} ## end case
default {Write-Warning "vSwitch not of expected type of either [VMware.Vim.HostVirtualSwitch] or [VMware.Vim.HostProxySwitch]. What kind of vSwitch is it? $_"}
} ## end switch
} ## end if
else {Write-Verbose "No vSwitch associated with VMNIC '$($oThisVMHostNetworkAdapter.Name)' found on VMHost '$($oThisVMHostNetworkAdapter.VMHost.Name)'"}
} ## end Foreach-Object
} ## end process
} ## end fn
124 changes: 124 additions & 0 deletions vNugglets.VDNetworking/SetItems.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,127 @@ function Set-VNVDTrafficRuleSet {
} ## end foreach-object
} ## end process
} ## end function


function Set-VNVMHostNetworkAdapterVDUplink {
<# .Description
Set the VDSwitch Uplink for a VMHost physical NIC ("VMNIC") on the VDSwitch of which the VMNIC is already a part
.Example
Get-VMHost myVMHost0.dom.com | Get-VMHostNetworkAdapter -Name vmnic3 | Set-VNVMHostNetworkAdapterVDUplink -UplinkName Uplinks-02
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)
.Example
Set-VNVMHostNetworkAdapterVDUplink -VMHostNetworkAdapter (Get-VMHost myVMHost0.dom.com | Get-VMHostNetworkAdapter -Name vmnic2, vmnic3) -UplinkName Uplinks-01, Uplinks-02
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)
Could then check out the current status like:
Get-VDSwitch myVDSwitch0 | Get-VDPort -Uplink | Where-Object {$_.ProxyHost.Name -eq "myVMHost0.dom.com"} | Select-Object key, ConnectedEntity, ProxyHost, Name | Sort-Object ProxyHost, Name
.Notes
One cannot put two VMNICs from a VMHost in the same vDUplink -- they should go into separate/unique vDUplinks
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)
Function checks that:
- all VMHostNetworkAdapters specified are on same VMHost and same vDSwitch
- all UplinkNames specified are on same vDSwitch
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
.Outputs
VMware.VimAutomation.Vds.Types.V1.VDPort for the Uplink VDPort with which the VMNIC(s) are now affiliated
#>
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact="High")]
[OutputType([VMware.VimAutomation.Vds.Types.V1.VDPort])]
param(
## 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
[parameter(Mandatory=$true, ValueFromPipeline=$true)][VMware.VimAutomation.Types.Host.NIC.PhysicalNic[]]$VMHostNetworkAdapter,

## 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
[parameter(Mandatory=$true)][String[]]$UplinkName
) ## end param

process {
## make sure that the same number of VMNICs and UplinkNames were provided
if (($VMHostNetworkAdapter | Measure-Object).Count -eq ($UplinkName | Measure-Object).Count) {
## the VMHost(s) of these VMNICs
$oTargetVMHost = $VMHostNetworkAdapter.VMHost | Select-Object -Unique
## if the VMNICs are from more than one VMHost
if (($oTargetVMHost | Measure-Object).Count -gt 1) {Write-Error "VMHostNetworkAdapters provided are from more than one VMHost. Specify VMNICs from just a single VMHost"}
## else, the VMNICs are from the same VMHost
else {
## get VDSwitch(es) of which VMNIC is a part (uses another function in this module)
$arrTargetVSwitches = $VMHostNetworkAdapter | Get-VNVSwitchByVMHostNetworkAdapter
## get the unique vSwitches associated with these VMNICs (should be only one vSwitch)
$oTargetVDSwitch = $arrTargetVSwitches | Select-Object -Unique

## if all VMHostNetworkAdapters are associated with vSwitches (num VMNICs is different than num of retrieved vSwitches), and all VMNICs are from same vSwitch
if ((($arrTargetVSwitches | Measure-Object).Count -eq $VMHostNetworkAdapter.Count) -and (($oTargetVDSwitch | Measure-Object).Count -eq 1)) {
## 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.
$oVDSwitchHostMember = $oTargetVDSwitch.ExtensionData.Config.Host | Where-Object {$_.Config.Host.ToString() -eq $oTargetVMHost.Id}
## 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
$arrVDUplinks_thisVDS_thisVMHost = Get-VDPort -Key $oVDSwitchHostMember.UplinkPortKey -VDSwitch $oTargetVDSwitch

## 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)
$arrUplinkNamesNameOnVDSwitch = Compare-Object -ReferenceObject $arrVDUplinks_thisVDS_thisVMHost.Name -DifferenceObject $UplinkName | Where-Object {$_.SideIndicator -eq "=>"}
## if all values of $UplinkName are valid for this VDSwitch
if ($null -eq $arrUplinkNamesNameOnVDSwitch) {
## the TODO of "check that there are any arrVDUplinks_thisVDS_thisVMHost where the VMNIC <--> UplinkName correlation needs changed" would start about here:

## make the messages for ShouldProcess()
$strShouldProcessMsg_target = "vDSwitch '{0}' for VMHost '{1}'" -f $oTargetVDSwitch.Name, $oTargetVMHost.Name
$strShouldProcessMsg_action = "Set VMNIC{0} '{1}' to be in vDUplink{0} '{2}'" -f $(if ($VMHostNetworkAdapter.Count -ne 1) {"s"}), ($VMHostNetworkAdapter.Name -join ", "), ($UplinkName -join ", ")
if ($PSCmdlet.ShouldProcess($strShouldProcessMsg_target, $strShouldProcessMsg_action)) {
## get NetworkSystem for the VMHost for this VMNIC
$viewNetworkSystem_thisVMHost = Get-View -Id $oTargetVMHost.ExtensionData.ConfigManager.NetworkSystem -Property NetworkConfig, NetworkInfo

## the existing PnicSpec objects for this VDSwitchHostMemeber.Config object
$arrExistingPnicSpec = $oVDSwitchHostMember.Config.Backing.PnicSpec | Where-Object {$VMHostNetworkAdapter.Name -notcontains $_.PnicDevice}
$arrNewPnicSpec = $VMHostNetworkAdapter | Foreach-Object -begin {$intI = 0} -process {
## the UplinkName that corresponds positionally in the $UplinkName param to the position in the $VMHostNetworkAdapter param that we currently are
$strUplinkNameToUseForThisVMNic = $UplinkName | Select-Object -First 1 -Skip $intI
New-Object -Type VMware.Vim.DistributedVirtualSwitchHostMemberPnicSpec -Property @{
PnicDevice = $_.Name
UplinkPortKey = ($arrVDUplinks_thisVDS_thisVMHost | Where-Object {$_.Name -eq $strUplinkNameToUseForThisVMNic}).Key
} ## end New-Object
$intI++
} ## end Foreach-Object

## make reconfigSpec to use to UpdateNetworkConfig() on NetworkSystem
$oHostNetworkConfig_toUse = New-Object -Type VMware.Vim.HostNetworkConfig -Property @{
ProxySwitch = @(
New-Object -Type VMware.Vim.HostProxySwitchConfig -Property @{
Uuid = $oTargetVDSwitch.ExtensionData.Uuid
ChangeOperation = [VMware.Vim.HostConfigChangeOperation]::edit
Spec = New-Object -Type VMware.Vim.HostProxySwitchSpec -Property @{
Backing = New-Object -Type VMware.Vim.DistributedVirtualSwitchHostMemberPnicBacking -Property @{
## 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)
PnicSpec = $arrExistingPnicSpec, $arrNewPnicSpec | Where-Object {$null -ne $_} | Foreach-Object {$_}
} ## end New-Object
} ## end New-Object
} ## end New-Object
) ## end array
} ## end New-Object
try {
## do the UpdateNetworkConfig()
$oHostNetworkConfigResult = $viewNetworkSystem_thisVMHost.UpdateNetworkConfig($oHostNetworkConfig_toUse, [VMware.Vim.HostConfigChangeMode]::modify)
## return an object with the VMNIC and vDUplink info for vDUplinks for this VMHost on this vDSwitch
Get-VDPort -Key $oVDSwitchHostMember.UplinkPortKey -VDSwitch $oTargetVDSwitch | Sort-Object ProxyHost, Name
} ## end try
catch {$PSCmdlet.ThrowTerminatingError($_)}
} ## end if ShouldProcess()

## the TODO of "check that there are any arrVDUplinks_thisVDS_thisVMHost where the VMNIC <--> UplinkName correlation needs changed" would end about here
} ## end if all values of $UplinkName are valid for this VDSwitch

else {
$intNumSpecifiedUplinkNamesNotOnThisVSwitch = ($arrUplinkNamesNameOnVDSwitch | Measure-Object).Count
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 ", "))
} ## end else
} ## end if all VMHostNetworkAdapters are associated with vSwitches (num VMNICs is different than num of retrieved vSwitches), and all VMNICs are from same vSwitch

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"}
} ## end else the VMNICs are from the same VMHost
} ## end if same number of VMNICs and UplinkNames were provided
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)}
} ## end process
} ## end fn
21 changes: 8 additions & 13 deletions vNugglets.VDNetworking/vNugglets.VDNetworking.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# Generated by: Matt Boren (@mtboren)
#
# Generated on: 1/10/2018
# Generated on: 6/18/2018
#

@{
Expand All @@ -12,7 +12,7 @@
RootModule = 'vNugglets.VDNetworking_ModRoot.psm1'

# Version number of this module.
ModuleVersion = '1.1.0'
ModuleVersion = '1.2.0'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down Expand Up @@ -76,9 +76,10 @@ NestedModules = @('GetItems.ps1',
# 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.
FunctionsToExport = 'Get-VNVDTrafficFilterPolicyConfig', 'Get-VNVDTrafficRuleSet',
'Get-VNVDTrafficRule', 'Get-VNVDTrafficRuleQualifier',
'Get-VNVDTrafficRuleAction', 'New-VNVDTrafficRuleQualifier',
'New-VNVDTrafficRuleAction', 'New-VNVDTrafficRule',
'Remove-VNVDTrafficRule', 'Set-VNVDTrafficRuleSet'
'Get-VNVDTrafficRuleAction', 'Get-VNVSwitchByVMHostNetworkAdapter',
'New-VNVDTrafficRuleQualifier', 'New-VNVDTrafficRuleAction',
'New-VNVDTrafficRule', 'Remove-VNVDTrafficRule',
'Set-VNVDTrafficRuleSet', 'Set-VNVMHostNetworkAdapterVDUplink'

# 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.
CmdletsToExport = @()
Expand Down Expand Up @@ -111,7 +112,7 @@ PrivateData = @{
PSData = @{

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

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

# Prerelease string of this module
# Prerelease = ''

# Flag to indicate whether the module requires explicit user acceptance for install/update
# RequireLicenseAcceptance = $false

# External dependent modules of this module
# ExternalModuleDependencies = @()
# ExternalModuleDependencies = ''

} # End of PSData hashtable

Expand Down

0 comments on commit e2a39af

Please sign in to comment.