You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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
125
93
In order to use these examples (below), you need to install the SqlServer module from the [PowerShell Gallery](https://www.powershellgallery.com/packages/SqlServer).
126
94
127
95
```powershell
@@ -145,34 +113,17 @@ Instance Name Version ProductLevel UpdateLevel HostPlatform Host
145
113
ServerA 13.0.5233 SP2 CU4 Windows Windows Server 2016 Datacenter
146
114
ServerB 14.0.3045 RTM CU12 Linux Ubuntu
147
115
```
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
-
```
161
116
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.
163
118
164
119
```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
+
}
172
125
```
173
126
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
-
176
127
Here is a sample of what that output will look like:
177
128
178
129
```
@@ -190,7 +141,7 @@ tempdb Normal 72.00 MB 61.25 MB Simple 140 sa
190
141
WideWorldImporters Normal 3.2 GB 2.6 GB Simple 130 sa
191
142
```
192
143
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.
194
145
195
146
```powershell
196
147
Get-SqlDatabase -ServerInstance ServerB |
@@ -208,6 +159,28 @@ FOREACH {
208
159
}
209
160
```
210
161
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
+
211
184
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.
212
185
213
186
```powershell
@@ -235,6 +208,24 @@ tempdb Normal 72.00 MB 61.25 MB Simple 140 sa
235
208
WideWorldImporters Normal 3.2 GB 2.6 GB Simple 130 sa
236
209
```
237
210
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 |
0 commit comments