Created
May 29, 2018 01:31
-
-
Save DataSic/e45de57120ecabd4d5b5e4ac621ceebc to your computer and use it in GitHub Desktop.
Azure Automation Runbook - JIT 'Allow access to Azure services'
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
<# | |
JIT 'Allow access to Azure services' for runbook on Azure Sql Database/Datawarehouse | |
#> | |
try | |
{ | |
# Init variables | |
$resourceGroupName = Get-AutomationVariable -Name "ResourceGroup" | |
$serverName = Get-AutomationVariable -Name "ServerName" | |
$firewallRuleName = "AllowAllWindowsAzureIps" | |
# Use built-in account | |
$servicePrincipalConnection = Get-AutomationConnection -Name "AzureRunAsConnection" | |
# Log into Azure with AzureRunAsConnection | |
$addAzureRmAccountSplat = @{ | |
CertificateThumbprint = $servicePrincipalConnection.CertificateThumbprint | |
TenantId = $servicePrincipalConnection.TenantId | |
ServicePrincipal = $true | |
ApplicationId = $servicePrincipalConnection.ApplicationId | |
} | |
$null = Add-AzureRmAccount @addAzureRmAccountSplat | |
# Enable 'Allow Access To Azure Services' | |
$newAzureRmSqlServerFirewallRuleSplat = @{ | |
ResourceGroupName = $resourceGroupName | |
ServerName = $serverName | |
FirewallRuleName = $firewallRuleName | |
StartIpAddress = "0.0.0.0" | |
EndIpAddress = "0.0.0.0" | |
} | |
$null = New-AzureRmSqlServerFirewallRule @newAzureRmSqlServerFirewallRuleSplat | |
# Perform task here | |
# Disable 'Allow Access To Azure Services' | |
$removeAzureRmSqlServerFirewallRuleSplat = @{ | |
ResourceGroupName = $resourceGroupName | |
ServerName = $serverName | |
FirewallRuleName = $firewallRuleName | |
Force = $true | |
} | |
$null = Remove-AzureRmSqlServerFirewallRule @removeAzureRmSqlServerFirewallRuleSplat | |
} | |
catch { | |
Write-Error -Message $_.Exception | |
throw $_.Exception | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment