Stop-SAPSystem.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183

<#PSScriptInfo
 
.DESCRIPTION Azure Automation Runbook Script to stop an SAP system.
 
.VERSION 0.0.4
 
.GUID e67257ff-d964-4403-8e39-7a5d47f725b3
 
.AUTHOR Goran Condric
 
.COMPANYNAME Microsoft
 
.COPYRIGHT (c) 2020 Microsoft . All rights reserved.
 
.TAGS Azure Automation SAP System Stop Runbook
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES SAPAzurePowerShellModules
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
0.0.1: - Add initial version
0.0.3: - Add dedpendencies to SAPAzurePowerShellModules module
0.0.4: - Add functionality for Java systems
#>


#Requires -Module SAPAzurePowerShellModules

Param(
    
[Parameter(Mandatory=$True, HelpMessage="SAP System <SID>. 3 characters , starts with letter.")] 
[ValidateLength(3,3)]
[string] $SAPSID,

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

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

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

)

# Connect to Azure
$connection = Get-AutomationConnection -Name AzureRunAsConnection
Add-AzAccount  -ServicePrincipal -Tenant $connection.TenantID -ApplicationId $connection.ApplicationID -CertificateThumbprint $connection.CertificateThumbprint 

# get start time
$StartTime = Get-Date

$SAPSID = $SAPSID.Trim()

# Connect to Azure
$connection = Get-AutomationConnection -Name AzureRunAsConnection
Add-AzAccount  -ServicePrincipal -Tenant $connection.TenantID -ApplicationId $connection.ApplicationID -CertificateThumbprint $connection.CertificateThumbprint 

# get start time
$StartTime = Get-Date

$SAPSID  = $SAPSID.Trim()

#Test if Tag 'SAPSystemSID' with value $SAPSID exist. If not exit
Test-AzSAPSIDTagExist -SAPSID $SAPSID

# Get SAP Appplication VMs
$SAPSIDApplicationVMs  = Get-AzSAPApplicationInstances -SAPSID $SAPSID

Write-Output ""

# List SAP Application layer VM
Write-Output ""
Write-WithTime "SAP Application layer VMs:"
Show-AzSAPSIDVMApplicationInstances -SAPVMs $SAPSIDApplicationVMs

# Get DBMS VMs
$SAPSIDDBMSVMs  = Get-AzSAPDBMSInstances -SAPSID $SAPSID

# List SAP DBMS layer VM(s)
Write-Output ""
Write-WithTime "SAP DBMS layer VM(s):"
Show-AzSAPSIDVMDBMSInstances -SAPVMs $SAPSIDDBMSVMs

###################
# Stop SAP
###################

# Get SAP System Status
Write-Output ""
Get-AzSAPSystemStatus -SAPSIDApplicationVMs $SAPSIDApplicationVMs -PrintExecutionCommand $PrintExecutionCommand 

# Stop SAP system
Write-Output ""
Stop-AzSAPSystem  -SAPSIDApplicationVMs $SAPSIDApplicationVMs -SoftShutdownTimeInSeconds $SoftShutdownTimeInSeconds -PrintExecutionCommand $PrintExecutionCommand  

# Get SAP System Status
Write-Output ""
Get-AzSAPSystemStatus -SAPSIDApplicationVMs $SAPSIDApplicationVMs -PrintExecutionCommand $PrintExecutionCommand 

###################
# Stop DBMS
###################

# get DBMS Status
Write-Output ""
Get-AzDBMSStatus -SAPSIDDBMSVMs $SAPSIDDBMSVMs -PrintExecutionCommand $PrintExecutionCommand

# Stop 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 VMs ...."

# Stop ABAP Application Servers (Dialog Instances) VMs
Write-Output ""
Stop-AzVMTagAndCheckVMStatus -SAPVMs $SAPSIDApplicationVMs -SAPInstanceType "SAP_D"

# Stop Java Application Servers VMs
Write-Output ""
Stop-AzVMTagAndCheckVMStatus -SAPVMs $SAPSIDApplicationVMs -SAPInstanceType "SAP_J"

# Stop ABAP ASCS Instance VMs
Write-Output ""
Stop-AzVMTagAndCheckVMStatus -SAPVMs $SAPSIDApplicationVMs -SAPInstanceType "SAP_ASCS"

# Stop ABAP DVEBMGS Instance VM
Stop-AzVMTagAndCheckVMStatus -SAPVMs $SAPSIDApplicationVMs -SAPInstanceType "SAP_DVEBMGS"

# Stop Java SCS Instance VMs
Write-Output ""
Stop-AzVMTagAndCheckVMStatus -SAPVMs $SAPSIDApplicationVMs -SAPInstanceType "SAP_SCS"

# Stop DBMS VMs
Write-Output ""
Stop-AzVMTagAndCheckVMStatus -SAPVMs $SAPSIDDBMSVMs -SAPInstanceType "SAP_DBMS"

####################################
# Convert the disks to Standard_LRS
####################################

if($ConvertDisksToStandard){
    Convert-AzALLSAPSystemVMsCollectionManagedDisksToStandard -SAPSIDApplicationVMs $SAPSIDApplicationVMs -SAPSIDDBMSVMs $SAPSIDDBMSVMs
}

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

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

Write-Output "SUMMARY:"
If($ConvertDisksToStandard){
    Write-Output " - All disks set to 'Standard_LRS' type."
}else{
    Write-Output " - All disks types are NOT changed."
}
Write-Output " - Virtual machine(s) stopped."
Write-Output " - DBMS stopped."
Write-Output " - SAP system '$SAPSID' stopped."
Write-Output ""


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