Skip to content

Commit 8a16c8f

Browse files
authored
Merge pull request #24766 from MightyPen/xeventps-03
Updating Powershell script to new module AzureRm.
2 parents 5461225 + 879492c commit 8a16c8f

File tree

1 file changed

+62
-72
lines changed

1 file changed

+62
-72
lines changed

articles/sql-database/sql-database-xevent-code-event-file.md

Lines changed: 62 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ ms.workload: data-management
1515
ms.tgt_pltfrm: na
1616
ms.devlang: na
1717
ms.topic: article
18-
ms.date: 02/06/2017
18+
ms.date: 10/04/2017
1919
ms.author: genemi
2020

2121
---
@@ -65,176 +65,165 @@ The script starts with commands to clean up after a possible previous run, and i
6565
* If you rerun the script without disrupting your session, you have the convenient option of commenting out the **Add-AzureAccount** command.
6666

6767
![PowerShell ISE, with Azure module installed, ready to run script.][30_powershell_ise]
68-
68+
;
6969

7070
### PowerShell code
7171

72+
This PowerShell script assumes you have already run the cmdlet Import-Module for the AzureRm module. For reference documentation, see [PowerShell Module Browser](https://docs.microsoft.com/powershell/module/).
73+
7274
```powershell
7375
## TODO: Before running, find all 'TODO' and make each edit!!
7476
75-
#--------------- 1 -----------------------
76-
77-
78-
# You can comment out or skip this Add-AzureAccount
79-
# command after the first run.
80-
# Current PowerShell environment retains the successful outcome.
81-
82-
'Expect a pop-up window in which you log in to Azure.'
77+
cls;
8378
79+
#--------------- 1 -----------------------
8480
85-
Add-AzureAccount
81+
'Script assumes you have already logged your PowerShell session into Azure.
82+
But if not, run Add-AzureRmAccount (or Login-AzureRmAccount), just one time.';
83+
#Add-AzureRmAccount; # Same as Login-AzureRmAccount.
8684
8785
#-------------- 2 ------------------------
8886
89-
9087
'
9188
TODO: Edit the values assigned to these variables, especially the first few!
92-
'
89+
';
9390
9491
# Ensure the current date is between
9592
# the Expiry and Start time values that you edit here.
9693
97-
$subscriptionName = 'YOUR_SUBSCRIPTION_NAME'
98-
$policySasExpiryTime = '2016-01-28T23:44:56Z'
99-
$policySasStartTime = '2015-08-01'
100-
94+
$subscriptionName = 'YOUR_SUBSCRIPTION_NAME';
95+
$resourceGroupName = 'YOUR_RESOURCE-GROUP-NAME';
10196
102-
$storageAccountName = 'gmstorageaccountxevent'
103-
$storageAccountLocation = 'West US'
104-
$contextName = 'gmcontext'
105-
$containerName = 'gmcontainerxevent'
106-
$policySasToken = 'gmpolicysastoken'
97+
$policySasExpiryTime = '2018-08-28T23:44:56Z';
98+
$policySasStartTime = '2017-10-01';
10799
100+
$storageAccountLocation = 'West US';
101+
$storageAccountName = 'YOUR_STORAGE_ACCOUNT_NAME';
102+
$contextName = 'YOUR_CONTEXT_NAME';
103+
$containerName = 'YOUR_CONTAINER_NAME';
104+
$policySasToken = ' ? ';
108105
109-
# Leave this value alone, as 'rwl'.
110-
$policySasPermission = 'rwl'
106+
$policySasPermission = 'rwl'; # Leave this value alone, as 'rwl'.
111107
112108
#--------------- 3 -----------------------
113109
114-
115110
# The ending display lists your Azure subscriptions.
116111
# One should match the $subscriptionName value you assigned
117112
# earlier in this PowerShell script.
118113
119-
'Choose an existing subscription for the current PowerShell environment.'
120-
121-
122-
Select-AzureSubscription -SubscriptionName $subscriptionName
114+
'Choose an existing subscription for the current PowerShell environment.';
123115
116+
Select-AzureRmSubscription -Subscription $subscriptionName;
124117
125118
#-------------- 4 ------------------------
126119
127-
128120
'
129121
Clean up the old Azure Storage Account after any previous run,
130-
before continuing this new run.'
131-
122+
before continuing this new run.';
132123
133124
If ($storageAccountName)
134125
{
135-
Remove-AzureStorageAccount -StorageAccountName $storageAccountName
126+
Remove-AzureRmStorageAccount `
127+
-Name $storageAccountName `
128+
-ResourceGroupName $resourceGroupName;
136129
}
137130
138131
#--------------- 5 -----------------------
139132
140-
[System.DateTime]::Now.ToString()
133+
[System.DateTime]::Now.ToString();
141134
142135
'
143136
Create a storage account.
144137
This might take several minutes, will beep when ready.
145-
...PLEASE WAIT...'
146-
147-
New-AzureStorageAccount `
148-
-StorageAccountName $storageAccountName `
149-
-Location $storageAccountLocation
150-
151-
[System.DateTime]::Now.ToString()
138+
...PLEASE WAIT...';
152139
153-
[System.Media.SystemSounds]::Beep.Play()
140+
New-AzureRmStorageAccount `
141+
-Name $storageAccountName `
142+
-Location $storageAccountLocation `
143+
-ResourceGroupName $resourceGroupName `
144+
-SkuName 'Standard_LRS';
154145
146+
[System.DateTime]::Now.ToString();
147+
[System.Media.SystemSounds]::Beep.Play();
155148
156149
'
157-
Get the primary access key for your storage account.
158-
'
159-
150+
Get the access key for your storage account.
151+
';
160152
161-
$primaryAccessKey_ForStorageAccount = `
162-
(Get-AzureStorageKey `
163-
-StorageAccountName $storageAccountName).Primary
153+
$accessKey_ForStorageAccount = `
154+
(Get-AzureRmStorageAccountKey `
155+
-Name $storageAccountName `
156+
-ResourceGroupName $resourceGroupName
157+
).Value[0];
164158
165-
"`$primaryAccessKey_ForStorageAccount = $primaryAccessKey_ForStorageAccount"
159+
"`$accessKey_ForStorageAccount = $accessKey_ForStorageAccount";
166160
167161
'Azure Storage Account cmdlet completed.
168162
Remainder of PowerShell .ps1 script continues.
169-
'
163+
';
170164
171165
#--------------- 6 -----------------------
172166
173-
174167
# The context will be needed to create a container within the storage account.
175168
176169
'Create a context object from the storage account and its primary access key.
177-
'
170+
';
178171
179172
$context = New-AzureStorageContext `
180173
-StorageAccountName $storageAccountName `
181-
-StorageAccountKey $primaryAccessKey_ForStorageAccount
182-
174+
-StorageAccountKey $accessKey_ForStorageAccount;
183175
184176
'Create a container within the storage account.
185-
'
186-
177+
';
187178
188179
$containerObjectInStorageAccount = New-AzureStorageContainer `
189180
-Name $containerName `
190-
-Context $context
191-
181+
-Context $context;
192182
193183
'Create a security policy to be applied to the SAS token.
194-
'
184+
';
195185
196186
New-AzureStorageContainerStoredAccessPolicy `
197187
-Container $containerName `
198188
-Context $context `
199189
-Policy $policySasToken `
200190
-Permission $policySasPermission `
201191
-ExpiryTime $policySasExpiryTime `
202-
-StartTime $policySasStartTime
192+
-StartTime $policySasStartTime;
203193
204194
'
205195
Generate a SAS token for the container.
206-
'
196+
';
207197
Try
208198
{
209199
$sasTokenWithPolicy = New-AzureStorageContainerSASToken `
210200
-Name $containerName `
211201
-Context $context `
212-
-Policy $policySasToken
202+
-Policy $policySasToken;
213203
}
214204
Catch
215205
{
216-
$Error[0].Exception.ToString()
206+
$Error[0].Exception.ToString();
217207
}
218208
219209
#-------------- 7 ------------------------
220210
221-
222211
'Display the values that YOU must edit into the Transact-SQL script next!:
223-
'
212+
';
224213
225-
"storageAccountName: $storageAccountName"
226-
"containerName: $containerName"
227-
"sasTokenWithPolicy: $sasTokenWithPolicy"
214+
"storageAccountName: $storageAccountName";
215+
"containerName: $containerName";
216+
"sasTokenWithPolicy: $sasTokenWithPolicy";
228217
229218
'
230219
REMINDER: sasTokenWithPolicy here might start with "?" character, which you must exclude from Transact-SQL.
231-
'
220+
';
232221
233222
'
234-
(Later, return here to delete your Azure Storage account. See the preceding - Remove-AzureStorageAccount -StorageAccountName $storageAccountName)'
223+
(Later, return here to delete your Azure Storage account. See the preceding Remove-AzureRmStorageAccount -Name $storageAccountName)';
235224
236225
'
237-
Now shift to the Transact-SQL portion of the two-part code sample!'
226+
Now shift to the Transact-SQL portion of the two-part code sample!';
238227
239228
# EOFile
240229
```
@@ -266,7 +255,7 @@ The PowerShell script printed a few named values when it ended. You must edit th
266255
### Transact-SQL code
267256

268257
```sql
269-
---- TODO: First, run the PowerShell portion of this two-part code sample.
258+
---- TODO: First, run the earlier PowerShell portion of this two-part code sample.
270259
---- TODO: Second, find every 'TODO' in this Transact-SQL file, and edit each.
271260

272261
---- Transact-SQL code for Event File target on Azure SQL Database.
@@ -544,6 +533,7 @@ For more info about accounts and containers in the Azure Storage service, see:
544533
* [Working with the Root Container](http://msdn.microsoft.com/library/azure/ee395424.aspx)
545534
* [Lesson 1: Create a stored access policy and a shared access signature on an Azure container](http://msdn.microsoft.com/library/dn466430.aspx)
546535
* [Lesson 2: Create a SQL Server credential using a shared access signature](http://msdn.microsoft.com/library/dn466435.aspx)
536+
* [Extended Events for Microsoft SQL Server](https://docs.microsoft.com/sql/relational-databases/extended-events/extended-events)
547537

548538
<!--
549539
Image references.

0 commit comments

Comments
 (0)