Emulating -ErrorAction in custom powershell function

get-help about_Functions_CmdletBindingAttribute

You want:

function Foo1() {
 [CmdletBinding()]
 PARAM()
 process{
   Write-Host "in Foo1"
   Foo2
 }
}

This is not about emulation, it means really implementing common parameters in your function; if this was your intention.


After that, you can work like this:

Foo1 -ErrorAction stop

You can use the same syntax for Foo2 and Foo3.


To log error use redirection as usual.


Here is a sample to illustrate @Empo Answer

function Test-ErrorAction
{
  [CmdletBinding()]
  Param( )

  begin 
  {
    Write-Host "I'am Here"    
   }

  Process 
  {
    Write-Error "coucou"
  }
  end 
  {
    Write-Host "Done !"
  }
}

clear
Test-ErrorAction -ErrorAction "silentlycontinue"
Test-ErrorAction -ErrorAction "stop"

gives

I'am Here
Done !
I'am Here
coucou
 à C:\Développements\Pgdvlp_Powershell\Sources partagées\Menus Contextuel Explorer\Test-ErrorAction.ps1: ligne:23 caractère:17
+ Test-ErrorAction  <<<< -ErrorAction "stop"

Tags:

Powershell