param ($rootMS, $groupName, $timeLength, $timeType, $cmdReason, $cmdComment) #Setup variables $ErrorActionPreference = "SilentlyContinue" $dictReason = @{1='plannedhardwaremaintenance';2='unplannedhardwaremaintenance';3='plannedhardwareinstallation';4='unplannedhardwareinstallation';5='plannedoperatingsystemreconfiguration';6='Unplannedoperatingsystemreconfiguration';7='plannedapplicationmaintenance';8='unplannedapplicationmaintenance';9='applicationinstallation';10='applicationunresponsive';11='applicationunstable';12='securityissue';13='lossofnetworkconnectivity';14='unplannedother'} $dictTimeType = @{1='minutes';2='hours';3='days'}; #Checking Root MS parameter if ($rootMS -eq $null) { Write-Host "serverMS parameter is NULL or not accepted by Powershell"; Write-Host "EX: rootManagementServer.fqdn.local"; Write-Host "Syntax:"; Write-Host "-rootMS: FQDN of the root management server for the target management group. `n Ex: RootManagementServer.fqdn.local"; Write-Host "-groupName: The target group. Use the display name of the group. `n Ex: 'NOC Computers Test'"; Write-Host "-timeLength: The time interval amount for the maintenance. 'n Ex: 12"; Write-Host "-timeType: Can be one of the following: minutes, hours, days"; Write-Host "-cmdReason: Can be one of the following:`n plannedhardwaremaintenance `n unplannedhardwaremaintenancev`n plannedhardwareinstallation `n unplannedhardwareinstallation `n plannedoperatingsystemreconfiguration `n unplannedoperatingsystemreconfiguration `n plannedapplicationmaintenance `n unplannedapplicationmaintenance `n applicationinstallation `n applicationunresponsive `n applicationunstable `n securityissue `n lossofnetworkconnectivity 'n unplannedother"; Write-Host "-cmdComment: Any explanation you want. If left blank, the script will send the 'N/A' comment.`n EX: 'This server is BSOD like crazy!'"; exit; } #Initializing the Ops Mgr 2007 Powershell provider add-pssnapin "Microsoft.EnterpriseManagement.OperationsManager.Client" -ErrorVariable errSnapin ; set-location "OperationsManagerMonitoring::" -ErrorVariable errSnapin ; new-managementGroupConnection -ConnectionString:$rootMS -ErrorVariable errSnapin ; set-location $rootMS -ErrorVariable errSnapin ; #Checks to see if it failed or succedded in loading the provider if ($errSnapin.count -eq 0){ Write-host "`nOpsMgr 2007 PSSnapin initialized!`n"; } else{ Write-host "`nOpsMgr 2007 PSSnapin failed initialize!`nPlease verify you are running this script on a Ops Mgr 2007 Management Server"; Write-host; } #Check Command Line Parameters #Checking for the groupName parameter if ($groupName -eq $null) { Write-Host "groupName parameter is NULL or not accepted by Powershell"; Write-Host "EX: AD Domain Controller Group (Windows 2003 Server)"; exit; } #Checking for the timeLength parameter if ($timeLength -eq $null) { If ($timeLength.GetType().Name -ne "Int32") { if ($timeLength.GetType().Name -ne "Double") { Write-host "timeLength is either NULL or not a number."; Write-Host "EX: 12 or 12.5"; } } } #Checking for the timeType parameter if (!($dicttimeType.ContainsValue($timeType.ToLower()))) { Write-Host "timeType has incorrect parameter."; Write-Host "Options: Minutes, Hours, Days"; exit; } #Checking for the cmdReason parameter if ($dictReason.ContainsValue($cmdReason.ToLower()) -eq $true){ $outReason = $cmdReason; } else { $outReason = "UnPlannedOther"; } #Checking for the cmdComment parameter if (($cmdComment -eq $null) -or ($cmdComment -eq "")){ $outComment = "N/A"; } else{ $outComment = $cmdComment; } #Main Program #Finding Group based on DisplayName $groupAgents = get-monitoringobject | where {$_.DisplayName -eq $groupName}; #Get Current Date/Time $timeFrame = get-date; #Initialize maintenance based on parameters if ($timeType -eq "minutes") { $groupAgents | New-MaintenanceWindow -startTime: $timeFrame -endTime: ($timeFrame.AddMinutes($timeLength)) -reason: $outReason -comment: $outComment -ErrorVariable err ; } elseif ($timeType -eq "days") { $groupAgents | New-Maintenancewindow -startTime: $timeFrame -endTime: ($timeFrame.AddDays($timeLength)) -reason: $outReason -comment: $outComment -ErrorVariable err ; } elseif ($timeType -eq "hours") { $groupAgents | New-Maintenancewindow -startTime: $timeFrame -endTime: ($timeFrame.AddHours($timeLength)) -reason: $outReason -comment: $outComment -ErrorVariable err ; } #Checks status and post to console on Success or fail if ($err.Count -eq 0) { Write-Host "The group" $groupAgents.DisplayName "is now in maintenance for $timelength $timeType."; Write-Host "Script completed successfully!"; } else { Write-Host "The group" $groupName "is not in maintenance mode."; Write-Host "Please review command line parameters."; }