2017-05-02 2 views
0

다음은 NAS 장치를 핑 (ping)하고 응답하지 않는 경우보고하는 스크립트입니다. 그런 다음 장치가 파일과 화면에 내려간 일 수를 출력하려고합니다. 하루에 한 번 실행되므로 장치가 이전 요일 목록에 있었는지 확인하고 목록에있는 요일의 값을 가져 와서 하나씩 늘린 다음 오늘의 장치 오류에 기록합니다 목록 목록.여러 번 출력하는 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에 내역 데이터를 이미 저장하고 있습니다. 별다른 차이는 없습니다. 광고가 실행될 때마다 csv를 업데이트/정리하십시오. – BenH

답변

2

당신은 각 항목 당 하나 개의 라인을 갖고 싶어 $ badnas. 따라서 모든 출력은 해당 루프 당 한 번만 발생해야합니다 (예제에서 세 번). 내부 루프마다 한 번 출력하면 3x3 출력을 얻습니다.

해결하려면 $ data 배열에 $ badnas [$ i]가 있는지 확인해야합니다. 그래서 코드에서

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 { 
      . . . 
     } 

    } 
} 
관련 문제