Skip to content

Set-VirtualSwitch

VMware: Modifies the properties of a virtual switch

#Requires -Version 5.1
#Requires -Modules VMware.VimAutomation.Core
[CmdletBinding()]
Param(
    [Parameter(Mandatory = $true)]
    [string]$VIServer,
    [Parameter(Mandatory = $true)]
    [pscredential]$VICredential,
    [Parameter(Mandatory = $true)]
    [string]$Name,
    [int32]$PortNumber,
    [int32]$Mtu
)
Process {
    try {
        $vmServer = Connect-VIServer -Server $VIServer -Credential $VICredential -ErrorAction Stop
        $vswitch = Get-VirtualSwitch -Server $vmServer -Name $Name -ErrorAction Stop
        if ($PortNumber -gt 0) { $vswitch = Set-VirtualSwitch -VirtualSwitch $vswitch -Server $vmServer -NumPorts $PortNumber -Confirm:$false -ErrorAction Stop | Select-Object * }
        if ($Mtu -gt 0) { $vswitch = Set-VirtualSwitch -VirtualSwitch $vswitch -Server $vmServer -Mtu $Mtu -Confirm:$false -ErrorAction Stop | Select-Object * }
        $result = $vswitch | Select-Object *
        if ($null -ne $result) { $result | Add-Member -NotePropertyName Timestamp -NotePropertyValue (Get-Date -Format "yyyy-MM-dd HH:mm:ss") -PassThru -Force }
    }
    catch { throw }
    finally { if ($null -ne $vmServer) { Disconnect-VIServer -Server $vmServer -Force -Confirm:$false -ErrorAction SilentlyContinue } }
}

IP address or DNS name of the vSphere server

PSCredential object for authenticating with the server

Name of the virtual switch

Number of ports

Maximum transmission unit in bytes

An interactive directory of PowerShell scripts.