commands

^

New-DbaCustomError

Author Adam Lancaster https://github.com/lancasteradam
Availability Windows, Linux, macOS

 

Synopsis

Creates a user defined message in sys.messages. This command does not support Azure SQL Database.

Description

This command provides a wrapper for the sp_addmessage system procedure that allows for user defined messages to be added to sys.messages.

Note: See the remarks section of the documentation for sp_addmessage regarding the addition of non-English messages. The U.S. English message needs to be added first and the severity must be the same.

Syntax

New-DbaCustomError
    [-SqlInstance] <DbaInstanceParameter[]>
    [[-SqlCredential] <PSCredential>]
    [[-MessageID] <Int32>]
    [[-Severity] <Int32>]
    [[-MessageText] <String>]
    [[-Language] <String>]
    [-WithLog]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> New-DbaCustomError -SqlInstance sqldev01, sqldev02 -MessageID 70001 -Severity 16 -MessageText "test"

Creates a new custom message on the sqldev01 and sqldev02 instances with ID 70001, severity 16, and text "test".

Example: 2
PS C:\> New-DbaCustomError -SqlInstance sqldev01 -MessageID 70001 -Severity 16 -MessageText "test" -Language "French"

Creates a new custom message on the sqldev01 instance for the french language with ID 70001, severity 16, and text "test".

Example: 3
PS C:\> New-DbaCustomError -SqlInstance sqldev01 -MessageID 70001 -Severity 16 -MessageText "test" -WithLog

Creates a new custom message on the sqldev01 instance with ID 70001, severity 16, text "test", and enables the log mechanism.

Example: 4
PS C:\> $server = Connect-DbaInstance sqldev01
PS C:\> $newMessage = New-DbaCustomError -SqlInstance $server -MessageID 70000 -Severity 16 -MessageText "test_70000"

Creates a new custom message on the sqldev01 instance with ID 70000, severity 16, and text "test_70000"
To modify the custom message at a later time the following can be done to change the severity from 16 to 20:

PS C:\> $original = $server.UserDefinedMessages | Where-Object ID -eq 70000
PS C:\> $messageID = $original.ID
PS C:\> $severity = 20
PS C:\> $text = $original.Text
PS C:\> $language = $original.Language
PS C:\> $removed = Remove-DbaCustomError -SqlInstance $server -MessageID 70000
PS C:\> $alteredMessage = New-DbaCustomError -SqlInstance $server -MessageID $messageID -Severity $severity -MessageText $text -Language $language -WithLog

The resulting updated message object is available in $alteredMessage.

Required Parameters

-SqlInstance

The target SQL Server instance or instances. This can be a collection and receive pipeline input to allow the function to be executed against multiple SQL Server instances.

Alias
Required True
Pipeline true (ByValue)
Default Value

Optional Parameters

-SqlCredential

Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential). Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported. For MFA support, please use Connect-DbaInstance.

Alias
Required False
Pipeline false
Default Value
-MessageID

An integer between 50001 and 2147483647.

Alias
Required False
Pipeline false
Default Value 0
-Severity

Severity level between 1 and 25.

Alias
Required False
Pipeline false
Default Value 0
-MessageText

Error message text with max length of 255 characters.

Alias
Required False
Pipeline false
Default Value
-Language

Language for this message. The valid values for Language are contained in the Name and Alias columns from sys.syslanguages.

Alias
Required False
Pipeline false
Default Value English
-WithLog

Always write this message to the Windows application log and the SQL Server Error Log when it occurs.

Alias
Required False
Pipeline false
Default Value False
-EnableException

By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message. This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting. Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.

Alias
Required False
Pipeline false
Default Value False
-WhatIf

Shows what would happen if the command were to run. No actions are actually performed.

Alias wi
Required False
Pipeline false
Default Value
-Confirm

Prompts you for confirmation before executing any changing operations within the command.

Alias cf
Required False
Pipeline false
Default Value

 

Want to see the source code for this command? Check out New-DbaCustomError on GitHub.
Want to see the Bill Of Health for this command? Check out New-DbaCustomError.