How to ignore warning errors?

纵然是瞬间 提交于 2019-12-11 03:53:18

问题


I have the following PowerShell script. It picks up the NetBIOS name of computers within a given IP address. I'm using a pipe so as to dump the results into a text file. The problem is that if an IP address is not available, a warning is printed.

This is the PowerShell script:

function Get-ComputerNameByIP {
param( $IPAddress = $null )
BEGIN {
    $prefBackup = $WarningPreference
    $WarningPreference = 'SilentlyContinue'
}
PROCESS {
    if ($IPAddress -and $_) {
        throw ‘Please use either pipeline or input parameter’
        break
    } elseif ($IPAddress) {
        ([System.Net.Dns]::GetHostbyAddress($IPAddress))
    } 
    } else {
        $IPAddress = Read-Host “Please supply the IP Address”
        [System.Net.Dns]::GetHostbyAddress($IPAddress)
    }
}
END {
    $WarningPreference = $prefBackup
}

This is the error message I wish to ignore:

WARNING: The requested name is valid, but no data of the requested type was found


回答1:


You want to suppress warnings, not errors. Warnings can be silenced completely by setting the $WarningPreference variable to SilentlyContinue:

PS C:\> Write-Warning 'foo'
WARNING: foo
PS C:\> $prefBackup = $WarningPreference
PS C:\> $WarningPreference = 'SilentlyContinue'
PS C:\> Write-Warning 'foo'
PS C:\> $WarningPreference = $prefBackup
PS C:\> Write-Warning 'foo'
WARNING: foo

The setting pertains to the current scope, so if you want to suppress all warnings for your function you'd simply set the preference at the beginning of your function:

function Get-ComputerNameByIP {
    param( $IPAddress = $null )

    BEGIN {
        $WarningPreference = 'SilentlyContinue'
    }

    PROCESS {
        if ($IPAddress -and $_) {
            throw ‘Please use either pipeline or input parameter’
            break
        } elseif ($IPAddress) {
            [System.Net.Dns]::GetHostbyAddress($IPAddress)
        } 
            [System.Net.Dns]::GetHostbyAddress($_)
        } else {
            $IPAddress = Read-Host "Please supply the IP Address"
            [System.Net.Dns]::GetHostbyAddress($IPAddress)
        }
    }

    END {}
}

If you want warnings suppressed for specific statements only, a simpler way is to redirect the warning output stream to $null:

[System.Net.Dns]::GetHostbyAddress($IPAddress) 3>$null

Warning stream redirection is only available in PowerShell v3 and newer, though.




回答2:


$ErrorActionPreference = "silentlyContinue"

This global var controls error output of those commands that provide intermittent (non-terminating) errors and warnings. Your error is of this kind, so set preference to silently continue to suppress these warnings.




回答3:


You could use a try/catch block for something like this. Consider the following example using a proper formed IP address but had no associated record.

try{
    [System.Net.Dns]::GetHostbyAddress("127.0.0.56")
} Catch [System.Management.Automation.MethodInvocationException]{
    Write-Host "Nothing Record Found"
}

When I tested this the error you were seeing was being caught as [System.Management.Automation.MethodInvocationException] so I checked for that specific error type. Based on it's name I'm sure there are other reasons for it to be called. It is possible to just omit that part altogether and it will catch all errors. Since you account for some other possibilities maybe you don't need it.

If that was a concern you could check the text of the $_.Exception.InnerException to see if it matches the error as well. In the above case it contains the text "The requested name is valid, but no data of the requested type was found".

This might be wrong because I am curious as to why your error is prefixed with "WARNING" where mine is not. A little more research on both our parts might be needed.




回答4:


You can trap the error and force PowerShell to do nothing with it, kind of like a Try/Catch but global for the whole script:

TRAP {"" ;continue} 
[System.Net.Dns]::GetHostbyAddress($IPAddress)


来源:https://stackoverflow.com/questions/30709884/how-to-ignore-warning-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!