Our SCOM monitoring environment monitors one of our provisioning environments, where machines come and go regularly. These hosts are potentially in many, many different groups depending on function, and they can come and go many times a day. Yes, a group can be put into maintenance mode easily, but doing so for a host, remotely, isn’t necessarily as easy. Here’s a PowerShell script (created after reading about something similar on the Coretech Blog, here) that takes machine names passed in as the first parameter, places all machine names in an array, and sets each machine into maintenance mode in a particular SCOM management group. There’s little error checking here, so if you wanted to make this more robust, you’d probably want to add some parameter validation, etc. Without ado, here it is:
# Parse Params: [CmdletBinding()] Param( [Parameter( Position=0, Mandatory=$False, HelpMessage="Full path of text file with list of machine names to put into maintenance mode:" )] [String]$TextPath = "C:\TEMP\MachineNames.txt" ) [Parameter( Position=1, Mandatory=$False, HelpMessage="How long should maintenance mode last (in minutes - default is 15)?" )] [ValidateRange(5,999)] [int]$Minutes = 15, [Parameter( Position=2, Mandatory=$False, HelpMessage="Which SCOM server should this script connect to?" )] [string]$SCOMServer = "SCOMServerName", [Parameter( Position=3, Mandatory=$False, HelpMessage="Add a comment about why you're doing this here:" )] [String]$Comment = "MaintenanceModeScript" ) $MachineNames = Get-Content $TextPath $MachineArray = New-Object System.Collections.ArrayList foreach($Machine in $MachineNames) { $MachineArray.Add($Machine) | Out-Null } # Connect to SCOM server: $Session = New-PSSession -ComputerName $SCOMServer $Session = Get-Pssession # Run script remotely against SCOM server: Invoke-Command -Session $Session -ScriptBlock { Param($MachineArray, $SCOMServer, $Minutes, $Comment) Write-Host $MachineArray.Count # Invoke OperationsManager module: Add-PSSnapin "Microsoft.EnterpriseManagement.OperationsManager.Client" # Create/connect to the DCSSCOM management group on SCOM server: Set-Location "OperationsManagerMonitoring::" $mgConn = New-ManagementGroupConnection -ConnectionString:$SCOMServer; # Connect to agent and set params for maintenance mode: $Agents = Get-Agent $QueriedAgents = @() for($i = 0; $i -lt $MachineArray.Count; $i++) { Write-Host "Working on: $($MachineArray[$i])" -ForegroundColor Cyan $AgentToAdd = $Agents | Where {$_.ComputerName –eq $MachineArray[$i]} if($AgentToAdd -eq $null) { Write-Host "Could not find agent for $($MachineArray[$i])" -ForegroundColor Red continue } $QueriedAgents += $AgentToAdd } foreach($Agent in $QueriedAgents) { $StartTime = [DateTime]::Now $EndTime = $StartTime.AddMinutes($Minutes) Start-SCOMMaintenanceMode -Instance $Agent.HostComputer -EndTime $EndTime -Reason "PlannedOther" -Comment $Comment } } -ArgumentList $MachineArray, $SCOMServer, $Minutes, $Comment # Cleanup! Remove-PSSession -Session $Session