Skip to content

New-VirtualSwitch

VMware: Creates a new 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]$VMHost,
    [Parameter(Mandatory = $true)]
    [string]$Name,
    [int32]$PortNumber
)
Process {
    try {
        $vmServer = Connect-VIServer -Server $VIServer -Credential $VICredential -ErrorAction Stop
        $cmdArgs = @{ ErrorAction = 'Stop'; Server = $vmServer; Name = $Name; VMHost = $VMHost; Confirm = $false }
        if ($PortNumber -gt 0) { $cmdArgs.Add('NumPorts', $PortNumber) }
        $result = New-VirtualSwitch @cmdArgs | 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

Host on which to create the virtual switch

Name for the new virtual switch

Number of ports for the switch

An interactive directory of PowerShell scripts.