-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStop-SAPSystemUsingACSS.ps1
136 lines (121 loc) · 6.09 KB
/
Stop-SAPSystemUsingACSS.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
<#
.SYNOPSIS
Used to stop SAP Virtual Instance
.DESCRIPTION
Runbook checks the status of SAP VIS registration and stops it using VIS commands. Requires Az.Workloads PS module.
.PARAMETER $virtualInstanceName
Name of the VIS to stop. This is the SAP System SID
.PARAMETER $virtualInstanceSubscription
Subscription ID for the VIS to stop. Defaults to automation account subscription
.NOTES
Author: Karthik Venkatraman
#>
param(
# Name of the SAP Virtual Instance i.e. SAP System SID
[Parameter(Mandatory = $true)]
[String]$virtualInstanceName,
# Subscription for SAP Virtual Instance
[Parameter(Mandatory = $false)]
[String]$virtualInstanceSubscription
)
try {
Disable-AzContextAutosave -Scope Process
# Connect to Azure with system-assigned managed identity
$AzureContext = (Connect-AzAccount -Identity).context
# set and store context
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext
Write-Output "Working on subscription $($AzureContext.Subscription) and tenant $($AzureContext.Tenant)"
if (!$virtualInstanceSubscription) {
$virtualInstanceSubscription = $($AzureContext.Subscription)
}
# Get VIS Status for the SID
$sapVIS = Get-AzWorkloadsSapVirtualInstance -SubscriptionId $virtualInstanceSubscription | Where-Object { $_.Name -eq $virtualInstanceName }
if ($sapVIS.State -eq "RegistrationComplete") {
##-and ##$sapVIS.Environment -ne "Production" )
Write-Output "SAP Virtual Instance $($virtualInstanceName) is registerd and environment is not Production. Proceeding to stop SAP System"
}
else {
Write-Error "SAP System $($virtualInstanceName) is not registered or environment is Production. Please check if the environment is correct or register the system and try again" -ErrorAction Stop
}
# Check if SAP System is already stopped
if ($sapVIS.Status -eq "Running" -or $sapVIS.SapSystem.Status -eq "PartiallyRunning") {
Write-Output "SAP System $($virtualInstanceName) is running. Proceeding to stop SAP System"
# Stop SAP VIS
Write-Output "Stopping SAP Virtual Instance for SID $virtualInstanceName"
$appstoprc = Stop-AzWorkloadsSapVirtualInstance -InputObject $sapVIS.Id
if ($appstoprc.Status -ne 'Succeeded') {
Write-Error "Failed to Stop Virtual Instance for SID $virtualInstanceName" -ErrorAction Stop
}
else {
Write-Output "Successfully stopped SAP System $virtualInstanceName"
}
# Stop DB instance
Write-Output "Checking DB Type for $($virtualInstanceName)"
$dbVIS = Get-AzWorkloadsSapDatabaseInstance -SapVirtualInstanceName $($sapVIS.Name) -ResourceGroupName $($sapVIS.ResourceGroupName)
if ($dbVIS.DatabaseType -ne "hdb") {
Write-Output "DB Type is not HANA. Cannot be stopped by ACSS"
Write-Output "Add additonal script to Stop DB instance for DB Type $($dbVIS.DatabaseType)"
}
else {
Write-Output "Stopping SAP HANA DB for SID $virtualInstanceName"
$dbstoprc = Stop-AzWorkloadsSapDatabaseInstance -InputObject $dbVIS.Id
if ($dbstoprc.Status -ne 'Succeeded') {
Write-Error "Failed to Stop DB Instance for SID $virtualInstanceName" -ErrorAction Stop
}
else {
Write-Output "Successfully stopped Database instance for System $virtualInstanceName"
}
}
# Final display of VIS status
$updatedVIS = Get-AzWorkloadsSapVirtualInstance -SubscriptionId $virtualInstanceSubscription | Where-Object { $_.Name -eq $virtualInstanceName }
$updatedVIS | Format-Table -AutoSize -Property Name, ResourceGroupName, Health, Environment, ProvisioningState, Location, Status
}
else {
Write-Output "SAP System $virtualInstanceName is already Stopped. Proceeding with VM shutdown"
}
# Stop VMs associated with SAP System
Write-Output "Stop VMs associated with SAP System $($virtualInstanceName)"
$vmList = [System.Collections.ArrayList]@()
foreach ($dbVM in $dbVIS.VMDetail) {
$vmList.Add($dbVM)
}
$scsVIS = Get-AzWorkloadsSapCentralInstance -SapVirtualInstanceName $($sapVIS.Name) -ResourceGroupName $($sapVIS.ResourceGroupName)
foreach ($scsVM in $scsVIS.VMDetail) {
$vmList.Add($scsVM)
}
$appVIS = Get-AzWorkloadsSapApplicationInstance -SapVirtualInstanceName $($sapVIS.Name) -ResourceGroupName $($sapVIS.ResourceGroupName)
foreach ($appVM in $appVIS.VMDetail) {
$vmList.Add($appVM)
}
Write-Output "Stopping below VMs for SAP System $($virtualInstanceName)"
$vmList | Format-Table -AutoSize -Property virtualMachineId
$vmList | ForEach-Object -ThrottleLimit 10 -Parallel {
$rc = Stop-AzVM -Id $_.virtualMachineId -Force
Write-Verbose $rc -Verbose
}
#Check status of VMs before Stopping SAP System
$failedVMs = [System.Collections.ArrayList]@()
$successVMs = [System.Collections.ArrayList]@()
Write-Output "Getting Final Status of VMs for SAP System $($virtualInstanceName)"
foreach ($vm in $vmList) {
$status = Get-AzVM -ResourceId $($vm.virtualMachineId) -Status
if ($($status.Statuses[1].DisplayStatus) -contains 'VM Running') {
[void]$failedVMs.Add($status)
}
else {
[void]$successVMs.Add($status)
}
}
if ($failedVMs.Count -gt 0) {
Write-Error "Failed to stop VMs for SAP System $($virtualInstanceName). See list below" -ErrorAction Stop
$failedVMs | Format-Table -AutoSize -Property Name, ResourceGroupName, @{label = 'VMStatus'; Expression = { $_.Statuses[1].DisplayStatus } }
}
else {
Write-Output "Successfully stopped VMs for SAP System $($virtualInstanceName)"
$successVMs | Format-Table -AutoSize -Property Name, ResourceGroupName, @{label = 'VMStatus'; Expression = { $_.Statuses[1].DisplayStatus } }
}
}
catch {
Write-Error $_.Exception.Message
"SAP System $virtualInstanceName stop failed. See previous error messages for details"
}