Const ERROR_EVENT_ID = 9000
Const WARNING_EVENT_ID = 9001
Const INFO_EVENT_ID = 9002
Const POWER_PLAN_EVENT_ID = 9003 'event to trigger the power plan discovery
Const POWER_BUDGET_EVENT_ID = 9004
Const POWER_BUDGET_NOT_ENABLED_EVENT_ID = 9005
Const POWER_BUDGET_NOT_WRITEABLE_EVENT_ID = 9006
Const POWER_BUDGET_NOT_ENABLED_2_EVENT_ID = 9007
'*************************************************************************
' Script Name - Windows Server 2012 Power Consumption Control
' Description
' Changes the active power plan or power budget of Windows Server 2012 computers based on a control method
' (0) TargetComputerName
' (1) ChangePowerBudget
' (2) NewPowerPlanName
' (3) NewPowerBudget
'*************************************************************************
Const SCRIPT_NAME = "WindowsServer8PowerConsumptionControlScript.vbs"
Call Main()
Sub Main()
On Error Resume Next
Dim oAPI, oArgs, objWMIService, powerPlans, msg
Dim TargetComputerName, ChangePowerBudget, NewPowerPlanName, NewPowerBudget
SetLocale("en-us")
Set oAPI = CreateObject("MOM.ScriptAPI")
if Err.Number <> 0 Then
WScript.Quit
End if
If WScript.Arguments.Count <> 4 Then
oAPI.LogScriptEvent SCRIPT_NAME, WARNING_EVENT_ID, EVENT_WARNING, "The number of arguments is wrong."
WScript.Quit
End if
Set objWMIService = GetObject("winmgmts:\\" & TargetComputerName & "\root\CIMV2\power")
If IsEmpty(objWMIService) Then
msg = "Failed to connect to ""\root\CIMV2\power"" WMI namespace."
WScript.Echo msg
oAPI.LogScriptEvent SCRIPT_NAME, WARNING_EVENT_ID, EVENT_WARNING, msg
WScript.Quit
End If
If ChangePowerBudget Then
Call SetPowerBudget(oAPI, objWMIService, NewPowerBudget)
Else
Call SetPowerPlan(oAPI, objWMIService, NewPowerPlanName)
End If
End Sub
Sub SetPowerPlan(ByRef oAPI, ByRef objWMIService, ByVal sNewPowerPlanName)
Dim powerPlans, msg, query
query = "SELECT IsActive FROM Win32_PowerPlan WHERE ElementName = '" & sNewPowerPlanName & "'"
Set powerPlans = objWMIService.ExecQuery(query)
If powerPlans.Count = 1 Then
If powerPlans.ItemIndex(0).IsActive Then
msg = """" & sNewPowerPlanName & """ power plan is already active."
oAPI.LogScriptEvent SCRIPT_NAME, POWER_PLAN_EVENT_ID, EVENT_INFORMATION, msg
Else
On Error Resume Next
powerPlans.ItemIndex(0).Activate
If Err.number <> 0 Then
msg = "Access denied."
WScript.Echo msg
oAPI.LogScriptEvent SCRIPT_NAME, POWER_PLAN_EVENT_ID, EVENT_WARNING, msg
WScript.Quit
End If
msg = """" & sNewPowerPlanName & """ power plan is activated."
oAPI.LogScriptEvent SCRIPT_NAME, POWER_PLAN_EVENT_ID, EVENT_INFORMATION, msg
End If
Else
msg = """" & sNewPowerPlanName & """ power plan is not found."
oAPI.LogScriptEvent SCRIPT_NAME, POWER_PLAN_EVENT_ID, EVENT_WARNING, msg
End If
WScript.Echo msg
End Sub
Sub SetPowerBudget(ByRef oAPI, ByRef objWMIService, ByVal iNewPowerBudget)
Dim objs, msg, query, minBudget, maxBudget
iNewPowerBudget = iNewPowerBudget * 1000
query = "SELECT * FROM Win32_PowerMeter"
Set objs = objWMIService.ExecQuery(query)
If objs.Count = 0 Then
msg = "Power budget cannot be set since there is no Win32_PowerMeter instances."
WScript.Echo msg
oAPI.LogScriptEvent SCRIPT_NAME, POWER_BUDGET_EVENT_ID, EVENT_ERROR, msg
WScript.Quit
End If
For Each meter in objs
'Need to check the budget is enabled before checking it is writeable
If (Not meter.BudgetEnabled) Or IsNull(meter.BudgetEnabled) Then
msg = "The power budget cannot be set since the power budget is not enabled on the target machine."
oAPI.LogScriptEvent SCRIPT_NAME, POWER_BUDGET_NOT_ENABLED_EVENT_ID, EVENT_WARNING, msg
WScript.Echo msg
WScript.Quit
End If
If (Not meter.BudgetWriteable) Or IsNull(meter.BudgetWriteable) Then
msg = "The power budget cannot be set since the power budget is not writeable on the target machine."
WScript.Echo msg
oAPI.LogScriptEvent SCRIPT_NAME, POWER_BUDGET_NOT_WRITEABLE_EVENT_ID, EVENT_WARNING, msg
WScript.Quit
End If
If iNewPowerBudget < meter.MinOperatingBudget Then
msg = "The given budget is lower than the minimum budget (" & meter.MinOperatingBudget / 1000 & " Watts)."
oAPI.LogScriptEvent SCRIPT_NAME, POWER_BUDGET_EVENT_ID, EVENT_WARNING, msg
ElseIf iNewPowerBudget > meter.MaxOperatingBudget Then
msg = "The given budget is greater than the maximum budget (" & meter.MaxOperatingBudget / 1000 & " Watts)."
oAPI.LogScriptEvent SCRIPT_NAME, POWER_BUDGET_EVENT_ID, EVENT_WARNING, msg
Else
msg = "Old power budget is " & meter.ConfiguredBudget / 1000 & " Watts." + vbNewline
meter.ConfiguredBudget = iNewPowerBudget
On Error Resume Next
meter.Put_()
If Err.number <> 0 Then
msg = "Access denied."
WScript.Echo msg
oAPI.LogScriptEvent SCRIPT_NAME, POWER_BUDGET_EVENT_ID, EVENT_WARNING, msg
WScript.Quit
End If
msg = msg + "New power budget is " & meter.ConfiguredBudget / 1000 & " Watts."
oAPI.LogScriptEvent SCRIPT_NAME, POWER_BUDGET_EVENT_ID, EVENT_INFORMATION, msg
End If
WScript.Echo msg
Next
End Sub </Script></ScriptBody>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
</WriteAction>
</MemberModules>
<Composition>
<Node ID="Script"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.CommandOutput</OutputType>
<InputType>System!System.BaseData</InputType>
</WriteActionModuleType>