Modules/Start-SCOMAgentMaintenanceMode.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
Function�Start-SCOMAgentMaintenanceMode
{
    <#�
            .SYNOPSIS�
            This�function�triggers maintenance mode on the current SCOM agent monitored server. It will prevent to set SCOM management server into maintenance mode.�
            Limitation: Works only with SCOM 2016.

            .PARAMETER��Reason�
            Defined reason for the maintenance mode
 
            .PARAMETER��Comment�
            Optional parameter which can be set to add a comment.
 
            .PARAMETER Duration
            Minutes to put system into mainteance mode

            .EXAMPLE�
            PS�C:\>�Start-SCOMAgentMaintenanceMode -Reason PlannedHardwareInstallation -Comment "Need to change HW" -Duration 60
    #>

    [CmdletBinding()]
    param�(
        [ValidateSet(
                'PlannedOther',
                'UnplannedOther',
                'PlannedHardwareMaintenance',
                'UnplannedHardwareMaintenance',
                'PlannedHardwareInstallation',
                'UnplannedHardwareInstallation',
                'PlannedOperatingSystemReconfiguration',
                'UnplannedOperatingSystemReconfiguration',
                'PlannedApplicationMaintenance',
                'UnplannedApplicationMaintenance',
                'ApplicationInstallation',
                'ApplicationUnresponsive',
                'ApplicationUnstable',
                'SecurityIssue',
                'LossOfNetworkConnectivity'
        )] 
        [Parameter(Mandatory = $false)]
        [string]$Reason = 'PlannedOther',
        [Parameter(Mandatory = $false)]
        [string]$Comment = 'No comment set',
        [Parameter(Mandatory = $true)]
        [double]$Duration 
    )

    Try
    {  
        $MinimumDurationInMins = 5.0 
        $ParamSeparator = '|'
        $CommentSeparator = ':'
        $ExtraTimeInMinutes = 8.0
        $RegistryKey = 'HKLM:\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\MaintenanceMode'
        $Valid = $null
        $Record = $null

        $Current = [System.Security.Principal.WindowsIdentity]::GetCurrent()
        $WindowsPrincipal = [System.Security.Principal.WindowsPrincipal]::new($Current)
        If(!($WindowsPrincipal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator )))
        {
            Write-Warning -Message 'Permission denied. Use elevated permission!'
            Throw
        }
        
        If(Test-MgmtServer) 
        {
            Write-Warning -Message 'This command can run only on Agent Machine'
            Throw 
        }

        $Now = Get-Date
        $d = $Now.AddMinutes($Duration)

        If(!(Check-MMAgent))
        {
            Write-Warning -Message 'SCOM agent is not installed!'
            Throw
        }

        If(($d - $Now).TotalMinutes -lt $MinimumDurationInMins)
        {
            Write-Warning -Message 'Minimum duration is 5 minutes!'
            Throw     
        }
        If(!([string]::IsNullOrEmpty($Comment)))
        {
            $Comment = $Comment.Trim()
        }
           
        $CurrentName = ([System.Security.Principal.WindowsIdentity]::GetCurrent()).Name

        If(!(Test-Path -Path  $RegistryKey))
        {
            New-Item -Path  $RegistryKey -Force
            Set-ItemProperty -Path  $RegistryKey -Name IsValid -Value '-1' 
            Set-ItemProperty -Path  $RegistryKey -Name Record -Value '-1'  
        }

        
        $Valid = Get-ItemProperty -Path  $RegistryKey -Name IsValid 
        $Record = Get-ItemProperty -Path  $RegistryKey -Name Record 

        if($Valid.isValid -eq '-1')
        {
            Set-ItemProperty -Path  $RegistryKey -Name IsValid -Value ''  
            $Valid = $null
        }

        If($Record.Record -eq '-1')
        {
            Set-ItemProperty -Path  $RegistryKey -Name Record -Value ''  
            $Record = $null
        }

        If(!([string]::IsNullOrEmpty($Valid.isValid)) -and !([string]::IsNullOrEmpty($Record.Record)) -and ($Valid.isValid -eq '0' -or $Valid.isValid -eq '1'))
        {
            [Double]$Min = $Record.Record.Split('|')[0]
            [DateTime]$SetDate = $Record.Record.Split('|')[3]
            [DateTime]$Now = Get-Date
            [Double]$RemainMin = ($SetDate-$Now).Minutes
            
            if($RemainMin -gt 0.0)
            {
                $Value = [string]::Concat($Duration,$ParamSeparator,$Reason,$ParamSeparator,$CurrentName,$CommentSeparator,$Comment,$ParamSeparator,$Now.ToString())
                Set-ItemProperty -Path  $RegistryKey -Name Record -Value $Value 
                Set-ItemProperty -Path  $RegistryKey -Name IsValid -Value '1'
            }
            
            $TotalMinutes = ($SetDate.AddMinutes($Min + $ExtraTimeInMinutes) -$Now).TotalMinutes
            If($TotalMinutes -gt 0.0)
            {
                if($Valid.isValid -eq '0')
                {
                    Write-Warning -Message "System is probably in Maintenance Mode. Preferably try after $(($Now.AddMinutes($TotalMinutes)))"
                }
                Else
                {
                    Write-Warning -Message 'Last Maintenance Mode Request pending. Try afterwards.'
                }

                return
            }
            
            $Value = [string]::Concat($Duration,$ParamSeparator,$Reason,$ParamSeparator,$CurrentName,$CommentSeparator,$Comment,$ParamSeparator,$Now.ToString())
            Set-ItemProperty -Path  $RegistryKey -Name Record -Value $Value 
            Set-ItemProperty -Path  $RegistryKey -Name IsValid -Value '1'        
        }
        Else
        {
            $Value = [string]::Concat($Duration,$ParamSeparator,$Reason,$ParamSeparator,$CurrentName,$CommentSeparator,$Comment,$ParamSeparator,$Now.ToString())
            Set-ItemProperty -Path  $RegistryKey -Name Record -Value $Value 
            Set-ItemProperty -Path  $RegistryKey -Name IsValid -Value '1'
        }
    }
        
    Catch
    {
        Write-Warning -Message 'Error!'
    }
}