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 {
<# .SYNOPSIS
Disables Sleep on remote PC(s).
.DESCRIPTION
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.
.PARAMETER LogErrors
Specify this switch to create a text log file of the PC(s)
that could not be queried.

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

    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$True,
                    ValueFromPipeline=$True,
                    HelpMessage="Computer name")]
        [string[]]$ComputerName,

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

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

    PROCESS {
        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