Quantcast
Channel: TechNet Blogs
Viewing all articles
Browse latest Browse all 36188

Use PowerShell to Set the Power Plan on Networked Servers

$
0
0

Summary: Use a couple of simple Windows PowerShell commands to report the power plan settings on servers as well as setting them.

Microsoft Scripting Guy, Ed Wilson, is here. Well, today the Scripting Wife and I travel to The Netherlands for the sold-out Dutch Windows PowerShell user group meeting. This all-day user group meeting is sort of like a Dutch version of Windows PowerShell Saturday, except it is on Friday and there is a single track. But oh, what track it is.

Richard Siddaway is speaking about Windows PowerShell and WMI via Lync from the UK; and then I am talking about Windows PowerShell remoting; Jaap Brasser is talking about Splatting; and Jeff Wouters is talking about creating Windows PowerShell tools. Yep, it will be an awesome meeting—sold out, and it is their FIRST-EVER Windows PowerShell user group meeting. Way to go.

Note  This is the third article discussing working with Windows PowerShell and WMI to detect and to configure power plans. You should read Use PowerShell and WMI or CIM to View and to Set Power Plans for a backgrounder on the technology. You should then read Use PowerShell to Detect Power State and to Set Power Plan to see how to use WMI to make the changes. The knowledge in both of those articles is assumed in today’s article.

Detect the power plan on all computers running Windows Server 2012

To detect the power plan on all of my computers running Windows Server 2012, I need to find them. The easiest way to do this is to use the Get-ADComputer cmdlet from the Active Directory module. The first thing I do is import the Active Directory module, and then obtain the credentials I will use later. These two lines of code appear here.

Import-Module ActiveDirectory

$cred = Get-Credential Iammred\administrator

Because I am not going to use the OperatingSystem attribute in my script, I do not need to select it via the Propertiesparameter. I only need to use the OperatingSystem attribute in my filter. Here is the command I use.

$cn = Get-ADComputer -Filter "OperatingSystem -like '* 2012 *'"

I use the Nameattribute from the collection of computer objects, and I use the credentials I capture via the Get-Credential cmdlet. I pass these values to the New-CimSession cmdlet, and store the returned CIMSession in the $cim variable. This command is shown here.

$cim = New-CimSession -ComputerName $cn.name -Credential $cred 

Now, I use the Get-CimInstance cmdlet to retrieve the active power plan on all of my servers. I pipe the results to the Format-Table cmdlet to produce a nice output. The command is shown here.

Get-CimInstance -Name root\cimv2\power -Class win32_PowerPlan `

    -Filter "IsActive = 'True'"   -CimSession $cim |

    Format-Table PsComputerName, ElementName

The script and the output from the script are shown here.

Image of command output

The complete script is shown here.

GetServerPowerSaverPlan.ps1

 Import-Module ActiveDirectory

$cred = Get-Credential Iammred\administrator

$cn = Get-ADComputer -Filter "OperatingSystem -like '* 2012 *'"

$cim = New-CimSession -ComputerName $cn.name -Credential $cred

 

Get-CimInstance -Name root\cimv2\power -Class win32_PowerPlan `

    -Filter "IsActive = 'True'"   -CimSession $cim |

    Format-Table PsComputerName, ElementName

Setting the plan on all of the computers running Windows Server 2012

Now I see why my servers appear to be running so slowly following my upgrade—the active power plan is set to Balanced on all of the servers. I think I will change them to High Performance. This works the same way as it did on my laptop the other day—only now I create a CIM session to all of the remote servers. The InputObject must be a single instance of the power plan. Because I am dealing with the same type of servers, I cheat a bit, and index into the collection of power plans and simply use the first one. The script works great, but perhaps a better way would be to pipe the results—but that would slow things down probably. Here is the script.

SetServerPowerSaverPlan.ps1

 Import-Module ActiveDirectory

$cred = Get-Credential Iammred\administrator

$cn = Get-ADComputer -Filter "OperatingSystem -like '* 2012 *'"

$cim = New-CimSession -ComputerName $cn.name -Credential $cred

 

$p = Get-CimInstance -Name root\cimv2\power -Class win32_PowerPlan `

    -Filter "ElementName = 'High performance'"  -CimSession $cim

 Invoke-CimMethod -InputObject $p[0] -MethodName Activate -CimSession $cim 

 

The script returns True for each successful change. This is shown here.

                               ReturnValue PSComputerName                         

                              ----------- --------------                         

                                     True HYPERV2                                

                                     True DC3                                    

                                     True DC2                                    

                                     True DC4                                    

                                     True WEB1                                   

                                     True HYPERV3                                

                                     True WDS1                                   

                                     True SQL1                                   

 

Now, I re-run the GetServerPowerSaverPlan script to ensure the changes worked. As shown in the following image, the change is successful.

Image of command output

Join me tomorrow when I will talk about cool Windows PowerShell stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy


Viewing all articles
Browse latest Browse all 36188

Trending Articles