Tag-SAPStandaloneHANA.ps1

<#PSScriptInfo

.DESCRIPTION Azure Automation runbook script to tag an standalone SAP HANA DB.

.VERSION 0.0.3

.GUID a1d371a7-7248-4af5-9735-4bbf1485e7e8

.AUTHOR Goran Condric

.COMPANYNAME Microsoft

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

.TAGS Azure Automation SAP HANA Tag Standalone System 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=$True, HelpMessage="SAP HANA <SID>. 3 characters , starts with letter.")] 
[ValidateLength(3,3)]
[string] $SAPHANASID,

[Parameter(Mandatory=$True, HelpMessage="HANA Instance Number")] 
[ValidateLength(1, 2)]
[string] $SAPHANAINstanceNumber,

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

)

# Connect to Azure
Write-Output "Connection to Azure ...."

# 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()
$SAPHANASID             = $SAPHANASID.Trim()
$SAPHANAINstanceNumber  = $SAPHANAINstanceNumber.Trim()

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

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

# Tag standalone SAP HANA on ONE VM
New-AzSAPStandaloneHANATags -ResourceGroupName $ResourceGroupName -VMName $VMName -SAPHANASID $SAPHANASID -SAPHANAINstanceNumber $SAPHANAINstanceNumber

Write-WithTime "Standalone SAP HANA use case."
Write-WithTime "Tagging of VM '$VMName' in resource group '$ResourceGroupName' with tags: SAPHANASID='$SAPHANASID'; SAPHANAINstanceNumber='$SAPHANAINstanceNumber'; SAPDBMSType='HANA' done."