使用 Powershell 在多台服务器上设置电源计划
Setting Power Plan on multiple server using Powershell
我想在多台服务器上设置 "High Performance" 电源计划。我有以下脚本,当我在服务器上一个一个地执行它时,它 运行 很好。
Set-PowerPlan "High performance"
function Set-PowerPlan {
[CmdletBinding(SupportsShouldProcess = $True)]
param (
[ValidateSet("High performance", "Balanced", "Power saver")]
[ValidateNotNullOrEmpty()]
[string] $PreferredPlan = "High Performance"
)
Write-Host "Setting power plan to `"$PreferredPlan`""
$guid = (Get-WmiObject -Class Win32_PowerPlan -Namespace root\cimv2\power -Filter "ElementName='$PreferredPlan'").InstanceID.ToString()
$regex = [regex]"{(.*?)}$"
$plan = $regex.Match($guid).groups[1].value
powercfg -S $plan
$Output = "Power plan set to "
$Output += "`"" + ((Get-WmiObject -Class Win32_PowerPlan -Namespace root\cimv2\power -Filter "IsActive='$True'").ElementName) + "`""
Write-Host $Output
}
如何将服务器列表传递给此脚本,以便递归地在所有服务器上设置电源计划?
您必须按照以下建议对脚本进行更改:
[CmdletBinding(SupportsShouldProcess = $True)]
param (
[ValidateSet("High performance", "Balanced", "Power saver")]
[ValidateNotNullOrEmpty()]
[string] $PreferredPlan = "High Performance"
)
Write-Host "Setting power plan to `"$PreferredPlan`""
$guid = (Get-WmiObject -Class Win32_PowerPlan -Namespace root\cimv2\power -Filter "ElementName='$PreferredPlan'").InstanceID.ToString()
$regex = [regex]"{(.*?)}$"
$plan = $regex.Match($guid).groups[1].value
powercfg -S $plan
$Output = "Power plan set to "
$Output += "`"" + ((Get-WmiObject -Class Win32_PowerPlan -Namespace root\cimv2\power -Filter "IsActive='$True'").ElementName) + "`""
Write-Host $Output
并使用此命令定位多个服务器:
Invoke-Command -FilePath .\Set-PowerPlan.ps1 -ComputerName (Get-Content -Path C:\temp\computers.txt)
其中 Computers.txt 将使用换行符分隔服务器名称。
只需将 computername 参数添加到您的函数中,并在其中添加 Get-WmiObject
。我还使用 this solution 远程启动进程。
function Set-PowerPlan {
[CmdletBinding(SupportsShouldProcess = $True)]
param (
[ValidateSet("High performance", "Balanced", "Power saver")]
[ValidateNotNullOrEmpty()]
[string]$PreferredPlan = "High Performance",
[string]$ComputerName = $env:COMPUTERNAME
)
Write-Host "Setting power plan to `"$PreferredPlan`""
$guid = (Get-WmiObject -Class Win32_PowerPlan -Namespace root\cimv2\power -Filter "ElementName='$PreferredPlan'" -ComputerName $ComputerName).InstanceID.ToString()
$regex = [regex]"{(.*?)}$"
$plan = $regex.Match($guid).groups[1].value
#powercfg -S $plan
$process = Get-WmiObject -Query "SELECT * FROM Meta_Class WHERE __Class = 'Win32_Process'" -Namespace "root\cimv2" -ComputerName $ComputerName
$results = $process.Create("powercfg -S $plan")
$Output = "Power plan set to "
$Output += "`"" + ((Get-WmiObject -Class Win32_PowerPlan -Namespace root\cimv2\power -Filter "IsActive='$True'" -ComputerName $ComputerName).ElementName) + "`""
Write-Host $Output
}
$ServerList = @(
'CompName1',
'Compname2'
)
foreach ($server in $ServerList ) {
Set-PowerPlan -PreferredPlan 'High performance' -ComputerName $comp
}
我想在多台服务器上设置 "High Performance" 电源计划。我有以下脚本,当我在服务器上一个一个地执行它时,它 运行 很好。
Set-PowerPlan "High performance"
function Set-PowerPlan {
[CmdletBinding(SupportsShouldProcess = $True)]
param (
[ValidateSet("High performance", "Balanced", "Power saver")]
[ValidateNotNullOrEmpty()]
[string] $PreferredPlan = "High Performance"
)
Write-Host "Setting power plan to `"$PreferredPlan`""
$guid = (Get-WmiObject -Class Win32_PowerPlan -Namespace root\cimv2\power -Filter "ElementName='$PreferredPlan'").InstanceID.ToString()
$regex = [regex]"{(.*?)}$"
$plan = $regex.Match($guid).groups[1].value
powercfg -S $plan
$Output = "Power plan set to "
$Output += "`"" + ((Get-WmiObject -Class Win32_PowerPlan -Namespace root\cimv2\power -Filter "IsActive='$True'").ElementName) + "`""
Write-Host $Output
}
如何将服务器列表传递给此脚本,以便递归地在所有服务器上设置电源计划?
您必须按照以下建议对脚本进行更改:
[CmdletBinding(SupportsShouldProcess = $True)]
param (
[ValidateSet("High performance", "Balanced", "Power saver")]
[ValidateNotNullOrEmpty()]
[string] $PreferredPlan = "High Performance"
)
Write-Host "Setting power plan to `"$PreferredPlan`""
$guid = (Get-WmiObject -Class Win32_PowerPlan -Namespace root\cimv2\power -Filter "ElementName='$PreferredPlan'").InstanceID.ToString()
$regex = [regex]"{(.*?)}$"
$plan = $regex.Match($guid).groups[1].value
powercfg -S $plan
$Output = "Power plan set to "
$Output += "`"" + ((Get-WmiObject -Class Win32_PowerPlan -Namespace root\cimv2\power -Filter "IsActive='$True'").ElementName) + "`""
Write-Host $Output
并使用此命令定位多个服务器:
Invoke-Command -FilePath .\Set-PowerPlan.ps1 -ComputerName (Get-Content -Path C:\temp\computers.txt)
其中 Computers.txt 将使用换行符分隔服务器名称。
只需将 computername 参数添加到您的函数中,并在其中添加 Get-WmiObject
。我还使用 this solution 远程启动进程。
function Set-PowerPlan {
[CmdletBinding(SupportsShouldProcess = $True)]
param (
[ValidateSet("High performance", "Balanced", "Power saver")]
[ValidateNotNullOrEmpty()]
[string]$PreferredPlan = "High Performance",
[string]$ComputerName = $env:COMPUTERNAME
)
Write-Host "Setting power plan to `"$PreferredPlan`""
$guid = (Get-WmiObject -Class Win32_PowerPlan -Namespace root\cimv2\power -Filter "ElementName='$PreferredPlan'" -ComputerName $ComputerName).InstanceID.ToString()
$regex = [regex]"{(.*?)}$"
$plan = $regex.Match($guid).groups[1].value
#powercfg -S $plan
$process = Get-WmiObject -Query "SELECT * FROM Meta_Class WHERE __Class = 'Win32_Process'" -Namespace "root\cimv2" -ComputerName $ComputerName
$results = $process.Create("powercfg -S $plan")
$Output = "Power plan set to "
$Output += "`"" + ((Get-WmiObject -Class Win32_PowerPlan -Namespace root\cimv2\power -Filter "IsActive='$True'" -ComputerName $ComputerName).ElementName) + "`""
Write-Host $Output
}
$ServerList = @(
'CompName1',
'Compname2'
)
foreach ($server in $ServerList ) {
Set-PowerPlan -PreferredPlan 'High performance' -ComputerName $comp
}