< Back

Assert-Error

Sun Jan 12, 2020 5:38 pm

NAME Assert-Error



SYNOPSIS

Asserts that there are errors, and/or that a specific error's messages matches a regular expression.





SYNTAX

Assert-Error [-Count <Int32>] [[-Message] <String>] [<CommonParameters>]



Assert-Error -Last [-Count <Int32>] [-Regex] <Regex> [[-Message] <String>] [<CommonParameters>]



Assert-Error -First [-Count <Int32>] [-Regex] <Regex> [[-Message] <String>] [<CommonParameters>]



Assert-Error [-Index] <Int32> [-Count <Int32>] [-Regex] <Regex> [[-Message] <String>] [<CommonParameters>]





DESCRIPTION

With no parameters, checks that there are errors in the `$Error` array.



If passed an index and a regular expression, checks that there are errors in the `$Error` array, that there is one

at index `Index`, and that it's message matches `$Regex`.



If you want to check the last/most recent error, use the `-Last` switch. To check the first/oldest error, use the

`-First` switch.





PARAMETERS

-Last [<SwitchParameter>]

Checks the last/most recent error.



Required? true

Position? named

Default value False

Accept pipeline input? false

Accept wildcard characters? false



-First [<SwitchParameter>]

Checks the first/oldest error.



Required? true

Position? named

Default value False

Accept pipeline input? false

Accept wildcard characters? false



-Index <Int32>

The index of the error to check.



Required? true

Position? 1

Default value 0

Accept pipeline input? false

Accept wildcard characters? false



-Count <Int32>

Check the number of errors.



Required? false

Position? named

Default value 0

Accept pipeline input? false

Accept wildcard characters? false



-Regex <Regex>

The regular expression to check.



Required? true

Position? 1

Default value

Accept pipeline input? false

Accept wildcard characters? false



-Message <String>

A message to show if the assertion fails.



Required? false

Position? 1

Default value

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:\\>Assert-Error



Demonstrates how to check that there is at least one error.









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



PS C:\\>Assert-Error 0 'not found'



Demonstrates how to check that the last error (remember, `$Error` is a stack) matches the regular expression `not

found`.









-------------------------- EXAMPLE 3 --------------------------



PS C:\\>Assert-Error -1 'not found'



Demonstrates how to check that the first error (remember, `$Error` is a stack) matches the regular expression `not

found`.









-------------------------- EXAMPLE 4 --------------------------



PS C:\\>Assert-Error -Last 'not found'



Demonstrates how to check that the last error matches the regular exprssion `not found` without worrying about

indexes.









-------------------------- EXAMPLE 5 --------------------------



PS C:\\>Assert-Error -First 'not found'



Demonstrates how to check that the first error matches the regular exprssion `not found` without worrying about

indexes.











RELATED LINKS