PowerShell basics

If you're unfamiliar with PowerShell, this page contains some general information and advice that will help you learn how to use the DLM Automation PowerShell cmdlets.

Open PowerShell ISE

Once you've installed DLM Automation, you can run the DLM Automation PowerShell cmdlets from any file location.

We recommend you run PowerShell ISE rather than basic PowerShell. PowerShell ISE has auto-complete behaviour that suggests available cmdlet names and parameters as you type.

To run PowerShell ISE:

  • From the Start menu, type PowerShell ISE.
  • Click Windows PowerShell ISE.

Allowing PowerShell ISE to run scripts

By default, PowerShell doesn't let you run saved scripts. If you need to do this, you may have to change your security policy.

You only need to do this once.

List all DLM Automation cmdlets

Run the following PowerShell code to see a list of all the DLM Automation cmdlets:

Get-Command -Module DlmAutomation

Getting information about a cmdlet

To get information about a cmdlet, including examples and a list of all available parameters, enter:

Get-Help <cmdlet name> -Full

Example

Get-Help Sync-DlmDatabaseSchema -Full

Input objects

Unlike the command line, which can only input and output text strings, PowerShell cmdlets work with objects.

Some PowerShell cmdlets require an input object, which you can specify after the cmdlet name:

Example

Invoke-DlmDatabaseTests "C:\Work\scripts"

In the example above, we've used the Invoke-DlmDatabaseTests cmdlet, which runs tSQLt tests on a database schema. The input object of the cmdlet is the scripts folder path C:\Work\scripts.

Variables

In PowerShell, you can assign values and objects to named variables, so you can reuse them in another PowerShell statement. Variable names are preceded by the dollar ($) symbol.

Example

$scriptsFolder = "C:\Work\scripts"
Invoke-DlmDatabaseTests $scriptsFolder

In the example above, we've assigned the scripts folder to the variable $scriptsFolder. We've then used this as the input object of the Invoke-DlmDatabaseTest cmdlet.

By using variables, you can take the output of one cmdlet and use it as the input of another:

Example

$scriptsFolder = "C:\Work\scripts"
$testResults = Invoke-DlmDatabaseTests $scriptsFolder
Export-DlmDatabaseTestResults $testResults -OutputFile "C:\Work\TestResults\scripts.junit.xml"

In the example above, we've assigned the output of the Invoke-DlmDatabaseTests cmdlet to the variable, $testResults. We've then used this as the input object for the Export-DlmDatabaseTestResults cmdlet. This cmdlet exports the test results to disk.

Cmdlet parameters

Most cmdlets use parameters that change their behaviour.

Example

$testResults = Invoke-DlmDatabaseTests "C:\Work\scripts"
Export-DlmDatabaseTestResults $testResults -OutputFile "C:\Work\TestResults\scripts.junit.xml"

In the example above, we've used the -OutputFile parameter of the Export-DlmDatabaseTestResults cmdlet to specify the file path to export the test results to.

You can also use variables to specify parameter values:

Example

$testResults = Invoke-DlmDatabaseTests "C:\Work\scripts"
$testResultsFile = "C:\Work\TestResults\scripts.junit.xml"
Export-DlmDatabaseTestResults $testResults -OutputFile $testResultsFile

Some parameters don't require a value. They're used to switch on or off a particular feature of the cmdlet:

Example

$testResults = Invoke-DlmDatabaseTests "C:\Work\scripts"
Export-DlmDatabaseTestResults $testResults -OutputFile "C:\Work\TestResults\scripts.junit.xml" -Force

In the example above, we've used the -Force parameter of the Export-DlmDatabaseTestResults cmdlet. This specifies that if the output file already exists, it should be overwritten.

Some paramaters are always required when you use a cmdlet. Others parameters are optional. To get help on a particular parameter and find out if it's required, enter:

Get-Help <cmdlet name> -Full <parameter name>

Example

Get-Help Export-DlmDatabaseTestResults -Parameter Force

Piping cmdlets together

We've already seen how with the help of variables, you can use the output of one cmdlet as the input of another. Another way to do this is to use the pipe (|) symbol to connect cmdlets together in a single PowerShell statement. When you "pipe" two cmdlets together, you're telling PowerShell to take the first cmdlet's output and use it as the input for the second cmdlet:

Example

Invoke-DlmDatabaseTests "C:\Work\scripts" | Export-DlmDatabaseTestResults -OutputFile "C:\Work\TestResults\scripts.junit.xml"

In the example above, we haven't specified an input object after the name of the Export-DlmDatabaseTestResults cmdlet. Instead we've used the pipe symbol to tell PowerShell to use the output of the Invoke-DlmDatabaseTests cmdlet as the input object.

Error action preference

PowerShell includes a set of preference variables that allow you to customize its behaviour. One of these is the error action preference variable. This determines what happens when there's an error in the script.

To make it easier to find errors, we recommend you set the error action preference variable to "stop". This means when a cmdlet encounters any kind of error, the PowerShell script will stop. To do this, at the beginning of your PowerShell script, enter the following:

$errorActionPreference = "stop"

What next?

You're now ready to look at the DLM Automation cmdlets in more detail. The best ones to start with are for Setting up database connections.

 


Didn't find what you were looking for?