2017-10-28 16 views
1

どこが間違っているのかわからないオンラインに戻ってくるコンピュータをスキップしたいが、オンラインコンピュータとオンラインコンピュータの両方をチェックし続ける。ループForeachのみオフラインコンピュータを繰り返す

第一の更新は...その、オンライン来て、それをスキップして一つだけが出力のscreenhot

g-10 Is coming online...Skipping to offline one's 
192.168.0.1 Is coming online...Skipping to offline one's 
Testing to see if Hero is coming online... 
Hero is Offline. Pausing for 2 seconds. Remaining attempts: 1 
Testing to see if zero is coming online... 
zero is Offline. Pausing for 2 seconds. Remaining attempts: 1 

その罰金ここ件までが、オンラインのコンピュータが再び繰り返されるのオフラインにpingを実行すべきであるということで結構です

g-10 Is coming online...Skipping to offline one's 
    192.168.0.1 Is coming online...Skipping to offline one's 
    Testing to see if Hero is coming online... 
    Hero is Offline. 

はここ

$Comps = GC c:\restarted.txt 
[int]$SleepTimer = "1" #minutes to attempt after 
[int]$SleepSeconds = $SleepTimer * 2 
[int]$Attempts = "2" 
[int]$AttemptsCounter = 0 

Do 
{ 
    $AttemptsCounter++ 
    $RemainingAttempts = ([int]$Attempts - [int]$AttemptsCounter) 
    Foreach($comp in $comps){ 
    $Online = Test-Connection -ComputerName $Comp -Quiet 
    IF($online -eq "True"){ 
    Write-Host "$comp" -BackgroundColor Green -NoNewline 
    Write-Host " Is coming online...Skipping to offline one's" 
    } 

    elseIf ($Online -NE "True") 
    { 
     Write-Host "Testing to see if $Comp is coming online..." 
     Write-Host "$comp" -BackgroundColor Red -NoNewline 
     Write-Host " is Offline" -BackgroundColor Red -ForegroundColor Black -NoNewline 
     If ($AttemptsCounter -eq $Attempts) { 
      Write-Host "." 
     } 
     Else { 
      Write-Host ". Pausing for $SleepSeconds seconds. Remaining attempts: $RemainingAttempts" 
     } 
    } 
} 
    #Check the number of attempts, break out if reached. 
    If ($AttemptsCounter -eq $Attempts) {break} 

    #Delay 
    Start-Sleep -s ($SleepTimer * 60) 
} 
While ($Online -NE "True") 

If ($Online -NE "True") { 
    Write-Host "Maximum number of attempts reached" 
    } 
Else { 
    Write-Host 
    Write-Host "Computer $Comp is " -NoNewline 
    Write-Host "ONLINE" -BackgroundColor Green 
} 

答えて

0

次のコードを試してみてください。

それは基本的にc:\restarted.txtで行ごとに以下のん:

  1. テストコンピュータがオンラインである場合(注:私は-Count 1を追加しました)
  2. それがonlineの場合:いくつかの緑色のテキストとbreak
  3. を印刷します
  4. offlineの場合):赤いテキストを表示し、$ RemainingAttempts変数を待機および減分して、が0またはコンピュータがonline
  5. になるまでやり直す
  6. はそれ$RemainingAttempts変数は0プリントアウトいくつかのより多くのテキスト

    $ComputerNameArray = Get-Content -Path c:\restarted.txt 
    [int]$SleepTimer = "1" #minutes to attempt after 
    [int]$Attempts = "2" 
    $DefaultBackgroundColor = (Get-Host).ui.rawui.BackgroundColor 
    
    foreach($ComputerName in $ComputerNameArray) { 
        $AttemptsCounter = 0 
        $RemainingAttempts = $Attempts - $AttemptsCounter 
    
        Write-Host "Testing to see if ""$ComputerName"" is coming online..." -BackgroundColor $DefaultBackgroundColor 
    
        while($RemainingAttempts -gt 0) { 
         if(Test-Connection -ComputerName $ComputerName -Quiet -Count 1) { 
          Write-Host """$ComputerName""" -BackgroundColor Green -NoNewline 
          Write-Host " Is coming online...Skipping to offline one's" 
          break 
         } else { 
          Write-Host """$ComputerName""" -BackgroundColor Red -NoNewline 
          Write-Host " is Offline" -BackgroundColor Red -ForegroundColor Black -NoNewline 
          Write-Host ". Pausing for $SleepTimer minutes. Remaining attempts: $($RemainingAttempts - 1)" 
          Start-Sleep -Seconds ($SleepTimer * 60) 
          $RemainingAttempts-- 
         } 
        } 
    
        if($RemainingAttempts -eq 0) { 
         Write-Host "Maximum number of attempts reached" -BackgroundColor $DefaultBackgroundColor 
        } 
    } 
    

