-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrain&Pause.ps1
More file actions
47 lines (38 loc) · 1.43 KB
/
Drain&Pause.ps1
File metadata and controls
47 lines (38 loc) · 1.43 KB
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
# Initialize variables and logging information
$clusterMaintenanceFolder = "C:\ClusterMaintenance"
$logFileName = "$clusterMaintenanceFolder\ClusterMaintenance_$(Get-Date -Format 'yyyyMMdd').log"
# Function to write to log
function Write-Log {
param (
[Parameter(Mandatory = $true)]
[string]$Message
)
Add-Content -Path $logFileName -Value "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - $Message"
}
# Check if the logging directory exists, create it if not
if (-not (Test-Path $clusterMaintenanceFolder)) {
New-Item -Path $clusterMaintenanceFolder -ItemType Directory | Out-Null
}
# Fetch the cluster name and node name
$clusterName = (Get-Cluster).name
$nodeName = [System.Environment]::MachineName
# Add this information to the log
Write-Log "Cluster Name: $clusterName"
Write-Log "Node Name: $nodeName"
try {
# Set the error action preference to stop
$ErrorActionPreference = 'Stop'
# Pause the node and drain the roles
Write-Log "Putting node $nodeName in pause mode..."
Suspend-ClusterNode -Name $nodeName -Drain -wait | Out-Null
Write-Log "Node $nodeName is now in pause mode and roles have been drained."
}
catch {
Write-Log "Error occurred while putting node $nodeName in pause mode: $_"
exit 1
}
finally {
# Reset the error action preference if needed
$ErrorActionPreference = 'Continue'
}
Write-Log "Cluster maintenance operations completed successfully for node $nodeName."