Skip to content

Install-HostPatch

VMware: Updates the specified host with patches

#Requires -Version 5.1
#Requires -Modules VMware.VimAutomation.Core
[CmdletBinding(DefaultParameterSetName = "HostPath")]
Param(
    [Parameter(Mandatory = $true, ParameterSetName = "HostPath")]
    [Parameter(Mandatory = $true, ParameterSetName = "WebPath")]
    [Parameter(Mandatory = $true, ParameterSetName = "LocalPath")]
    [string]$VIServer,
    [Parameter(Mandatory = $true, ParameterSetName = "HostPath")]
    [Parameter(Mandatory = $true, ParameterSetName = "WebPath")]
    [Parameter(Mandatory = $true, ParameterSetName = "LocalPath")]
    [pscredential]$VICredential,
    [Parameter(Mandatory = $true, ParameterSetName = "HostPath")]
    [Parameter(Mandatory = $true, ParameterSetName = "WebPath")]
    [Parameter(Mandatory = $true, ParameterSetName = "LocalPath")]
    [string]$HostName,
    [Parameter(Mandatory = $true, ParameterSetName = "HostPath")]
    [string]$HostPath,
    [Parameter(Mandatory = $true, ParameterSetName = "WebPath")]
    [string]$WebPath,
    [Parameter(Mandatory = $true, ParameterSetName = "LocalPath")]
    [string]$LocalPath,
    [Parameter(ParameterSetName = "LocalPath")]
    [pscredential]$HostCredential,
    [Parameter(ParameterSetName = "LocalPath")]
    [string]$HostUsername,
    [Parameter(ParameterSetName = "LocalPath")]
    [securestring]$HostPassword,
    [Parameter(ParameterSetName = "HostPath")]
    [Parameter(ParameterSetName = "WebPath")]
    [Parameter(ParameterSetName = "LocalPath")]
    [switch]$RunAsync
)
Process {
    $vmServer = $null
    try {
        $vmServer = Connect-VIServer -Server $VIServer -Credential $VICredential -ErrorAction Stop
        $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
        $cmdArgs = @{ ErrorAction = 'Stop'; Server = $vmServer; RunAsync = $RunAsync; Confirm = $false }
        $vmHost = Get-VMHost @cmdArgs -Name $HostName
        $cmdArgs.Add('VMHost', $vmHost)
        if ($PSCmdlet.ParameterSetName -eq 'LocalPath') {
            if ($PSBoundParameters.ContainsKey('HostCredential')) { $cmdArgs.Add('HostCredential', $HostCredential) }
            else {
                if ($PSBoundParameters.ContainsKey('HostUsername')) { $cmdArgs.Add('HostUsername', $HostUsername) }
                if ($PSBoundParameters.ContainsKey('HostPassword')) { $cmdArgs.Add('HostPassword', $HostPassword) }
            }
            $result = Install-VMHostPatch @cmdArgs -LocalPath $LocalPath
        }
        elseif ($PSCmdlet.ParameterSetName -eq 'WebPath') {
            $result = Install-VMHostPatch @cmdArgs -WebPath $WebPath
        }
        else {
            $result = Install-VMHostPatch @cmdArgs -HostPath $HostPath
        }
        if ($null -ne $result) {
            $result | Add-Member -NotePropertyName 'Timestamp' -NotePropertyValue $timestamp -Force
            Write-Output $result
        }
    }
    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 host to update

File path on the ESX/ESXi host to the patches

Web location of the patches

Local file system path to the patches

PSCredential object for authenticating with the host

User name for authenticating with the host

Password for authenticating with the host

Off

Return immediately without waiting for completion

An interactive directory of PowerShell scripts.