Start-SAPApplicationServer.ps1

<#PSScriptInfo

.DESCRIPTION Azure Automation runbook script to start an SAP Application Server.

.VERSION 0.0.3

.GUID ec9a6f30-b5ab-4ab0-bb29-0a6c072bdd26

.AUTHOR Goran Condric

.COMPANYNAME Microsoft

.COPYRIGHT (c) 2020 Microsoft . All rights reserved.

.TAGS Azure Automation SAP Application Server Start Runbook

.LICENSEURI

.PROJECTURI

.ICONURI

.EXTERNALMODULEDEPENDENCIES SAPAzurePowerShellModules

.REQUIREDSCRIPTS

.EXTERNALSCRIPTDEPENDENCIES

.RELEASENOTES
0.0.1: - Add initial version
0.0.2: - Add dedpendencies to SAPAzurePowerShellModules module
0.0.3: - Support for using a system-assigned managed identity for an Azure Automation account, and multiple Azure subscriptions

#>


#Requires -Module SAPAzurePowerShellModules

Param(
    [Parameter(Mandatory = $True)]
     [ValidateNotNullOrEmpty()]        
     [string] $ResourceGroupName,
           
     [Parameter(Mandatory = $True)]
     [ValidateNotNullOrEmpty()]        
     [string] $VMName,

    [Parameter(Mandatory = $False)] 
    [int] $SAPApplicationServerWaitTime = "300",

    [Parameter(Mandatory=$False)] 
    [bool] $ConvertDisksToPremium =  $False,

    [Parameter(Mandatory=$False)] 
    [bool] $PrintExecutionCommand = $False,

    [Parameter(Mandatory=$false, HelpMessage="Subscription ID. If null, the current subscription of automation account is used instead.")] 
    [ValidateLength(36,36)]
    [string] $SubscriptionId
    
)

Write-WithTime "Make sure to enable appropriate RBAC permissions to the system identity of this automation account. Otherwise, the runbook may fail."

Write-Output ""
Write-Output "You can enable system identity on the Azure automation account:"
Write-Output "1. Go to: Azure automation acccount -> Identity -> System asigned -> Status -> <On>"
Write-Output "2. Go to: Azure automation acccount -> Identity -> System asigned -> Permissions -> Azure role assignments -> Add role assignment ->"
Write-Output "Scope: 'Subscription'"
Write-Output "Subscription: <Chose your Subscription>"
Write-Output "Role: 'Owner'"
Write-Output ""
Write-Output "More info on: https://docs.microsoft.com/en-us/azure/automation/enable-managed-identity-for-automation#assign-role-to-a-system-assigned-managed-identity "
Write-Output ""

# Connect to Azure with Automation Account system-assigned managed identity
Write-WithTime " Connecting to Azure with Automation Account system-assigned managed identity ...."
Write-Output ""

# Ensures you do not inherit an AzContext in your runbook
Disable-AzContextAutosave -Scope Process | out-null

try {
    # Connect to Azure with system-assigned managed identity
    $AzureContext = (Connect-AzAccount -Identity).context
}
catch{
    Write-Error "There is no system-assigned user identity. Aborting."; 
    Write-Error  $_.Exception.Message
    exit
}

if ($SubscriptionId){
    Write-Output "Using specified Subscription ID '$SubscriptionId'."
    $SubscriptionId = $SubscriptionId.trim()
    Select-AzSubscription -SubscriptionId $SubscriptionId -ErrorVariable -notPresent  -ErrorAction SilentlyContinue -Tenant $AzureContext.Tenant
}

# set and store context
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext


$ResourceGroupName  = $ResourceGroupName.Trim()
$VMName             = $VMName.Trim()

# get start time
$StartTime = Get-Date

# Check if resource group exists. If $False exit
Confirm-AzResoureceGroupExist -ResourceGroupName $ResourceGroupName 

# Check if VM. If $False exit
Confirm-AzVMExist -ResourceGroupName $ResourceGroupName -VMName $VMName

# Get SAP Application Server data
$SAPApplicationServerData = Get-AzSAPApplicationInstanceData -ResourceGroupName $ResourceGroupName -VMName $VMName  

####################################
# Convert the disks to Premium_LRS
####################################
if($ConvertDisksToPremium){
    ConvertTo-AzVMManagedDisksToPremium -ResourceGroupName  $ResourceGroupName -VMName  $VMName    
}

# Start Azure VM
Start-AzVMAndPrintStatus  -ResourceGroupName $ResourceGroupName -VMName $VMName

# Start SAP Application Server
Start-AzSAPApplicationServer  -ResourceGroupName $ResourceGroupName -VMName $VMName -WaitTime $SAPApplicationServerWaitTime -PrintExecutionCommand $PrintExecutionCommand 

# Get end time
$EndTime = Get-Date
$ElapsedTime = $EndTime - $StartTime

Write-Output ""
Write-Output "Job succesfully finished."
Write-Output ""

Write-Output "SUMMARY:"
If($ConvertDisksToPremium){
    Write-Output " - All disks set to 'Premium_LRS' type."
}else{
    Write-Output " - All disks types are NOT changed."
}
Write-Output " - Virtual machine(s) started."
Write-Output " - SAP Application Server with SAP SID '$($SAPApplicationServerData.SAPSID)' and instance number '$($SAPApplicationServerData.SAPApplicationInstanceNumber)' on VM '$VMName' and Azure resource group '$ResourceGroupName' started."
Write-Output ""

Write-Output "[INFO] Total time : $($ElapsedTime.Days) days, $($ElapsedTime.Hours) hours, $($ElapsedTime.Minutes) minutes, $($ElapsedTime.Seconds) seconds, $($ElapsedTime.Seconds) milliseconds."