Skip to content

Commit 7f9afdb

Browse files
authored
Update powershell-extension.md
1 parent 83a13b7 commit 7f9afdb

1 file changed

Lines changed: 50 additions & 59 deletions

File tree

docs/azure-data-studio/powershell-extension.md

Lines changed: 50 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Read the [FAQ](https://github.com/PowerShell/vscode-powershell/wiki/FAQ) for ans
5757

5858
## Installing PowerShell Core
5959

60-
If you are running Azure Data Studio on macOS or Linux, you may also need to install PowerShell Core.
60+
If you are running Azure Data Studio on MacOS or Linux, you may also need to install PowerShell Core.
6161

6262
PowerShell Core is an Open Source project on [GitHub](https://github.com/powershell/powershell).
6363
For more information on installing PowerShell Core on MacOS or Linux platforms, see the following articles:
@@ -83,45 +83,13 @@ or if you're using the preview version of the extension
8383
$HOME/.azuredatastudio/extensions/ms-vscode.powershell-preview-<version>/examples
8484
```
8585

86-
To open/view the extension's examples in Azure Data Studio, run the following code from your PowerShell command prompt:
86+
To open/view the extension's examples in Azure Data Studio, run the following from your PowerShell command prompt:
8787

8888
```powershell
8989
azuredatastudio (Get-ChildItem $Home\.azuredatastudio\extensions\ms-vscode.PowerShell-*\examples)[-1]
9090
```
9191

92-
### Creating and opening files
93-
94-
To create and open a new file inside the editor, use the New-EditorFile from within the PowerShell Integrated Terminal.
95-
96-
```powershell
97-
PS C:\temp> New-EditorFile ExportData.ps1
98-
```
99-
100-
This command works for any file type, not just PowerShell files.
101-
102-
```powershell
103-
PS C:\temp> New-EditorFile ImportData.py
104-
```
105-
106-
To open one or more files in Azure Data Studio, use the `Open-EditorFile` command.
107-
108-
```powershell
109-
Open-EditorFile ExportData.ps1, ImportData.py
110-
```
111-
112-
### No focus on console when executing
113-
114-
For those users who are used to working with SSMS, you're used to being able to execute a query, and then being able to re-execute it again without having to switch back to the query pane. In this case, the default behavior of the code editor may feel strange to you. To keep the focus in the editor when you execute with <kbd>F8</kbd> change the following setting:
115-
116-
```json
117-
"powershell.integratedConsole.focusConsoleOnExecute": false
118-
```
119-
120-
The default is `true` for accessibility purposes.
121-
122-
Be aware this setting will prevent the focus from changing to the console, even when you use a command that explicitly calls for input, like `Get-Credential`.
123-
124-
## SQL PowerShell Examples
92+
### SQL PowerShell Examples
12593
In order to use these examples (below), you need to install the SqlServer module from the [PowerShell Gallery](https://www.powershellgallery.com/packages/SqlServer).
12694

12795
```powershell
@@ -145,34 +113,17 @@ Instance Name Version ProductLevel UpdateLevel HostPlatform Host
145113
ServerA 13.0.5233 SP2 CU4 Windows Windows Server 2016 Datacenter
146114
ServerB 14.0.3045 RTM CU12 Linux Ubuntu
147115
```
148-
The `SqlServer` module contains a Provider called `SQLRegistration` which allows you to programmatically access the following types of saved SQL Server connections:
149-
150-
+ Database Engine Server (Registered Servers)
151-
+ Central Management Server (CMS)
152-
+ Analysis Services
153-
+ Integration Services
154-
+ Reporting Services
155-
156-
In the following 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.
157-
158-
```powershell
159-
dir 'SQLSERVER:\SQLRegistration\Database Engine Server Group' -Recurse
160-
```
161116

162-
Here is a sample of what that output could look like:
117+
In the following 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.
163118

164119
```powershell
165-
Mode Name
166-
---- ----
167-
- ServerA
168-
- ServerB
169-
- localhost\SQL2017
170-
- localhost\SQL2016Happy
171-
- localhost\SQL2017
120+
dir 'SQLSERVER:\SQLRegistration\Database Engine Server Group' -Recurse |
121+
WHERE { $_.Mode -ne 'd' } |
122+
FOREACH {
123+
Get-SqlDatabase -ServerInstance $_.Name
124+
}
172125
```
173126

174-
For many operations that involve a database, or objects within a database, the `Get-SqlDatabase` cmdlet can be used. If you supply values for both the `-ServerInstance` and `-Database` parameters, only that one database object will be retrieved. However, if you specify only the `-ServerInstance` parameter, a full list of all databases on that instance will be returned.
175-
176127
Here is a sample of what that output will look like:
177128

178129
```
@@ -190,7 +141,7 @@ tempdb Normal 72.00 MB 61.25 MB Simple 140 sa
190141
WideWorldImporters Normal 3.2 GB 2.6 GB Simple 130 sa
191142
```
192143

193-
This next 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.
144+
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.
194145

195146
```powershell
196147
Get-SqlDatabase -ServerInstance ServerB |
@@ -208,6 +159,28 @@ FOREACH {
208159
}
209160
```
210161

162+
### SQL PowerShell Examples
163+
In order to use these examples (below), you need to install the SqlServer module from the [PowerShell Gallery](https://www.powershellgallery.com/packages/SqlServer).
164+
165+
```powershell
166+
Install-Module -Name SqlServer -AllowPrerelease
167+
```
168+
169+
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.
170+
171+
```powershell
172+
Get-SqlInstance -ServerInstance ServerA, ServerB
173+
```
174+
175+
Here is a sample of what that output will look like:
176+
177+
```
178+
Instance Name Version ProductLevel UpdateLevel
179+
------------- ------- ------------ -----------
180+
ServerA 13.0.5233 SP2 CU4
181+
ServerB 14.0.3045 RTM CU12
182+
```
183+
211184
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.
212185

213186
```powershell
@@ -235,6 +208,24 @@ tempdb Normal 72.00 MB 61.25 MB Simple 140 sa
235208
WideWorldImporters Normal 3.2 GB 2.6 GB Simple 130 sa
236209
```
237210

211+
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.
212+
213+
```powershell
214+
Get-SqlDatabase -ServerInstance ServerB |
215+
Out-GridView -PassThru |
216+
Backup-SqlDatabase -CompressionOption On
217+
```
218+
219+
This example, again, gets a 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 instance listed.
220+
221+
```powershell
222+
dir 'SQLSERVER:\SQLRegistration\Database Engine Server Group' -Recurse |
223+
WHERE {$_.Mode -ne 'd' } |
224+
FOREACH {
225+
Get-SqlAgentJobHistory -ServerInstance $_.Name -Since Midnight -OutcomesType Failed
226+
}
227+
```
228+
238229
## Reporting Problems
239230

240231
If you experience any problems with the PowerShell Extension, see

0 commit comments

Comments
 (0)