Created
September 9, 2016 03:39
-
-
Save MasayukiOzawa/4b71db19ebdd469cdac311c8e0db33c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://azure.microsoft.com/ja-jp/documentation/articles/sql-database-develop-direct-route-ports-adonet-v12/ | |
# https://blogs.msdn.microsoft.com/sqlcat/2016/09/08/connect-to-azure-sql-database-v12-via-redirection/ | |
# https://msdn.microsoft.com/en-us/library/azure/mt604439.aspx | |
$adTenant = "hogehoge.onmicrosoft.com" | |
$subscriptionid = "xxxxxxxxxxx" | |
$resourgegroup = "groupname" | |
$servername = "servername" | |
$user = "hogehoge@hogehoge.onmicrosoft.com" | |
$password = "M@sterEr0s" | |
# 認証ヘッダーの作成 | |
$clientId = "1950a258-227b-4e31-a9cf-717495945fc2" | |
$redirectUri = "urn:ietf:wg:oauth:2.0:oob" | |
$resourceAppIdURI = "https://management.core.windows.net/" | |
$authority = "https://login.windows.net/$adTenant" | |
$programDir = ${env:ProgramFiles(x86)} | |
if(!$programDir) | |
{ | |
$programDir = ${env:ProgramFiles} | |
} | |
$adal = "$programDir\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Services\Microsoft.IdentityModel.Clients.ActiveDirectory.dll" | |
$adalforms = "$programDir\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Services\Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll" | |
Add-Type -Path $adal | |
Add-Type -Path $adalforms | |
$password = ConvertTo-SecureString $password -asPlainText -Force | |
$cred = New-Object System.Management.Automation.PSCredential $user,$password | |
Add-AzureRMAccount -Credential $cred | |
$authContext = New-Object -TypeName "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority | |
$creds = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.UserCredential" -ArgumentList $user,$password | |
$authResult = $authContext.AcquireToken($resourceAppIdURI, $clientId, $creds) | |
# Create Authorization Header | |
$authHeader = $authResult.CreateAuthorizationHeader() | |
# 現状設定の取得 | |
$method = "GET" | |
$apiurl = "https://management.azure.com/subscriptions/$subscriptionid/resourceGroups/$resourgegroup/providers/Microsoft.Sql/servers/$servername/connectionPolicies/Default?api-version=2014-04-01-preview" | |
$requestHeader = @{ | |
"Authorization" = $authHeader | |
"Accept" = "application/json" | |
} | |
$contentType = "application/json;charset=utf-8" | |
Invoke-RestMethod -Verbose -Uri $apiurl -Method $method -Headers $requestHeader -ContentType $contentType | |
# 設定の変更 (Default / Proxy / Redirect) | |
$method = "PUT" | |
$requestHeader = @{ | |
"Authorization" = $authHeader; | |
"Accept" = "application/json" | |
} | |
$contentType = "application/json;charset=utf-8" | |
$json = @" | |
{ | |
"properties": { | |
"connectionType": "Redirect" | |
} | |
} | |
"@ | |
[byte[]]$requestBody = [System.Text.Encoding]::UTF8.GetBytes($json) | |
$apiurl = "https://management.azure.com/subscriptions/$subscriptionid/resourceGroups/$resourgegroup/providers/Microsoft.Sql/servers/$servername/connectionPolicies/Default?api-version=2014-04-01-preview" | |
Invoke-RestMethod -Verbose -Uri $apiurl -Method $method -Headers $requestHeader -ContentType $contentType -Body $requestBody |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment