2017-06-21 22 views
1

以下のPowerShellスクリプトを使用して、サーバーの電源プランをハイパフォーマンスモードに設定しています。問題は、サーバー名をテキストファイル(Servers.txt)に渡しても、スクリプトを実行しているサーバーだけに変更が加えられていることです。私はforeachループを使ってサーバーリストを繰り返しましたが、まだ運がありません。私はどこにロジックが不足しているかわからない、誰かがこれを手伝うことができます。前もって感謝します。PowerShellスクリプトが正常に動作しない(foreachループ)

$file = get-content J:\PowerShell\PowerPlan\Servers.txt 
foreach ($args in $file) 
{ 
    write-host "`r`n`r`n`r`nSERVER: " $args 
    Try 
    { 
     gwmi -NS root\cimv2\power -Class win32_PowerPlan -CN $args | select ElementName, IsActive | ft -a 
     #Set power plan to High Performance 
     write-host "`r`n<<<<<Changin the power plan to High Performance mode>>>>>" 
     $HighPerf = powercfg -l | %{if($_.contains("High performance")) {$_.split()[3]}} 
     $CurrPlan = $(powercfg -getactivescheme).split()[3] 
     if ($CurrPlan -ne $HighPerf) {powercfg -setactive $HighPerf} 
     #Validate the change 
     gwmi -NS root\cimv2\power -Class win32_PowerPlan -CN $args | select ElementName, IsActive | ft -a 
    } 
    Catch 
    { 
      Write-Warning -Message "Can't set power plan to high performance, have a look!!" 
    } 
} 

答えて

1

問題がforeachループがすべてのサーバーを反復するために使用されているが、名前が実際の電力構成に使用されることはありませんということです。つまり、

$HighPerf = powercfg -l | %{if($_.contains("High performance")) {$_.split()[3]}} 

は、常にローカルシステム上で実行されます。したがって、リモートサーバー上で電源プランは変更されません。

回避策として、はリモートシステム管理をサポートしていないように、おそらくpsexecまたはPowershellリモート処理があります。

MS Scripting GuysはいつものようにWMI based solutionも持っています。

+0

こんにちはvonPryz、システム名を渡すかもしれないと思いますそれを修正しました。 –

0

ご質問の趣旨を、私は私は、あなたがInvoke-Command Documentation Invoke-Commandコマンドのコマンドの完全なセットを実行してみたいと感謝トン-ComputerName

$file = get-content J:\PowerShell\PowerPlan\Servers.txt 
foreach ($args in $file) 
{ 
invoke-command -computername $args -ScriptBlock { 
write-host "`r`n`r`n`r`nSERVER: " $args 
    Try 
    { 
     gwmi -NS root\cimv2\power -Class win32_PowerPlan -CN $args | select ElementName, IsActive | ft -a 
     #Set power plan to High Performance 
     write-host "`r`n<<<<<Changin the power plan to High Performance mode>>>>>" 
     $HighPerf = powercfg -l | %{if($_.contains("High performance")) {$_.split()[3]}} 
     $CurrPlan = $(powercfg -getactivescheme).split()[3] 
     if ($CurrPlan -ne $HighPerf) {powercfg -setactive $HighPerf} 
     #Validate the change 
     gwmi -NS root\cimv2\power -Class win32_PowerPlan -CN $args | select ElementName, IsActive | ft -a 
    } 
    Catch 
    { 
      Write-Warning -Message "Can't set power plan to high performance, have a look!!" 
    } 

} 

} 
関連する問題