Public/Start-SCOMServerMaintenance.ps1
<#
.SYNOPSIS Starts maintenance mode for specified servers in System Center Operations Manager (SCOM). .DESCRIPTION This function initiates SCOM maintenance mode for a list of servers. If a server is already in maintenance mode, it reports who set it and when it is scheduled to end. .PARAMETER SCOMServer The hostname of the SCOM management server. .PARAMETER MaintTime The duration in hours for how long the server should stay in maintenance mode. Default is 0.5 hours if not specified. .PARAMETER ServerList An array of server names for which maintenance mode should be started. .PARAMETER Comment A comment to describe the reason for putting the servers in maintenance mode. Defaults to "Planned Nutanix Activity". .PARAMETER Reason The reason for maintenance, chosen from a set of predefined reasons. .PARAMETER OutputFolder The folder where maintenance mode logs should be saved. Defaults to the user's profile folder. .EXAMPLE Start-SCOMServerMaintenance -SCOMServer vdurscomms001 -MaintTime 0.4 -ServerList @("vdurcxa01001","vdurcxa01002") -Comment "testing" -Reason PlannedApplicationMaintenance -OutputFolder C:\Temp This example starts maintenance mode for "server01" and "server02" on "scomserver.example.com" with a comment and a specific reason. .NOTES Make sure you have the required permissions to interact with SCOM and start maintenance modes on servers. #> function Start-SCOMServerMaintenance { [CmdletBinding()] Param( [Parameter(Mandatory = $true)] [string] $SCOMServer, [Parameter(Mandatory = $false)] [string] $MaintTime = 0.5, [Parameter(Mandatory = $true)] [string[]] $ServerList, [Parameter(Mandatory = $false)] [string] $Comment = "Planned Nutanix Activity", [Parameter(Mandatory = $false)] [ValidateSet("PlannedHardwareMaintenance", "PlannedHardwareInstallation", "PlannedApplicationMaintenance", "PlannedApplicationInstallation", "PlannedOperatingSystemReconfiguration")] [string] $Reason = "PlannedHardwareMaintenance", [Parameter(Mandatory = $false)] [string] $OutputFolder = $env:USERPROFILE ) Import-Module OperationsManager $instanceClass = Get-SCOMClass -Name "Microsoft.Windows.Computer" -ComputerName $SCOMServer foreach ($server in $ServerList) { $fqdn = [System.Net.Dns]::GetHostEntry($server).HostName $instance = Get-SCOMClassInstance -Class $instanceClass -ComputerName $SCOMServer | Where-Object {$_.DisplayName -eq $fqdn} $currentMM = Get-SCOMMaintenanceMode -Instance $instance -ComputerName $SCOMServer if ($currentMM) { $userWhoSetMM = $currentMM.User $utcTime = $currentMM.ScheduledEndTime $dateTimeUtc = [DateTime]::Parse($utcTime, [Globalization.CultureInfo]::InvariantCulture) $easternZone = [TimeZoneInfo]::FindSystemTimeZoneById("Eastern Standard Time") $easternTime = [TimeZoneInfo]::ConvertTimeFromUtc($dateTimeUtc, $easternZone) $format = "MM/dd/yyyy hh:mm:ss tt" # 'tt' denotes the AM/PM part $ScheduledEndTime = $easternTime.ToString($format) Write-Host "Server $fqdn is already in maintenance mode, set by $userWhoSetMM and scheduled to end at $ScheduledEndTime EST." } else { $endTime = (Get-Date).AddHours($MaintTime) Start-SCOMMaintenanceMode -Instance $instance -EndTime $endTime -Comment $Comment -Reason $Reason $dateTime = Get-Date -Format "dd_MM_yy" $file = Join-Path -Path $OutputFolder -ChildPath "MaintenanceModelist_$dateTime.txt" Get-SCOMMaintenanceMode -Instance $instance -ComputerName $SCOMServer | Select-Object ManagementGroup, User, StartTime, ScheduledEndTime, Reason, Comments | Out-File -FilePath $file -Append Write-Host "Maintenance mode started for server $fqdn." } } } |