これが役に立てば幸いです!

コードに関するご質問がある場合はお知らせください。KR ギュンター

0

Test-ConnectionはARを返す私のコードですIPまたはホスト名が生存しているかどうかに応じて$ nullが返されます。

ので、その十分なあなたはこれを行う場合:

IF($online){ 
    // if its alive 
} 
else 
{ 
    // if its not responding 
} 

あなたは「ブール質問の答え」をテストしている場合は、

if(condition) { 
} 
elseif(condition) { 
} 

に持っていけないもう一つ指摘することだそうでありませんelseifが必要です。あなたのケースでは、コンピュータの状態をチェックしている場合、2(はい/いいえ)以上の回答は得られません。したがって、この場合はelseifが冗長です。

if(condition) { 
    // if the condition is true 
} 
else { 
    // if the condition is not true. you can't have the third option 
} 

また、あなたの変数$Onlineが等しくない場合、条件If ($Online -NE "True")がチェックされている場合、正確な文字列-neは大文字小文字を区別しない条件演算子であるため、「真」または「真」。 変数に何かが保持されているかどうかを確認してください。$Null

+0

'Test-Connection -Quiet'は' boolean'を返します。 '-Quiet'がなければ正しいですが、失敗すると配列または' $ null'を返します。 –

0

私はギュンター・シュミッツが既に緑色のチェックを持っている知っているものの

は、私はいくつかの追加の詳細とポインタをフォローアップしたかったです。

Test-Connection-Quietスイッチでは、[Boolean]タイプが返されます。これは単純な真偽です。文字列の値をブール値と比較する際には、テストの枠組みに応じて奇妙な結果を得ることができるので、細心の注意が必要です。例えば:

'false' -eq $true 
$true -eq 'false' 

トップ一つはPowerShellは最初と同じ型に第二オブジェクトに変換されるので、偽、ボトムいずれかに該当等しく等しく、非空の文字列が真です。だから私の強いの推薦は型付きの比較に固執することです。だから私はで始まるだろう最初のコードの変更はされています@pandemicは、すでに述べたように、あなたは単純な比較、elseifためので必要はありません、ただelseをやっている

if ($Online -eq $true) { 

2番目の問題は、テストリストからコンピュータを削除することがないことです。あなたのテストリストの最後のコンピュータが "ダウン"していれば、すべてのコンピュータを再び通過します。リスト内の最後のコンピュータが「上」の場合、テストするコンピュータの数とダウンしたコンピュータの数にかかわらず、do|whileループを終了します。 1つの既知のホストと1つの偽のホストをテストファイルに入れるという単純なテストでこれを確認できます。正常なホストが最初に見つかった場合は、$AttemptsCounterが最大値に達するまでループし続けます。リストをめくると、既知の良いホストをテストした直後に破棄されます。

最初のコードがすべてのコンピュータでループした後、元に戻って再開しました。 Guentherの例では、一度に1台のコンピュータをループし、テストが来るまでチェックします。この中

$comps = Get-Content -path 'c:\restarted.txt' 
[int]$SleepTimer = 1 
[int]$Attempts = 2 
[int]$AttemptsCounter = 0 

do { 
    Write-Host "Testing Network, attempt $($AttemptsCount + 1)" 
    $newComps = @() 
    foreach($comp in $comps) { 
     $online = Test-Connection -Quiet -Count 1 -ComputerName $comp 
     if ($online) { 
      Write-Host "$comp" -BackgroundColor Green -NoNewline 
      Write-Host " Is coming online...Skipping to offline one's" 
     } else { 
      $newComps += $comp 
      Write-Host "$comp" -BackgroundColor Red -NoNewline 
      Write-Host " is Offline" -BackgroundColor Red -ForegroundColor Black 
     } 
    } 
    $comps = $newComps 
    $AttemptsCounter++ 
    Start-Sleep -Seconds ($SleepTimer * 60) 
} 
while (($comps.count -gt 0) -and ($AttemptsCounter -lt $Attempts)) 

if ($comps.Count -gt 0) { 
    Write-Host "Exceeded Attempts Counter" -BackgroundColor Red 
} 

:私はギュンターの例では、あなたが後何であったか、おそらくだったが、あなたはすべてのコンピュータを介して循環したかった場合、その後のみ失敗したことをテストし、私はこのような何かを書きたいと思いますコードでは、コンピュータのリストを取得しており、コンピュータ上で失敗した場合は、一時的なリストに入れて再利用します。

+0

あなたのアイデアもうまくいきました。それは3台のコンピュータがオフラインで、3台がオフラインで、その間にオンラインのものを飛ばしているとします。このスクリプトで遊ぶのは楽しいです –

関連する問題