Understand ErrorAction

Hi,

I try to understand something with ErrorAction. In a script, I tried this line :

[int]$numb = Read-Host "Please choose the number of the destination OU :" -ErrorAction SilentlyContinue

I want to disable the error message because I create my own error message, but I don’t know why,the SilentlyContinue does not work. I still have the error message.

So I bypass it with that :

 $ErrorActionPreference = "SilentlyContinue"
        [int]$numb = Read-Host "Please choose the number of the destination OU :"
        $ErrorActionPreference = "Continue"
        if ((!$numb) -or ($numb -gt $arrayou.LongLength))
        {
            $earry1 = $arrayou.Length -1
            Write-Error "YOU ENTER A UNVALID NUMBER. ENTER A NUMBER BEWTWEEN 0 and $earry1"
        }
        else
        {
            Get-ADComputer $computer | Move-ADObject -TargetPath $listou[$numb]    
        }

But I don’t understand why that’s not working with the first line. Can somebody explain what I didn’t understand or what I did wrong ?

Thanks !

Jeremie

Jeremie, what is the error message you are receiving? Can you also show us the script?

There are two types of errors in PowerShell: Terminating and Non-Terminating. The main difference between the two is in whether they allow a pipeline to continue executing or not. A terminating error will abort the entire pipeline, whereas a non-terminating error will generally just stop processing the current record, and allow the next pipeline input to be processed.

The -ErrorAction parameter, when passed to a cmdlet, affects how non-terminating errors behave within that cmdlet.

$ErrorActionPreference is slightly different. It does the same thing as -ErrorAction within cmdlets that you call, but it also affects the processing of all errors in your local scope (including terminating).

These little quirks and differences are what led me to write the error handling ebook, which you can find on this site under Resources -> Free eBooks. :slight_smile:

Hi,

I just want to hide the default error message for the [int]$numb. The error message appears if my users enter letters not numbers.
My true question is why : [int]$numb = Read-Host “Please choose the number of the destination OU :” -ErrorAction SilentlyContinue dont hide the message.

This is my script :

function Move-ACQComputer
{
    [CmdletBinding()]
    [OutputType([int])]
    Param
    (
        # Define the computer to move
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        $computer
    )

    Begin
    {
        $listou = Get-ADOrganizationalUnit -Filter 'Name -like "Lap*" -or Name -like "Desk*"' | Select DistinguishedName -ExpandProperty DistinguishedName | Sort-Object DistinguishedName
        $arrayou = for($i=0;$i-le $listou.length-1;$i++){"`[{0}] = {1}" -f $i,$listou[$i]}
        $computerou =  Get-ADComputer $computer | Select DistinguishedName -ExpandProperty DistinguishedName
        Write-Output "Computer $computer is member of this OU : $computerou."
        Write-Output ""
    }
    Process
    {
        Write-Output "This is the list with index of your OU :"
        $arrayou
        $ErrorActionPreference = "SilentlyContinue"
        [int]$numb = Read-Host "Please choose the number of the destination OU :"
        $ErrorActionPreference = "Continue"
        if ((!$numb) -or ($numb -gt $arrayou.LongLength))
        {
            $earry1 = $arrayou.Length -1
            Write-Error "YOU ENTER A UNVALID NUMBER. ENTER A NUMBER BEWTWEEN 0 and $earry1"
        }
        else
        {
            Get-ADComputer $computer | Move-ADObject -TargetPath $listou[$numb]    
        }
        
    }
    End
    {
        Write-Output "Result :"
        Get-ADComputer $computer
    }
}

I’m not sure if I’m clear or not.
J.

In this case, the error isn’t coming from Read-Host, it’s coming from the casting of whatever they typed to an [int]. A simpler way to achieve that (without the error message) is to use PowerShell’s -as operator:

$numb = (Read-Host "Please choose the number of the destination OU :") -as [int]

If they enter something non-numeric, $numb will wind up being $null, and no error will be produced.

Your first line wouldn’t throw an error unless you typed a non-integer.

You should really use a simple switch here imo. Use do/while $ou equals $null if you want to keep prompting for the correct number.

[int]$numb = Read-Host "Please choose the number of the destination OU :"

switch($numb){

1{$ou = 'one'}
2{$ou = 'two'}
3{$ou = 'three'}

}

if(($numb -gt 0) -and ($numb -le 3)){$ou}else{'you did not choose an OU'}

Or you could do it like this, in a try catch block. But I like Dave’s solution more, it’s simpler.

try{[int]$numb = Read-Host 'Please choose the number of the destination OU'}
catch{}

In my opinion it would be better to make $numb a parameter, so you can do parameter validation on it.

Hi,

Thanks everybody for your answer.
I tried the solution of Dave and it’s worked like I wanted. So thank’s Dave.

J.