-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConnectToAzureSQLDB-SP.ps1
More file actions
20 lines (16 loc) · 968 Bytes
/
Copy pathConnectToAzureSQLDB-SP.ps1
File metadata and controls
20 lines (16 loc) · 968 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Import-Module SQLServer -Verbose
$ClientID = "" # enter application id that corresponds to the Service Principal" # Do not confuse with its display name
$TenantID = "" # enter the tenant ID of the Service Principal
$ClientSecret = "" #enter the secret associated with the Service Principal
$RequestToken = Invoke-RestMethod -Method POST `
-Uri "https://login.microsoftonline.com/$TenantID/oauth2/token"`
-Body @{ resource="https://database.windows.net/"; grant_type="client_credentials"; client_id=$ClientID; client_secret=$ClientSecret }`
-ContentType "application/x-www-form-urlencoded"
$AccessToken = $RequestToken.access_token
#SQL server, database & table information
$SQLServer = "ServerName.database.windows.net"
$DBName = "DatbaseName"
$DBTableName1 = "dbo.TableName"
#Database query
$Query = "Select * from $DBTableName1"
Invoke-Sqlcmd -ServerInstance $SQLServer -Database $DBName -AccessToken $AccessToken -Query $Query