Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore warning errors?

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

like image 313
Obby Avatar asked Dec 03 '22 16:12

Obby


2 Answers

You can use common parameter -WarningAction:SilentlyContinue with the command that generates warning. It's better than separately overriding $WarningPreference before executing the command and reverting it back afterwards as was suggested above - this parameter basically does that for you.

The WarningAction parameter overrides the value of the $WarningPreference variable for the current command. Because the default value of the $WarningPreference variable is Continue, warnings are displayed and execution continues unless you use the WarningAction parameter.

See more here.

like image 140
miguello Avatar answered Dec 06 '22 20:12

miguello


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.

like image 44
Ansgar Wiechers Avatar answered Dec 06 '22 20:12

Ansgar Wiechers