Suspend-Computer (a.k.a. Sleep)

Shutdown.exe hibernates a computer, but there’s no easy way to put a computer to sleep.  One possible reason is that shutdown.exe’s /s, /l, and /e flags are already defined, and /p isn’t the most intuitive mnemonic for’ ‘sleep’.  Maybe /zzzz?

shows the magic to be

[System.Windows.Forms.Application]::SetSuspendState("Suspend", $false, $true);

but I don’t want to put any of my lab machines to sleep. (We have sideband management via iLO, but it’s a pain to use.) I’ve added a simple check against the Win32_OperatingSystem Caption property to be sure it doesn’t include the string ‘ Server ‘.

function Suspend-Computer
    Sleep a computer
        [System.Windows.Forms.Application]::SetSuspendState("Suspend", $false, $true);
    to put the local computer to sleep.  Will not work on Server OS, defined as
        (Get-WmiOnject -Class Win32_OperatingSystem).Caption -match ' Server ';
    if ((Get-WmiObject -Class Win32_OperatingSystem).Caption -match '\sServer\s')
        $msg = "$($MyInvocation.MyCommand.Name) will not work on Server OS";
        Write-Error -ErrorAction SilentlyContinue -Message $msg;
        Write-Warning "$msg";
    } # if ((Get-WmiObject -Class Win32_OperatingSystem).Caption -match '\sServer\s')
        Add-Type -Assembly System.Windows.Forms;
        [System.Windows.Forms.Application]::SetSuspendState("Suspend", $false, $true);
    } # if ((Get-WmiObject -Class Win32_OperatingSystem).Caption -match '\sServer\s')... else 
} # function Suspend-Computer 


Comments (0)