Skip to content

Commit

Permalink
Updated to AzureAutomation Runbooks (#16)
Browse files Browse the repository at this point in the history
Updated the Modules from AzureRM to Az (as AzureRM are legacy ones)
And moved from Run as account to Managed Identity as those would be deprecated as of this FY end
  • Loading branch information
NandanHegde15 authored Mar 21, 2023
1 parent a12cd9a commit 02a329b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 53 deletions.
28 changes: 6 additions & 22 deletions AzureAutomation/PauseAzureAS.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,20 @@ param(



$connectionName = "AzureRunAsConnection"
try
{
# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection=Get-AutomationConnection -Name $connectionName

"Logging in to Azure..."
Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
Connect-AzAccount -Identity
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
Write-Error -Message $_.Exception
throw $_.Exception
}
$ErrorActionPreference = "Stop";



$ErrorActionPreference = "Stop";

# Get old status
$OldAsSetting = Get-AzureRmAnalysisServicesServer -ResourceGroupName $ResourceGroupName -Name $ServerName
$OldAsSetting = Get-AzAnalysisServicesServer -ResourceGroupName $ResourceGroupName -Name $ServerName
$OldStatus = $OldAsSetting.State


Expand All @@ -51,7 +35,7 @@ if($OldStatus -eq "Paused")
}
else
{
$null = Suspend-AzureRmAnalysisServicesServer -ResourceGroupName $ResourceGroupName -Name $ServerName
$null = Suspend-AzAnalysisServicesServer -ResourceGroupName $ResourceGroupName -Name $ServerName
Write-Output "Pausing $($ServerName) Completed. Current status: $($OldStatus)"

}
Expand Down
26 changes: 16 additions & 10 deletions AzureAutomation/ProcessAzureAS.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
The runbook will process full the specified database. It will temporarily open the firewall to the current Azure Automation public IP.
The Azure Automation runas account needs Contributor permissions on the Azure Analysis Services server and inside SSMS you need to grant that runas account server administrator permissions.
The script requires the following Modules be imported in your Azure Automation account:
AzureRM.AnalysisServices
Az.AnalysisServices
PackageManagement
.PARAMETER serverName
The name of your Azure Analysis Services instance. This is not the full asazure:// URI. This is just the final section saying the name of your server.
Expand All @@ -31,19 +31,25 @@ param
[string] $CubeDatabaseName
)

$ErrorActionPreference = "Stop";
try
{
"Logging in to Azure..."
Connect-AzAccount -Identity
}
catch {
Write-Error -Message $_.Exception
throw $_.Exception
}

$runAsConnectionProfile = Get-AutomationConnection -Name "AzureRunAsConnection"
Add-AzureRmAccount -ServicePrincipal -TenantId $runAsConnectionProfile.TenantId `
-ApplicationId $runAsConnectionProfile.ApplicationId -CertificateThumbprint $runAsConnectionProfile.CertificateThumbprint | Out-Null
$ErrorActionPreference = "Stop";

$asServer = Get-AzureRmAnalysisServicesServer -ResourceGroupName $resourceGroupName -Name $serverName
$asServer = Get-AzAnalysisServicesServer -ResourceGroupName $resourceGroupName -Name $serverName

"Current Azure AS status: $($asServer.State)"

if ($asServer.State -eq "Paused")
{
$asServer | Resume-AzureRmAnalysisServicesServer -Verbose
$asServer | Resume-AzAnalysisServicesServer -Verbose
}

if ($asServer.FirewallConfig -ne $null)
Expand All @@ -64,9 +70,9 @@ if ($asServer.FirewallConfig -ne $null)
$ipinfo = Invoke-RestMethod http://ipinfo.io/json

#add a new AzureAutomation firewall rule
$newRule = New-AzureRmAnalysisServicesFirewallRule -FirewallRuleName "AzureAutomation" -RangeStart $ipinfo.ip -RangeEnd $ipinfo.ip
$newRule = New-AzAnalysisServicesFirewallRule -FirewallRuleName "AzureAutomation" -RangeStart $ipinfo.ip -RangeEnd $ipinfo.ip
$asServer.FirewallConfig.FirewallRules.Add($newRule)
Set-AzureRmAnalysisServicesServer -ResourceGroupName $resourceGroupName -Name $serverName -FirewallConfig $asServer.FirewallConfig
Set-AzAnalysisServicesServer -ResourceGroupName $resourceGroupName -Name $serverName -FirewallConfig $asServer.FirewallConfig

"Updated Azure AS firewall to allow current Azure Automation Public IP: " + $ipinfo.ip
}
Expand Down Expand Up @@ -128,6 +134,6 @@ if ($asServer.FirewallConfig -ne $null)
#reset firewall to the state it was in before this script started
$asServer.FirewallConfig.FirewallRules.Clear()
$asServer.FirewallConfig.FirewallRules.AddRange($rulesBackup)
Set-AzureRmAnalysisServicesServer -ResourceGroupName $resourceGroupName -Name $serverName -FirewallConfig $asServer.FirewallConfig
Set-AzAnalysisServicesServer -ResourceGroupName $resourceGroupName -Name $serverName -FirewallConfig $asServer.FirewallConfig
"Reset Azure AS firewall rules"
}
28 changes: 7 additions & 21 deletions AzureAutomation/ResumeAzureAS.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,22 @@ param(



$connectionName = "AzureRunAsConnection"

try
{
# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection=Get-AutomationConnection -Name $connectionName

"Logging in to Azure..."
Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
Connect-AzAccount -Identity
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
Write-Error -Message $_.Exception
throw $_.Exception
}
$ErrorActionPreference = "Stop";


$ErrorActionPreference = "Stop";


# Get old status
$OldAsSetting = Get-AzureRmAnalysisServicesServer -ResourceGroupName $ResourceGroupName -Name $ServerName
$OldAsSetting = Get-AzAnalysisServicesServer -ResourceGroupName $ResourceGroupName -Name $ServerName
$OldStatus = $OldAsSetting.State


Expand All @@ -51,7 +37,7 @@ if($OldStatus -eq "Succeeded")
}
else
{
$null = Resume-AzureRmAnalysisServicesServer -ResourceGroupName $ResourceGroupName -Name $ServerName
$null = Resume-AzAnalysisServicesServer -ResourceGroupName $ResourceGroupName -Name $ServerName
Write-Output "Resuming $($ServerName) Completed. Current status: $($OldStatus)"

}
Expand Down

0 comments on commit 02a329b

Please sign in to comment.