2017-05-02 1 views
0

以下は、ping NASデバイスに書き込まれ、応答しない場合に報告するスクリプトです。私は、その後、デバイスがファイルと画面にダウンしている日数を出力します。それは1日に1回実行されるので、デバイスが前日のリストにあったかどうかを調べるようにします。リストにある日数の値を1つ増分して今日のデバイス障害に書き出しますリストリスト。複数回出力するPowerShellスクリプト

最後にループに到達するまでうまく動作します。その時点では、出力を何度も返す/書き込むように見えますが、その理由を正確には伝えられません。私はそれが各デバイスごとに一度だけ出力したい。私は現在、1の日付入力でテストしていますので、それをプルして2に変更して出力する必要があります。

Import-Module ActiveDirectory 
$failurepath = "C:\scripts\NAS Ping\failures.txt" 
$naslist = @(Get-ADComputer -Filter {(name -like "*nas0*") -and (name -notlike "*spare*")} | Select-Object name -ExpandProperty Name) 
$failurepath2 = "C:\scripts\NAS Ping\naslist.txt" 
$failurepath3 = "C:\scripts\Nas Ping\previousfailures.txt" 

if (Test-Path $failurepath) { 
    if (Test-Path $failurepath3) { 
     Remove-Item $failurepath3 
    } 
    Rename-Item -Path $failurepath -NewName $failurepath3 
} 
if (test-path $failurepath2) { 
    Remove-Item $failurepath2 
} 

$naslist | Out-File -Force -FilePath "$($failurepath2)" 

foreach ($DPs in $NASlist) { 
    $pingable = test-connection -computername $($DPs) -count 1 -quiet  
    if ($pingable) { 
     $goodPCs += , $($DPs).Substring(0, 4) 
    } else { 
     $secondtest = Test-Connection -ComputerName $($DPs) -Count 4 -Quiet 
     if (!$secondtest) { 
      [array]$badnas += , $($DPs) 
     } 
    } 
} 

if (Test-Path "C:\scripts\Nas Ping\previousfailures.txt") { 
    $data = Get-Content "C:\scripts\Nas Ping\previousfailures.txt" 
} else { 
    $data = "not found" 
} 

for ($i = 0; $i -lt $badnas.Length; ++$i) { 
    if (!($goodPCs -contains $($badnas[$i].Substring(0, 4)))) { 
     for ($j = 0; $j -lt $data.Length; ++$j) { 
      if ($badnas[$i] -eq $data[$j]) { 
       [int]$data[$j + 1] = [convert]::ToInt32($data[$j + 1]) 
       $data[$j + 1]++ 
       $data[$j] | Out-File -Force -FilePath "$($failurepath)" -Append 
       $data[$j + 1] | Out-File -Force -FilePath "$($failurepath)" -Append 
       Write-Host $($data[$j]) is not pingable for the last $($data[$j + 1]) days 
       $j++ 
      } else { 
       $badnas[$i] | Out-File -Force -FilePath "$($failurepath)" -Append 
       '1' | Out-File -Force -FilePath "$($failurepath)" -Append 
       Write-Host $($badnas[$i]) is not pingable for the last 1 days 
       $j++ 
      } 
     } 
    } 
} 

と出力は(例として)である:

 
Saaa-NAS01 is not pingable for the last 2 days 
Saaa-NAS01 is not pingable for the last 1 days 
Saaa-NAS01 is not pingable for the last 1 days 
Sbbb-NAS01 is not pingable for the last 1 days 
Sbbb-NAS01 is not pingable for the last 2 days 
Sbbb-NAS01 is not pingable for the last 1 days 
Sccc-NAS01 is not pingable for the last 1 days 
Sccc-NAS01 is not pingable for the last 1 days 
Sccc-NAS01 is not pingable for the last 2 days 

私はそれを返したいすべてがある:

 
Saaa-NAS01 is not pingable for the last 2 days 
Sbbb-NAS01 is not pingable for the last 2 days 
Sccc-NAS01 is not pingable for the last 2 days 
+0

は、あなただけのcsvファイルで別々の名前と数を保存した場合、あなたは多くのことで、あなたのロジックを削減することができように思えるこのコードにアクセスしてください。そして、毎回カウントを増加/リセットするだけです。 – BenH

+0

私があなたが言っていると信じているのは間違っているかもしれないが、すべてのNASの名前とカウントをCSVに保存し、必要に応じてインクリメント/リセットすることです。その問題は、私たちの組織がすべてのNAS名のリストを持っておらず、絶えず変化しているので、私は毎回ADからそれを取り出し、オンラインかどうかを判断しなければならないということです。 – ManofManyTigers

+0

すでに履歴データをfailures.txtに保存していますが、これは変わりません。それが実行されているADからあなたのcsvを毎回更新/プルーニングするだけです。 – BenH

答えて

2

あなたには、各エントリごとに1行を持つようにしたいです$ badnas。したがって、すべての出力はループごとに1回だけ発生します(例では3回)。内部ループごとに1回出力すると、3x3の出力が得られます。

これを解決するには、実際に$ badnas [$ i]が$ data配列に存在するかどうかだけチェックする必要があります。だからコードから

for ($i = 0; $i -lt $badnas.Length; ++$i) { 
    if (!($goodPCs -contains $($badnas[$i].Substring(0, 4)))) { 
     for ($j = 0; $j -lt $data.Length; ++$j) { 
      if ($badnas[$i] -eq $data[$j]) { 
       . . . 
      } else { 
       . . . 
      } 
     } 
    } 
} 

for ($i = 0; $i -lt $badnas.Length; ++$i) { 
    if (!($goodPCs -contains $($badnas[$i].Substring(0, 4)))) { 

     if ($badnas[$i] -in $data) { 
      . . . 
     } else { 
      . . . 
     } 

    } 
} 
関連する問題