Skip to content

New-HostRoute

VMware: Creates a new route in the routing table of a host

#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]$HostName,
    [Parameter(Mandatory = $true)]
    [string]$Destination,
    [Parameter(Mandatory = $true)]
    [string]$Gateway,
    [Parameter(Mandatory = $true)]
    [int32]$PrefixLength
)
Process {
    $vmServer = $null
    try {
        $vmServer = Connect-VIServer -Server $VIServer -Credential $VICredential -ErrorAction Stop
        $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
        $result = New-VMHostRoute -VMHost $HostName -Destination $Destination -PrefixLength $PrefixLength -Gateway $Gateway -ErrorAction Stop | Select-Object *
        foreach ($item in $result) {
            $item | Add-Member -NotePropertyName 'Timestamp' -NotePropertyValue $timestamp -Force
            Write-Output $item
        }
    }
    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 for which to create a route

Destination IP address for the new route

Gateway IP address for the new route

Prefix length of the destination IP address

An interactive directory of PowerShell scripts.