2

How can I properly use ErrorAction parameter with my Advanced function? For instance I have such function:

function test1 {
[CmdletBinding()]
param([string]$path = "c:\temp")

$src = Join-Path $path "src"
$dest = Join-Path $path "dest"

Copy-Item -Path $src $dest -Recurse -Verbose
write "SomeText"
}

Lets assume the source path $src does not exist. And I am executing this function with ErrorAction = Stop:

test1 -ea stop

I expect that the error will be thrown and I will not see "SomeText" message. But I got it:

Copy-Item : Cannot find path 'C:\temp\src' because it does not exist.
At line:5 char:1
+ Copy-Item -Path $path\src $path\dest -Recurse -Verbose
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\temp\src:String) [Copy-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand

SomeText

I could add ErrorAction parameter to Copy-Item cmdlet inside the test1 function, but I want to be able to explicitly set it to the function to enable/disable error action behavior.

What should I do to make it work?

4
  • Unable to reproduce this behavior. Which version of PowerShell is this? Does it make a difference if you use the full parameter name (test1 -ErrorAction Stop)? Commented Aug 4, 2016 at 9:34
  • Likewise, not reproducible in PowerShell 5. Commented Aug 4, 2016 at 9:55
  • I discovered that w/o Verbose parameter added to Copy-Item it works fine (stops straight after the error). But with Verbose parameter it does not work. PowerShell v.5 Commented Aug 4, 2016 at 9:59
  • Hmm explicit preference variables must prevent automatic passing... helpful. Good catch. Commented Aug 4, 2016 at 10:08

3 Answers 3

2

You need to use $PSBoundParameters and alos check $ErrorActionPreference

function test1 {
    [CmdletBinding()]
    param([string]$path = "c:\temp")

    $src = Join-Path $path "src"
    $dest = Join-Path $path "dest"

    $errorAction = $PSBoundParameters["ErrorAction"]
    if(-not $errorAction){
        $errorAction = $ErrorActionPreference
    }


    Copy-Item -Path $src $dest -Recurse -Verbose -ErrorAction $errorAction
    write "SomeText"
}

$ErrorActionPreference = 'Stop'
test1

$ErrorActionPreference = 'continue'
test1 -ErrorAction Stop
Sign up to request clarification or add additional context in comments.

1 Comment

You should not consult $PSBoundParameters["ErrorAction"] (as evidenced from how messy this is!), when [CmdletBinding()] is present, the parameter is already applied to $ErrorActionPreference. Also, as part of CmdletBinding, you do not need to forward this to nested function calls, this is already done.
0

I would suggest you to place validation events for input parameters instead, like this:

param([ValidateScript({Test-Path $_})][string]$path = "c:\temp")

Comments

0

Handling is via Write-Error:

    function test1 {
        [CmdletBinding()]
        param()
        Write-Error "blah blah blah"
    }
  • Write-Error will throw when $ErrorActionPreference is Stop, ask the user for Inquire, print and continue for Continue, etc. etc.

More advanced handling is via:

  • throw, which will, by contrast, always halt execution.
  • $ErrorActionPreference, which holds the current state of the cascading -ErrorAction value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.