Overview
Pre-scripts and post-scripts are Windows PowerShell code executed on the Staging Target host before and after a SnapSync of a dSource. You can specify pre- and post-scripts in the wizard for creating a dSource, or you can modify them afterwards by navigating to the Configuration > Standard tab. You can also set pre- and post-scripts using the Delphix command line interface (CLI) or REST Web API.
The Delphix Engine executes a pre-script on the Staging Target host prior to the SnapSync of a dSource. If it is an initial snapshot, or manual snapshot by selecting the snapshot button on the GUI, the pre/post scripts do not get executed for dSources. Since the dSource resides within a restoring database in the SQL instance on the Staging Target host, the script can perform queries on the instance if a database account is available.
Hooks do allow for a pre/post snapshot hook, but the pre/post scripts do not
A post-script is executed after the SnapSync on a dSource completes. If the post-script fails, the provision, refresh, or rewind operation will also fail with an error message, and a fault will be created on the dSource.
Pre/Post hooks allow for pre/post snapshot hooks for which there is no equivalent in the old pre/post scripts. Pre/Post scripts are run during validated sync, for which there is no equivalent hook operation today.
By default, hooks for SQL Server are not enabled on Delphix virtualization engines; to enable them, you need to create a case with Delphix Support. Pre- and post-scripts are supported for backward compatibility with older versions of the Delphix virtualization engine.
Associating Scripts with a dSource
During the dSource linking process
- Login to the Delphix Admin application using delphix_admin credentials or another account with admin privileges.
- In the top menu bar, click Manage.
- Select Add dSource.
- In the third panel of the Link dSource wizard, entitled Data Management, go to the bottom of the panel and click Advanced.
Fields for Pre Script and Post Script will appear. - Enter the calling syntax of the Windows PowerShell script into either or both of the appropriate fields.
- The calling environment is the used during linking, as shown in the Environment Details panel of the Delphix environment (Manage > Environments)
- Four (4) environment variables will be populated with the name of the Delphix dSource, the SQL Server instance name and port, and the SQL Server database name.
After linking, using the Configuration tab of the Datasets details page
- Login to the Delphix Admin application using delphix_admin credentials or another account with admin privileges.
- In the top menu bar, click Manage.
- Select My Datasets to display the SQL Server dSources and VDBs.
- Select a SQL Server dSource from the listed Datasets in the left-hand navigation bar.
- Click the Configuration panel.
- In the Data Management column:
- Click the pencil icon
- Enter the calling syntax of the Windows Powershell script into either or both of the appropriate fields
- The calling environment is that of the primary Environment User account, as shown in the Environment Details panel of the Delphix environment (Manage > Environments)
- Four (4) environment variables will be populated with the name of the VDB, the SQL Server instance name and port, and the SQL Server database name.
- Click the check mark icon to accept the change.
- Click the pencil icon
Execution Context for SQL Server Scripts
For dSources, pre- and post-scripts are executed in the context of the staging host user that was provided when linking.
Available Variables for SQL Server dSource Scripts
The Delphix Engine sets the following environment variables prior to launching the pre- and post-scripts that are associated with a dSource.
Environment Variables | Description |
---|---|
SOURCE_INSTANCE_HOST | Hostname of linked instance for the dSource |
SOURCE_INSTANCE_PORT | Port of linked instance for the dSource |
SOURCE_INSTANCE_NAME | Name of linked instance for the dSource |
SOURCE_DATABASE_NAME | Name of database linked for the dSource |
Error handling for SQL Server PowerShell Scripts
PowerShell gives you a few ways to handle errors in your scripts:
Set
$
ErrorActionPreference
. This only applies to PowerShell Cmdlets. For scripts or other executables such assqlcmd
, PowerShell will return with exit code 0 even if there is an error, regardless of the value of$ErrorActionPreference
. The allowable values for$ErrorActionPreference
are:Continue
(default) – Continue even if there is an errorSilentlyContinue
– Acts like Continue with the exception that errors are not displayedInquire
– Prompts the user in case of errorStop
: Stops execution after the first error
- Use exception handling by using traps and try/catch blocks to detect errors and return with non-zero exit codes
Use custom error handling that can be invoked after launching each command in the script to correctly detect errors. The following example shows how you can use the function verifySuccess to detect whether the previous command failed, and if it did print, print an error message and return with an exit code of 1.
function die { Write-Error "Error: $($args[0])" exit 1 } function verifySuccess { if (!$?) { die "$($args[0])" } } Write-Output "I'd rather be in Hawaii" verifySuccess "WRITE_OUTPUT_FAILED" & C:\Program Files\Delphix\scripts\myscript.ps1 verifySuccess "MY_SCRIPT_FAILED"