Skip to content

Set-HostService

VMware: Configures a host service

#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,
    [string]$ServiceKey,
    [string]$ServiceLabel,
    [ValidateSet("Automatic", "On", "Off")]
    [string]$HostPolicy = "Automatic"
)
Process {
    $vmServer = $null
    try {
        $vmServer = Connect-VIServer -Server $VIServer -Credential $VICredential -ErrorAction Stop
        $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
        $services = Get-VMHostService -Server $vmServer -VMHost $HostName -ErrorAction Stop
        $output = @()
        if (-not [System.String]::IsNullOrWhiteSpace($ServiceKey)) {
            $output += $services | Where-Object { $_.Key -like $ServiceKey }
        }
        if (-not [System.String]::IsNullOrWhiteSpace($ServiceLabel)) {
            $output += $services | Where-Object { $_.Label -like $ServiceLabel }
        }
        if ($output.Count -le 0) { $output = $services }
        $output = $output | Set-VMHostService -Policy $HostPolicy -Confirm:$false -ErrorAction Stop | Select-Object *
        foreach ($item in $output) {
            $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 modify the available services

Key of the service to modify

Label of the service to modify

Activation policy for the host service

An interactive directory of PowerShell scripts.