< Back

Stop-PSFFunction

Sun Jan 19, 2020 6:45 pm

NAME Stop-PSFFunction



SYNOPSIS

Function that interrupts a function.





SYNTAX

Stop-PSFFunction -Message <String> [-EnableException <Boolean>] [-Category {NotSpecified | OpenError | CloseError

| DeviceError | DeadlockDetected | InvalidArgument | InvalidData | InvalidOperation | InvalidResult | InvalidType

| MetadataError | NotImplemented | NotInstalled | ObjectNotFound | OperationStopped | OperationTimeout |

SyntaxError | ParserError | PermissionDenied | ResourceBusy | ResourceExists | ResourceUnavailable | ReadError |

WriteError | FromStdErr | SecurityError | ProtocolError | ConnectionError | AuthenticationError | LimitsExceeded |

QuotaExceeded | NotEnabled}] [-ErrorRecord <ErrorRecord[]>] [-Tag <String[]>] [-FunctionName <String>]

[-ModuleName <String>] [-File <String>] [-Line <Int32>] [-Exception <Exception>] [-OverrideExceptionMessage]

[-Target <Object>] [-Continue] [-SilentlyContinue] [-ContinueLabel <String>] [-Cmdlet <PSCmdlet>] [-StepsUpward

<Int32>] [<CommonParameters>]



Stop-PSFFunction -String <String> [-StringValues <Object[]>] [-EnableException <Boolean>] [-Category {NotSpecified

| OpenError | CloseError | DeviceError | DeadlockDetected | InvalidArgument | InvalidData | InvalidOperation |

InvalidResult | InvalidType | MetadataError | NotImplemented | NotInstalled | ObjectNotFound | OperationStopped |

OperationTimeout | SyntaxError | ParserError | PermissionDenied | ResourceBusy | ResourceExists |

ResourceUnavailable | ReadError | WriteError | FromStdErr | SecurityError | ProtocolError | ConnectionError |

AuthenticationError | LimitsExceeded | QuotaExceeded | NotEnabled}] [-ErrorRecord <ErrorRecord[]>] [-Tag

<String[]>] [-FunctionName <String>] [-ModuleName <String>] [-File <String>] [-Line <Int32>] [-Exception

<Exception>] [-OverrideExceptionMessage] [-Target <Object>] [-Continue] [-SilentlyContinue] [-ContinueLabel

<String>] [-Cmdlet <PSCmdlet>] [-StepsUpward <Int32>] [<CommonParameters>]





DESCRIPTION

Function that interrupts a function.



This function is a utility function used by other functions to reduce error catching overhead.

It is designed to allow gracefully terminating a function with a warning by default and also allow opt-in into

terminating errors.

It also allows simple integration into loops.



Note:

When calling this function with the intent to terminate the calling function in non-ExceptionEnabled mode too, you

need to add a return below the call.



For a more detailed explanation - including commented full-scale implementation examples - see the associated help

article:

Get-Help about_psf_flowcontrol





PARAMETERS

-Message <String>

A message to pass along, explaining just what the error was.



Required? true

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-String <String>

A stored string to use to write the log.

Used in combination with the localization component.

For more details see the help on Import-PSFLocalizedString and Get-PSFLocalizedString.



Required? true

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-StringValues <Object[]>

Values to format into the localized string referred to in the -String parameter.



Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-EnableException <Boolean>

Replaces user friendly yellow warnings with bloody red exceptions of doom!

Use this if you want the function to throw terminating errors you want to catch.



Required? false

Position? named

Default value False

Accept pipeline input? false

Accept wildcard characters? false



-Category

What category does this termination belong to?

Is automatically set when passing an error record. Helps with differentiating exceptions without having to

resort to text parsing.



Required? false

Position? named

Default value NotSpecified

Accept pipeline input? false

Accept wildcard characters? false



-ErrorRecord <ErrorRecord[]>

An option to include an inner exception in the error record (and in the exception thrown, if one is thrown).

