Sunday, 2 December 2018

Disable remote PC sleep settings

The following PowerShell code is designed to disable the sleep settings on any number of remote PC. The tool can ultimately be incorporated as a module so you can call it straight from a PowerShell console.

function Disable-Sleep {
Disables Sleep on remote PC(s).
Disable-Sleep changes the 'standby-timeout' to '0' for
AC (Mains) & DC (Battery) which sets it to 'Never'.
.PARAMETER ComputerName
Name of PC(s) to disable Sleep on.
Specify this switch to create a text log file of the PC(s)
that could not be queried.

When used with -LogErrors, specifies the file path and name
to which the failed PC(s) will be written. Defaults to
current Powershell directory.
Disable-Sleep -ComputerName pc123
Disable-Sleep -ComputerName pc123, pc456 -Verbose

                    HelpMessage="Computer name")]

        [string]$ErrorLog = (Get-Location).Path,

    BEGIN {
        Write-Verbose "Error log will be $ErrorLog"

        Write-Verbose 'Beginning PROCESS block'

        ForEach ($computer in $ComputerName) {

            #Run the below command on the remote PC
            Invoke-Command -ComputerName $computer -ScriptBlock {
                #Disable Sleep for AC (Mains)
                powercfg.exe -change -standby-timeout-ac 0
                Write-Host "Successfully disabled Sleep on $env:computername for AC (Mains)" -ForegroundColor Green

                #Disable Sleep for DC (Battery)
                powercfg.exe -change -standby-timeout-dc 0
                Write-Host "Successfully disabled Sleep on $env:computername for DC (Battery)" -ForegroundColor Green


        Write-Verbose 'End of function'
    END {}

Disable Remote PC Sleep Settings.ps1

No comments:

Post a Comment