Skip to content

Set-HyperVVMIntegrationService

Hyper-V: Configures virtual machine integration services

#Requires -Version 5.1
#Requires -Modules Hyper-V

[CmdletBinding()]
Param (
    [string]$ComputerName = "localhost",

    [PSCredential]$Credential,

    [Parameter(Mandatory = $true)]
    [string]$Name,

    [Parameter(Mandatory = $true)]
    [ValidateSet('Guest Service Interface', 'Heartbeat', 'Key-Value Pair Exchange', 'Shutdown', 'Time Synchronization', 'VSS')]
    [string[]]$Service,

    [Parameter(Mandatory = $true)]
    [bool]$Enabled
)

Process {
    try {
        $params = @{
            'ComputerName' = $ComputerName
            'ErrorAction'  = 'Stop'
        }
        if ($Credential) { $params.Add('Credential', $Credential) }

        $vm = Get-VM @params | Where-Object { $_.Name -eq $Name -or $_.Id -eq $Name }

        if (-not $vm) {
            throw "Virtual machine '$Name' not found on '$ComputerName'."
        }

        foreach ($svc in $Service) {
            if ($Enabled) {
                Enable-VMIntegrationService -VM $vm -Name $svc -ErrorAction Stop
            }
            else {
                Disable-VMIntegrationService -VM $vm -Name $svc -ErrorAction Stop
            }
        }

        $results = Get-VMIntegrationService -VM $vm | Where-Object { $Service -contains $_.Name } | Select-Object Name, Enabled
        
        $result = [PSCustomObject]@{
            VMName    = $vm.Name
            Services  = $results
            Action    = "IntegrationServicesUpdated"
            Status    = "Success"
            Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
        }

        Write-Output $result
    }
    catch {
        throw
    }
}

Specifies the name of the Hyper-V host. Defaults to the local machine.

Specifies the credentials to use for the remote connection.

Specifies the name or ID of the virtual machine.

Specifies the integration service(s) to configure.

Specifies whether the specified service(s) should be enabled ($true) or disabled ($false).

An interactive directory of PowerShell scripts.