Skip to content

Set-HyperVVMCheckpointConfig

Hyper-V: Configures virtual machine checkpoint policies and storage

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

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

    [PSCredential]$Credential,

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

    [ValidateSet('Disabled', 'Standard', 'Production', 'ProductionOnly')]
    [string]$CheckpointType,

    [string]$Path
)

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'."
        }

        $setParams = @{ 'VM' = $vm; 'ErrorAction' = 'Stop' }
        if ($CheckpointType) { $setParams.Add('CheckpointType', $CheckpointType) }
        if ($Path) { 
            if (-not (Test-Path -Path $Path)) {
                New-Item -ItemType Directory -Path $Path -Force | Out-Null
            }
            $setParams.Add('CheckpointFileLocation', $Path) 
        }

        if ($setParams.Count -gt 2) {
            Set-VM @setParams
        }

        $updatedVM = Get-VM -VM $vm
        
        $result = [PSCustomObject]@{
            VMName                 = $updatedVM.Name
            CheckpointType         = $updatedVM.CheckpointType
            CheckpointFileLocation = $updatedVM.CheckpointFileLocation
            Action                 = "CheckpointConfigUpdated"
            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.

Optional. Specifies the type of checkpoints to create (Disabled, Standard, Production, ProductionOnly).

Optional. Specifies the folder path where checkpoint files will be stored.

An interactive directory of PowerShell scripts.