ManofManyTigers
ManofManyTigers

Reputation: 63

PowerShell Script Outputting Multiple Times

Below is a script written to ping NAS devices and report if they don't respond. I then want to output the number of days the device has been down to a file and the screen. Since it runs once per day I have it try to find if the device was on the previous days list and if so, pull the value for the number of days on the list, increment it by one, and then write it to today's device failure list list.

It works fine until it gets to the loops at the end. At that point it seems to return/write the output multiple times but I can't tell exactly why. I only want it to output once per each device. I'm currently testing it with a date input of 1 so it should pull that in and change it to 2 then output it.

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++
            }
        }
    }
}

and the output is (as an example):

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

All I would like it to return is:

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

Upvotes: 0

Views: 1250

Answers (1)

Igor
Igor

Reputation: 1445

You want to have one line per each entry in $badnas. Therefore all the output should happen only once per that loop (three times in your example). If you have output once per inner loop, then you get 3x3 outputs.

To resolve it, you need actually to check only if $badnas[$i] exists in $data array. So from you code

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

go to this code

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

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

    }
}

Upvotes: 2

Related Questions