Search This Blog

Monday, March 26, 2012

Powershell script to stop system maintenance mode

In the event you want to pull the computer out of maintenance mode for some reason, here is the script for ending the maintenance mode. The command line prompts could be hard coded as variables or you could add a $params statement to the front of the script to feed in a variable.


$RMSFQDN = Read-Host "Enter the FQDN of your management server"

$Name = "Microsoft.EnterpriseManagement.OperationsManager.Client"

$ModuleLoaded = Get-Pssnapin $Name -ErrorAction SilentlyContinue

If (-not $ModuleLoaded)
{
add-pssnapin "Microsoft.EnterpriseManagement.OperationsManager.Client";
}

New-ManagementGroupConnection -ConnectionString $RMSFQDN
Set-Location "OperationsManagerMonitoring::";

$computerClass = get-monitoringclass -name:Microsoft.SystemCenter.ManagedComputer
$computerPrincipalName = Read-Host "Enter the FQDN computer name for ending maintenance:"
$computerCriteria = "PrincipalName='" + $computerPrincipalName + "'"
$computer = get-monitoringobject -monitoringclass:$computerClass -criteria:$computerCriteria


if($computer -eq $null)
{
$unixClass = get-monitoringclass -name "Microsoft.Unix.Computer"
$monObject = Get-MonitoringObject -monitoringclass:$unixClass
$computer = $monObject | where {$_.displayname -eq $computerPrincipalName}
}
ELSE
{
$computerClass = get-monitoringclass -name:Microsoft.Windows.Computer
$computer = get-monitoringobject -monitoringclass:$computerClass -criteria:$computerCriteria
}


if($computer.InMaintenanceMode -eq $true)
{
"Stopping maintenance mode for: " + $computerPrincipalName
$computer.StopMaintenanceMode([DateTime]::Now.ToUniversalTime(),[Microsoft.EnterpriseManagement.Common.TraversalDepth]::Recursive);
}
ELSE
{
"The computer " + $computerPrincipalName + " is not in maintenance mode."
}

No comments:

Post a Comment