PowerShell脚本输出多次

时间:2017-05-02 21:16:10

标签: powershell for-loop scripting increment

下面是一个用于ping NAS设备的脚本,并报告他们是否做出响应。然后,我想输出设备已关闭到文件和屏幕的天数。因为它每天运行一次,所以我试着找出设备是否在前几天的列表中,如果是,请将列表中的天数值拉出来,将其递增1,然后将其写入今天&#39 ; s设备故障列表清单。

它可以正常工作,直到最后到达循环。此时它似乎多次返回/写入输出,但我无法确切地说出原因。我只希望每个设备输出一次。我目前正在使用日期输入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

1 个答案:

答案 0 :(得分:2)

您希望$ badnas中的每个条目都有一行。因此,每个循环只应发生一次输出(在您的示例中为三次)。如果每个内循环输出一次,那么你得到3x3输出。

要解决此问题,您实际上只需检查$ datanas中是否存在$ 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 {
            . . .
        }

    }
}