Skip to content

Instantly share code, notes, and snippets.

@DataSic
Created May 29, 2018 01:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DataSic/e45de57120ecabd4d5b5e4ac621ceebc to your computer and use it in GitHub Desktop.
Save DataSic/e45de57120ecabd4d5b5e4ac621ceebc to your computer and use it in GitHub Desktop.
Azure Automation Runbook - JIT 'Allow access to Azure services'
<#
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