Use this, whenever you call Stop-PSFFunction in a catch block.



Note:

Pass the full error record, not just the exception.



Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-Tag <String[]>

Tags to add to the message written.

This allows filtering and grouping by category of message, targeting specific messages.



Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-FunctionName <String>

The name of the function to crash.

This parameter is very optional, since it automatically selects the name of the calling function.

The function name is used as part of the errorid.

That in turn allows easily figuring out, which exception belonged to which function when checking out the

$error variable.



Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-ModuleName <String>

The name of the module, the function to be crashed is part of.

This parameter is very optional, since it automatically selects the name of the calling function.



Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-File <String>

The file in which Stop-PSFFunction was called.

Will be automatically set, but can be overridden when necessary.



Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-Line <Int32>

The line on which Stop-PSFFunction was called.

Will be automatically set, but can be overridden when necessary.



Required? false

Position? named

Default value 0

Accept pipeline input? false

Accept wildcard characters? false



-Exception <Exception>

Allows specifying an inner exception as input object. This will be passed on to the logging and used for

messages.

When specifying both ErrorRecord AND Exception, Exception wins, but ErrorRecord is still used for record

metadata.



Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-OverrideExceptionMessage [<SwitchParameter>]

Disables automatic appending of exception messages.

Use in cases where you already have a speaking message interpretation and do not need the original message.



Required? false

Position? named

Default value False

Accept pipeline input? false

Accept wildcard characters? false



-Target <Object>

The object that was processed when the error was thrown.

For example, if you were trying to process a Database Server object when the processing failed, add the object

here.

This object will be in the error record (which will be written, even in non-silent mode, just won't show it).

If you specify such an object, it becomes simple to actually figure out, just where things failed at.



Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-Continue [<SwitchParameter>]

This will cause the function to call continue while not running with exceptions enabled (-EnableException).

Useful when mass-processing items where an error shouldn't break the loop.



Required? false

Position? named

Default value False

Accept pipeline input? false

Accept wildcard characters? false



-SilentlyContinue [<SwitchParameter>]

This will cause the function to call continue while running with exceptions enabled (-EnableException).

Useful when mass-processing items where an error shouldn't break the loop.



Required? false

Position? named

Default value False

Accept pipeline input? false

Accept wildcard characters? false



-ContinueLabel <String>

When specifying a label in combination with "-Continue" or "-SilentlyContinue", this function will call

continue with this specified label.

Helpful when trying to continue on an upper level named loop.



Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-Cmdlet <PSCmdlet>

The $PSCmdlet object of the calling command.

Used to write exceptions in a more hidden manner, avoiding exposing internal script text in the default

message display.



Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-StepsUpward <Int32>

When not throwing an exception and not calling continue, Stop-PSFFunction signals the calling command to stop.

In some cases you may want to signal a step or more further up the chain (notably from helper functions within

a function).

This parameter allows you to add additional steps up the callstack that it will notify.



Required? false

Position? named

Default value 0

Accept pipeline input? false

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).



INPUTS



OUTPUTS



-------------------------- EXAMPLE 1 --------------------------



PS C:\\>Stop-PSFFunction -Message "Foo failed bar!" -EnableException $EnableException -ErrorRecord $_



return



Depending on whether $EnableException is true or false it will:

- Throw a bloody terminating error. Game over.

- Write a nice warning about how Foo failed bar, then terminate the function. The return on the next line will

then end the calling function.









-------------------------- EXAMPLE 2 --------------------------



PS C:\\>Stop-PSFFunction -Message "Foo failed bar!" -EnableException $EnableException -Category InvalidOperation

-Target $foo -Continue



Depending on whether $EnableException is true or false it will:

- Throw a bloody terminating error. Game over.

- Write a nice warning about how Foo failed bar, then call continue to process the next item in the loop.

In both cases, the error record added to $error will have the content of $foo added, the better to figure out what

went wrong.











RELATED LINKS