PowerShell函数Can-Ping不会使用switch返回$ True或$ False

时间:2014-07-11 12:08:51

标签: powershell

我写了一个函数来检查客户端是在线还是离线,这非常有用!但是,当我尝试使用切换-Remember时,它不会根据需要迭代脚本块$PingCheck,因为测试已经完成一次,但它没有返回{{1 }或$true

让函数始终输出$false$true的最佳方法是什么,但不要复制重复的脚本块?

一如既往地感谢您的帮助。

$false

1 个答案:

答案 0 :(得分:0)

我通过创建一个名为$Script:arrayCanPingResult的额外数组并在脚本块$true中处理false$PingResult的输出来找到解决方案。

对于每个感兴趣的人,这里是:

# Function to check if $Server is online
Function Can-Ping ($Server,[switch]$SendMail,[switch]$Remember) {

    $PingResult = {
          # Return $true or $false based on the result from script block $PingCheck
          foreach ($_ in $Script:arrayCanPingResult) { 
                   # Write-Host $_ 
                   if ($Server -eq $($_.Split(",")[0])) {

                   # Write-Host "We will return $($_.Split(",")[1])" -ForegroundColor Green
                    return $($_.Split(",")[1])  
                   } 
          }
    }

    $PingCheck = {

        $Error.Clear()

        if (Test-Connection -ComputerName $Server -BufferSize 16 -Count 1 -ErrorAction 0 -quiet) { # ErrorAction 0 doesn't display error information when a ping is unsuccessful

            Write-Host "$(Get-TimeStamp) $Server > Function Can-Ping: Ping test ok" -ForegroundColor Gray; $Script:arrayCanPingResult+=@("$Server,$true"); return
        } 
        else {
            $Error.Clear()
            Write-Host "$(Get-TimeStamp) $Server > Function Can-Ping: Ping test FAILED" -ForegroundColor Gray

            Write-Host "$(Get-TimeStamp) $Server > Function Can-Ping: Flushing DNS" -ForegroundColor Gray
            ipconfig /flushdns | Out-Null

            Write-Host "$(Get-TimeStamp) $Server > Function Can-Ping: Registering DNS" -ForegroundColor Gray
            ipconfig /registerdns | Out-Null

            Write-Host "$(Get-TimeStamp) $Server > Function Can-Ping: NSLookup for $Server" -ForegroundColor Gray
            nslookup $Server | Out-Null # Surpressing error here is not possible unless using '2> $null', but if we do this, we don't get $true or $false for the function so '| Out-Null' is an obligation
            if (!$?) {
                Write-Host "$(Get-TimeStamp) $Server > Function Can-Ping: NSlookup can't find the hostname, DNS issues or hostname incorrect?" -ForegroundColor Yellow
                # Write-Host $Error -ForegroundColor Red
                if ($SendMail) {
                    Send-Mail "FAILED Ping test" "$(Get-TimeStamp) NSlookup can't find $Server, hostname incorrect or DNS issues?" "$error"
                }
                $script:arrayCanPingError += "ERROR | $(Get-TimeStamp) Ping test failed: NSlookup can't find $Server, hostname incorrect or DNS issues?`n$error"
                $script:HTMLarrayCanPingError += "ERROR | $(Get-TimeStamp) Ping test failed:<br>NSlookup can't find $Server, hostname incorrect or DNS issues?<br>$error<br>"
                $Script:arrayCanPingResult+=@("$Server,$false")
                return
                }
            else {
                Write-Host "$(Get-TimeStamp) $Server > Function Can-Ping: Re-pinging $Server" -ForegroundColor Gray
                if (Test-Connection -ComputerName $Server -BufferSize 16 -Count 1 -ErrorAction 0 -Quiet) {
                   Write-Host "$(Get-TimeStamp) $Server > Function Can-Ping: Ping test ok, problem resolved" -ForegroundColor Gray
                   $Script:arrayCanPingResult+=@("$Server,$true")
                   return
                }
                else {
                      Write-Host "$(Get-TimeStamp) $Server > Function Can-Ping: DNS Resolving is ok but can't connect, server offline?" -ForegroundColor Yellow
                      if ($SendMail) {
                          Send-Mail "FAILED Ping test" "$error" "DNS Resolving is ok but can't connect to $Server, server offline?"
                      } 
                      $script:arrayCanPingError += "ERROR Ping test failed: DNS Resolving is ok but can't connect to $Server, server offline?`n$error"
                      $script:HTMLarrayCanPingError += "ERROR Ping test failed: DNS Resolving is ok but can't connect to $Server, server offline?<br>$error<br>"
                      $Script:arrayCanPingResult+=@("$Server,$false")
                      return
                }
            }
        }
    }

    # Call the scriptblock $PingAction every time, unless the switch $Remember is provided, than we only check each server once
    if ($Remember) {
        Write-Host "$(Get-TimeStamp) $Server > Function Can-Ping: Switch '-Remember' detected" -ForegroundColor Gray
        While ($tmpPingCheckServers -notcontains $Server) { 
                  &$PingCheck
                  $script:tmpPingCheckServers = @($tmpPingCheckServers+$Server) #Script wide variable, othwerwise it stays empty when we leave the function / @ is used to store it as an Array (table) instaed of a string
        }
        &$PingResult
    } 
    else {
          &$PingCheck
          &$PingResult

    }
}