-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathConnect Azure SQL.ps1
54 lines (42 loc) · 1.71 KB
/
Connect Azure SQL.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
$clientID = "your Client ID"
$Clientsecret = "your Secret"
$tenantID = "your Tenant ID"
$DB="Database Name"
$Serverinstance="yourSQLserver.database.windows.net"
$Query="SELECT * FROM [dbo].[Table] order by Date desc"
$Modules = @("sqlServer")
foreach ($Module in $Modules) {
if (Get-Module -ListAvailable -Name $Module) {
# "Module is already installed: $Module"
}
else {
W# "Module is not installed, try simple method: $Module"
try {
Install-Module $Module -Force -Confirm:$false
# "Module was installed the simple way: $Module"
}
catch {
# "Module is not installed, try the advanced way: $Module"
try {
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
Install-Module $Module -Force -Confirm:$false
# "Module was installed the advanced way: $Module"
}
catch {
# "could not install module: $Module"
}
}
}
# "Import Module: $Module"
Import-module $Module
}
#Get Token for autentication
$request = 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"
$access_token = $request.access_token
#Run SQL Command with accessToken
Invoke-Sqlcmd -ServerInstance $Serverinstance -Database $DB -AccessToken $access_token -query $Query
y