Start-Stop-Automation/Automation-Backend/Runbooks/Stop-SAPHANA.ps1 (104 lines of code) (raw):
<#PSScriptInfo
.DESCRIPTION Azure Automation Runbook Script to stop an SAP HANA DB.
.VERSION 0.0.3
.GUID 7e64d4d0-abb6-42d5-af93-9eeb3a1e026e
.AUTHOR Goran Condric
.COMPANYNAME Microsoft
.COPYRIGHT (c) 2020 Microsoft . All rights reserved.
.TAGS Azure Automation SAP System Stop HANA Runbook
.LICENSEURI
.PROJECTURI
.ICONURI
.EXTERNALMODULEDEPENDENCIES SAPAzurePowerShellModules
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
0.0.1: - Add initial version
0.0.3: - Add dedpendencies to SAPAzurePowerShellModules module
#>
#Requires -Module SAPAzurePowerShellModules
Param(
[Parameter(Mandatory=$True, HelpMessage="SAP System <SID>. 3 characters , starts with letter.")]
[ValidateLength(3,3)]
[string] $SAPHANASID,
[Parameter(Mandatory=$False)]
[bool] $ConvertDisksToStandard = $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
)
# Deprecated due to using System Managed Identity
#$connection = Get-AutomationConnection -Name AzureRunAsConnection
#Add-AzAccount -ServicePrincipal -Tenant $connection.TenantID -ApplicationId $connection.ApplicationID -CertificateThumbprint $connection.CertificateThumbprint
# Connect to Azure with Automation Account system-assigned managed identity
# Ensure that you do not inherit an AZ Context in your runbook
Disable-AzContextAutosave -Scope Process | out-null
# Connect using Managed Service Identity
try {
$AzureContext = (Connect-AzAccount -Identity -WarningAction Ignore).context
}
catch{
Write-Output "There is no system-assigned user identity. Aborting.";
Write-Error $_.Exception.Message
exit
}
if ($SubscriptionId){
$SubscriptionId = $SubscriptionId.trim()
Select-AzSubscription -SubscriptionId $SubscriptionId -ErrorVariable -notPresent -ErrorAction SilentlyContinue -Tenant $AzureContext.Tenant
}
# get start time
$StartTime = Get-Date
$SAPHANASID = $SAPHANASID.Trim()
#Test if Tag 'SAPHANASID' with value $SAPHANASID exist. If not exit
Test-AzSAPHANASIDTagExist -SAPHANASID $SAPHANASID
# Get DBMS VMs
$SAPSIDDBMSVMs = Get-AzSAPHANAInstances -SAPHANASID $SAPHANASID
# List SAP DBMS layer VM(s)
Write-Output ""
Write-WithTime "SAP HANA DBMS VM(s):"
Show-AzSAPSIDVMDBMSInstances -SAPVMs $SAPSIDDBMSVMs
###################
# Stop DBMS
###################
# get DBMS Status
Write-Output ""
Get-AzDBMSStatus -SAPSIDDBMSVMs $SAPSIDDBMSVMs -PrintExecutionCommand $PrintExecutionCommand
# Start DBMS
Write-Output ""
Stop-AzDBMS -SAPSIDDBMSVMs $SAPSIDDBMSVMs -PrintExecutionCommand $PrintExecutionCommand
# get DBMS Status
Write-Output ""
Get-AzDBMSStatus -SAPSIDDBMSVMs $SAPSIDDBMSVMs -PrintExecutionCommand $PrintExecutionCommand
###################
# Stop VMs
###################
Write-WithTime "Stopping SAP HANA VM(s) ..."
Write-Output ""
Stop-AzVMTagAndCheckVMStatus -SAPVMs $SAPSIDDBMSVMs -SAPInstanceType "SAP_DBMS"
####################################
# Convert the disks to Standard_LRS
####################################
if($ConvertDisksToStandard){
Convert-AzALLSAPVMsCollectionManagedDisksToStandard -SAPVMs $SAPSIDDBMSVMs
}
# get end time
$EndTime = Get-Date
$ElapsedTime = $EndTime - $StartTime#
###################
# SUMMARY
###################
Write-Output ""
Write-Output "Job succesfully finished."
Write-Output ""
Write-Output "SUMMARY:"
Write-Output " - SAP HANA '$SAPHANASID' DBMS stopped."
Write-Output " - Virtual machine(s) are stopped."
If($ConvertDisksToStandard){
Write-Output " - All disks set to 'Standard_LRS' type."
}else{
Write-Output " - All disks types are NOT changed."
}
Write-Output ""
Write-Output "[INFO] Total time : $($ElapsedTime.Days) days, $($ElapsedTime.Hours) hours, $($ElapsedTime.Minutes) minutes, $($ElapsedTime.Seconds) seconds, $($ElapsedTime.Seconds) milliseconds."