Functions/Set-VMResolution.ps1


function Set-VMResolution {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [ArgumentCompleter( {
                param ( $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters )
                Get-VM "$wordToComplete*" | Sort-Object Name | Select-Object -ExpandProperty Name | ForEach-Object { "`"$($_)`"" } }
        )]
        [string[]] $VMName,
        [Parameter(Mandatory)] [ValidateSet("800x600", "1024x768", "1440x900", "1750x850", "1920x1200")] [string] $Resolution,
        [Parameter()] [switch] $RestartVM
    )


    $TempRes = $Resolution.Split("x")
    $HoriRes = $TempRes[0]
    $VertRes = $TempRes[1]

    # $VertRes
    # $HoriRes

    $VMs = Get-VM $VMName -ErrorAction 0 -Verbose | Sort-Object Name

    if ($VMs) {
        $VMs | ForEach-Object {

            $VMVideo = Get-VMVideo $_
            # $VMVideo.VerticalResolution; $VertRes; $VMVideo.HorizontalResolution; $HoriRes

            if($VMVideo.VerticalResolution -eq $VertRes -and $VMVideo.HorizontalResolution -eq $HoriRes) {
                Write-Warning "Resolution already set correctly for $($_.Name)"
            } else {
                if ($RestartVM -and ($_.State -ne "Off")) {
                    $RestartNeeded = $true
                } else {
                    $RestartNeeded = $false
                }


                if ($RestartNeeded -eq $true) {
                    Stop-VM $_ -Force -Verbose
                }
                if ($_.State -eq "Off") {
                    Set-VMVideo $_ -ResolutionType Single -HorizontalResolution $HoriRes -VerticalResolution $VertRes -Verbose
                } else {
                    Write-Warning "$($_.Name) is still running"
                }
                if ($RestartNeeded -eq $true) {
                    Start-VM $_ -Verbose
                }
            }

        }
    } else {
        "No such VM found"
    }

}