This was an easy task that I want to share, and the request was to set the power option when the computer will go into sleep state on a list of Windows 8.1 domain computers. Most of the computers on the list were having the default option of 30 minutes for going into sleep state. And this value of 30 minutes has to be changed into 5 hours, but all computers that were having a changed default setting of 30 minutes into Never must not be set. I was using PowerShell with WMI for achieving this task. And here is the script:
$comps =Get-Content "C:\Temp\Scripts\computers.txt"
foreach ($comp in $comps)
{
if (Test-Connection -ComputerName $comp.Trim() -Quiet)
{
#find the active powerplan
$a = (gwmi win32_powerplan -Namespace root\cimv2\power -ComputerName $comp.Trim() -filter { IsActive = 'True' }).instanceid.split("\")[1]
#find the powersetting for sleep option
$b = (gwmi win32_powersetting -Namespace root\cimv2\power -ComputerName $comp.Trim() -Filter { Elementname = 'Sleep After' }).instanceid.split("\")[1]
#get the value for sleep setting on active power plan
$seconds = gwmi win32_powersettingdataindex -Namespace root\cimv2\power -ComputerName $comp.Trim() -Filter "InstanceID like '%$a%ac%$b'"
#check if the setting is Never
if ( $seconds.SettingIndexValue -ne 0 )
{
#set the value to 5 hours
$seconds.SettingIndexValue = 18000
$seconds.Put()
}
else
{
write-host "$comp has sleep option Never"
}
}
}
The script is easy to understand, but anyway here is the overview:
Sweet Dreams :)
$comps =Get-Content "C:\Temp\Scripts\computers.txt"
foreach ($comp in $comps)
{
if (Test-Connection -ComputerName $comp.Trim() -Quiet)
{
#find the active powerplan
$a = (gwmi win32_powerplan -Namespace root\cimv2\power -ComputerName $comp.Trim() -filter { IsActive = 'True' }).instanceid.split("\")[1]
#find the powersetting for sleep option
$b = (gwmi win32_powersetting -Namespace root\cimv2\power -ComputerName $comp.Trim() -Filter { Elementname = 'Sleep After' }).instanceid.split("\")[1]
#get the value for sleep setting on active power plan
$seconds = gwmi win32_powersettingdataindex -Namespace root\cimv2\power -ComputerName $comp.Trim() -Filter "InstanceID like '%$a%ac%$b'"
#check if the setting is Never
if ( $seconds.SettingIndexValue -ne 0 )
{
#set the value to 5 hours
$seconds.SettingIndexValue = 18000
$seconds.Put()
}
else
{
write-host "$comp has sleep option Never"
}
}
}
The script is easy to understand, but anyway here is the overview:
- Getting the list of computers from file and looping from each of the computer and checking if the computer is online.
- In the main part I'm getting the guid for active power plan and guid for sleep option. After that I'm getting the value for sleep option in seconds.
- And in the last part of the script I'm setting the option for going into sleep after 5 hours if the current value is not 0, which is the value for never put computer into sleep state.
Sweet Dreams :)
No comments:
Post a Comment