Skip to content

Added SQL PowerShell Examples #1787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 13, 2019
67 changes: 67 additions & 0 deletions docs/azure_data_studio/README_FOR_MARKETPLACE.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,73 @@ To open/view the extension's examples in Visual Studio Code, run the following f
code (Get-ChildItem $Home\.azuredatastudio\extensions\ms-vscode.PowerShell-*\examples)[-1]
```

### SQL PowerShell Examples
In order to use these examples (below), you need to install the SqlServer module from the [PowerShell Gallery](https://www.powershellgallery.com/packages/SqlServer).

```powershell
Install-Module -Name SqlServer -AllowPrerelease
```

In this example, we use the `Get-SqlInstance` cmdlet to Get the Server SMO objects for ServerA & ServerB. The default output for this command will include the Instance name, version, Service Pack, & CU Update Level of the instances.

```powershell
Get-SqlInstance -ServerInstance ServerA, ServerB
```

Here is a sample of what that output will look like:

```
Instance Name Version ProductLevel UpdateLevel
------------- ------- ------------ -----------
ServerA 13.0.5233 SP2 CU4
ServerB 14.0.3045 RTM CU12
```

In this example, we will do a `dir` (alias for `Get-ChildItem`) to get the list of all SQL Server instances listed in your Registered Servers file, and then use the `Get-SqlDatabase` cmdlet to get a list of Databases for each of those instances.

```powershell
dir 'SQLSERVER:\SQLRegistration\Database Engine Server Group' -Recurse |
WHERE { $_.Mode -ne 'd' } |
FOREACH {
Get-SqlDatabase -ServerInstance $_.Name
}
```

Here is a sample of what that output will look like:

```
Name Status Size Space Recovery Compat. Owner
Available Model Level
---- ------ ---- ---------- -------- ------- -----
AdventureWorks2017 Normal 336.00 MB 57.01 MB Simple 140 sa
master Normal 6.00 MB 368.00 KB Simple 140 sa
model Normal 16.00 MB 5.53 MB Full 140 sa
msdb Normal 48.44 MB 1.70 MB Simple 140 sa
PBIRS Normal 144.00 MB 55.95 MB Full 140 sa
PBIRSTempDB Normal 16.00 MB 4.20 MB Simple 140 sa
SSISDB Normal 325.06 MB 26.21 MB Full 140 sa
tempdb Normal 72.00 MB 61.25 MB Simple 140 sa
WideWorldImporters Normal 3.2 GB 2.6 GB Simple 130 sa
```

This example uses the `Get-SqlDatabase` cmdlet to retrieve a list of all databases on the ServerB instance, then presents a grid/table (using the `Out-GridView` cmdlet) to select which databases should be backed up. Once the user clicks on the "OK" button, only the highlighted databases will be backed up.

```powershell
Get-SqlDatabase -ServerInstance ServerB |
Out-GridView -PassThru |
Backup-SqlDatabase -CompressionOption On
```

This example, again, gets list of all SQL Server instances listed in your Registered Servers file, then calls the `Get-SqlAgentJobHistory` which reports every failed SQL Agent Job since Midnight, for each SQL Server instances listed.

```powershell
dir 'SQLSERVER:\SQLRegistration\Database Engine Server Group' -Recurse |
WHERE {$_.Mode -ne 'd' } |
FOREACH {
Get-SqlAgentJobHistory -ServerInstance $_.Name -Since Midnight -OutcomesType Failed
}
```

## Contributing to the Code

Check out the [development documentation](docs/development.md) for more details
Expand Down