Sep 05

Print this Post

Put agent into maintenance mode remotely via PowerShell in SCOM 2012 R2

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:
        HelpMessage="Full path of text file with list of machine names to put into maintenance mode:"
        [String]$TextPath = "C:\TEMP\MachineNames.txt"
        HelpMessage="How long should maintenance mode last (in minutes - default is 15)?"
        [int]$Minutes = 15,
        HelpMessage="Which SCOM server should this script connect to?"
        [string]$SCOMServer = "SCOMServerName",
        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

        $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

Permanent link to this article: http://www.cluberti.com/blog/2014/09/05/put-agent-into-maintenance-mode-remotely-via-powershell-in-scom-2012-r2/

Bad Behavior has blocked 248 access attempts in the last 7 days.