2016-07-13 4 views
0

複数のサーバーマシンで "高性能" powerplanを設定する必要があります。私は、サーバー上でこれを1つずつ実行するときにうまく動作している次のスクリプトを持っています。Powershellを使用して複数のサーバーで電源プランを設定する

セットPowerPlan「高パフォーマンス」電源プランを再帰的にすべてのサーバーに設定されているように、このスクリプトにサーバのリストを渡す方法

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 
} 

? 私を助けてください。

答えて

1

コンピュータ名のパラメータを関数に追加し、その内部に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 
} 
0

の下に示唆したようにあなたがスクリプトに変更を加える必要があります:

[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 

そして、複数のサーバをターゲットに、このコマンドを使用します。

Computers.txtのはで区切られたサーバー名を持つことになります
Invoke-Command -FilePath .\Set-PowerPlan.ps1 -ComputerName (Get-Content -Path C:\temp\computers.txt) 

を改行文字。

関連する問題