Skip to content

Test-HostProfileCompliance

VMware: Tests hosts for profile compliance

#Requires -Version 5.1
#Requires -Modules VMware.VimAutomation.Core
[CmdletBinding(DefaultParameterSetName = "Host")]
Param(
    [Parameter(Mandatory = $true, ParameterSetName = "Host")]
    [Parameter(Mandatory = $true, ParameterSetName = "Profile")]
    [string]$VIServer,
    [Parameter(Mandatory = $true, ParameterSetName = "Host")]
    [Parameter(Mandatory = $true, ParameterSetName = "Profile")]
    [pscredential]$VICredential,
    [Parameter(Mandatory = $true, ParameterSetName = "Profile")]
    [string]$ProfileName,
    [Parameter(Mandatory = $true, ParameterSetName = "Host")]
    [string]$HostName,
    [Parameter(ParameterSetName = "Host")]
    [Parameter(ParameterSetName = "Profile")]
    [switch]$UseCache
)
Process {
    $vmServer = $null
    try {
        $vmServer = Connect-VIServer -Server $VIServer -Credential $VICredential -ErrorAction Stop
        $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
        if ($PSCmdlet.ParameterSetName -eq "Profile") {
            $profile = Get-VMHostProfile -Server $vmServer -Name $ProfileName -ErrorAction Stop
            $result = Test-VMHostProfileCompliance -Server $vmServer -Profile $profile -UseCache:$UseCache -ErrorAction Stop | Select-Object *
        }
        else {
            $vmHost = Get-VMHost -Server $vmServer -Name $HostName -ErrorAction Stop
            $result = Test-VMHostProfileCompliance -Server $vmServer -VMHost $vmHost -UseCache:$UseCache -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 profile against which to test compliance

Host to test for profile compliance

Off

Use cached vCenter Server information

An interactive directory of PowerShell scripts.