#Requires -Version 4.0 <# .SYNOPSIS Quickly creates my default vSphere distributed switch .DESCRIPTION Quickly creates a vSphere distributed switch with a lacp group containing a configurable number of ports and, if needed, some port groups .PARAMETER VIServer Specifies the IP address or the DNS name of the vSphere server to which you want to connect. You can also specify a server by providing its IPv6 address enclosed in square brackets, for example [fe80::56ff:feb0:74bd]. .PARAMETER Credential Specifies a PSCredential object that contains credentials for authenticating with the server. .PARAMETER Datacenter Specifies the name of the datacenter you want to create the vDs within. .PARAMETER VdsLinkDiscoveryProtocol Specifies the discovery protocol type of the vSphere distributed switch that you want to create. This parameter accepts CDP and LLDP values. If you do not set a value for this parameter, the CDP is used. .PARAMETER VdsLinkDiscoveryProtocolOperation Specifies the link discovery protocol operation for the vSphere distributed switch that you want to create. This parameter accepts Advertise, Listen, Both, and Disabled values. If you do not set a value for this parameter, the Both is used. .PARAMETER VdsNumUplinkPorts Specifies the number of uplink ports on the vSphere distributed switch that you want to create. .PARAMETER VdsVersion Specifies the version of the vSphere distributed switch that you want to create. This parameter accepts 4.0, 4.1.0, 5.0.0, 5.1.0, 5.5.0, and 6.0.0 values. You cannot specify a version that is incompatible with the version of the vCenter Server system you are connected to. .PARAMETER VdsName Specifies a name for the new vSphere distributed switch that you want to create. .PARAMETER LagName Specifies the name of the ling aggregation group you want to create. .PARAMETER LagUplinkNum Specifies number of uplinks to be used. .PARAMETER LagMode Specifies the mode for the ling aggregation group that you want to create. This parameter accepts active or passive values. If you do not set a value for this parameter, active is used. .PARAMETER PortGroups Specifies the port groups that you want to be created. Takes an array of hashtables: @( @{'Suffix'='Mgmt';'NumPorts'=4;'VlanId'=702}, @{'Suffix'='ActiveDirectory';'NumPorts'=4;'VlanId'=814} ) The field VlanId might be omitted for a non-trunk or trunk native link. .NOTES Author: kurator | lagerhaus128.ch Revision 0: Initial revision .EXAMPLE Create-VDSwitch -VIServer vc01.fexlab.local -Credential (Get-Credential) -Datacenter 'datacenter01' -VdsLinkDiscoveryProtocol CDP -VdsLinkDiscoveryProtocolOperation Both -VdsNumUplinkPorts 2 -VdsVersion 6.0.0 -VdsName 'vDs3' -LagName 'lag0' -LagUplinkNum 4 -LagMode active -PortGroups @(@{'Suffix'='Mgmt';'NumPorts'=4;'VlanId'=702},@{'Suffix'='ActiveDirectory';'NumPorts'=4;'VlanId'=814}) #> function Create-VDSwitch { param( [Parameter(Mandatory=$true)][string]$VIServer, [Parameter(Mandatory=$true)][System.Management.Automation.PSCredential]$Credential, [Parameter(Mandatory=$true)][string]$Datacenter, [Parameter(Mandatory=$false)][ValidateSet('CDP','LLDP','Unknown')][string]$VdsLinkDiscoveryProtocol = 'CDP', [Parameter(Mandatory=$false)][ValidateSet('Advertise','Both','Disabled','Listen','Unknown')][string]$VdsLinkDiscoveryProtocolOperation = 'Both', [Parameter(Mandatory=$false)][int]$VdsNumUplinkPorts = 2, [Parameter(Mandatory=$false)][ValidateSet('4.0','4.1.0','5.0.0','5.1.0','5.5.0','6.0.0','6.5.0')][string]$VdsVersion = '6.0.0', [Parameter(Mandatory=$false)][string]$VdsName = 'vDSwitch0', [Parameter(Mandatory=$false)][string]$LagName = 'lag0', [Parameter(Mandatory=$false)][int]$LagUplinkNum = 4, [Parameter(Mandatory=$false)][ValidateSet('active','passive')][string]$LagMode = 'active', [Parameter(Mandatory=$false)][array]$PortGroups = @(@{'Suffix'='Mgmt';'NumPorts'=4;'VlanId'=702}) ) Try{ Import-Module VMware.VimAutomation.Core Import-Module VMware.VimAutomation.Vds $VIServerObject = Connect-VIServer -Server $VIServer -Credential $Credential -NotDefault $VDSwitch = New-VDSwitch -LinkDiscoveryProtocol $VdsLinkDiscoveryProtocol -LinkDiscoveryProtocolOperation $VdsLinkDiscoveryProtocolOperation -NumUplinkPorts $VdsNumUplinkPorts -Version $VdsVersion -Name $VdsName -Server $VIServerObject -Location (Get-Datacenter -Name $Datacenter -Server $VIServerObject) $VDSwitch.ExtensionData.EnableNetworkResourceManagement($true) $VDSwitch.ExtensionData.UpdateViewData() $VDSwitch.ExtensionData.ReconfigureDvs((New-Object VMware.Vim.VMwareDVSConfigSpec -Property @{ 'ConfigVersion'=($VDSwitch.ExtensionData.Config.ConfigVersion); 'NetworkResourceControlVersion' = 'version3'; 'LacpApiVersion' ='multipleLag'; })) $VDSwitch.ExtensionData.UpdateDVSLacpGroupConfig((New-Object VMware.Vim.VMwareDvsLacpGroupSpec -Property @{ 'LacpGroupConfig' = New-Object VMware.Vim.VMwareDvsLacpGroupConfig -Property @{ 'Name' = $LagName; 'UplinkNum' = $LagUplinkNum; 'Mode' = $LagMode; }; 'Operation' = 'Add' })) if($PortGroups) { ForEach($PortGroup in $PortGroups) { $VDPortGroup = New-VDPortgroup -VDSwitch $VDSwitch -Name ('{0}-{1}' -f $VDSwitch.Name,$PortGroup.Suffix) -NumPorts $PortGroup.NumPorts -Server $VIServerObject if(-not [string]::IsNullOrEmpty($PortGroup.VlanId)) { $VDPortGroup | Set-VDPortgroup -VlanId $PortGroup.VlanId -Server $VIServerObject | Out-Null } $VDPortGroup | Get-VDUplinkTeamingPolicy | Set-VDUplinkTeamingPolicy -UnusedUplinkPort @('dvUplink1','dvUplink2') -ActiveUplinkPort @($LagName) -LoadBalancingPolicy LoadBalanceIP | Out-Null Remove-Variable VDPortGroup } } Disconnect-VIServer -Server $VIServer -Confirm:$false Remove-Variable VDSwitch,VIServerObject }Catch{ Write-Error $_.Exception.Message } } Export-ModuleMember -Function Create-VDSwitch