Are you a SCOM admin? Have you ever received a call or an e-mail from a user or technician asking you to put a server in maintenance mode in SCOM? You have? Then I think you have something in common with most SCOM admins out there. Today, I will share a solution letting you put a server into maintenance mode directly from the server itself.
This will help your technicians by letting them put the server into maintenance mode themselves, and it will save you from some phone calls.
The solution consists of a PowerShell script that will be placed on each server, and a shared folder containing some DLL files and the Operations Manager PowerShell module.
To get it running, you need to perform the following tasks;
- Create a share on one of the management servers
- Download the script to a server and edit the script
- Copy the DLL and PowerShell files needed to run the script
Start by creating a folder named “Shared” on one of your management servers with another folder named “OperationsManager” in it. Then right click the “Shared” folder and select “Properties”. Then click the “Sharing” pane followed by “Advanced Sharing…”.
Your share is ready and you can move on to copying the files needed to run the script.
Download the script from TechNet Gallery here and place it (preferably) on the desktop of a server that isn´t a SCOM Management Server.
To get started you will need to adjust some parameters of the script. The following is what need to be changed:
- On line #33; type the fully qualified domain name (FQDN) of one of your management servers.
- On line #34; type in your domain name as seen in the script. se for example.
- On line #37; type in the shared path to be used.
- On line #41; Make sure that the path is exactly as it is on your SCOM reporting server.
The script is seen below;
Author: Daniel Örneling, Approved Consulting
Description: Use this script to set a server into maintenance mode in Operations Manager.
Copy the following files to a shared folder named "OperationsManager";
From C:\Program Files\System Center <generation>\Operations Manager\Server\SDK Binaries
Also copy the folder "PowerShell" located in "C:\Program Files\Microsoft System Center <generation>\Operations Manager" to the same folder as above.
In order for this script to work, you need to go through the following parameters and make sure they fit your environment;
# Type in the amount of minutes you want to put the server to maintenance mode
# The name of the management server to connect to and the local domain. Adjust the values to match your environment.
$ManagementServer = "FQDN of your management server"
$Domain = "YourDomain.com"
# Set the parameters needed
$SCOMModuleRemotePath = "\\YourManagementServer\Shared$\OperationsManager\" # Type in the remote share where the files are placed
$SCOMLocalPath = "C:\Program Files\Microsoft System Center 2016\Operations Manager"
$SCOMModuleLocalPath = "C:\Program Files\Microsoft System Center 2016\Operations Manager\Powershell\OperationsManager"
$SCOMModule = "C:\Program Files\Microsoft System Center 2016\Operations Manager\Powershell\OperationsManager\OperationsManager.psm1"
$SCOMReportingPath = "C:\Program Files\Microsoft System Center 2016\Operations Manager\Reporting"
# Check if the server is a SCOM reporting server or not
if (Test-Path $SCOMReportingPath)
#$SCOMLocalPath = "C:\Program Files\Microsoft System Center 2016"
Write-Host "Server is SCOM Reporting server. Copying module." -ForegroundColor Green -ErrorAction Ignore
Copy-Item "$SCOMModuleRemotePath\*.*" -Destination $SCOMLocalPath -Recurse -ErrorAction Ignore
Copy-Item "$SCOMModuleRemotePath\Powershell" -Destination $SCOMLocalPath -Recurse -ErrorAction Ignore
Write-Host "Server is not SCOM Reporting server, continuing." -ForegroundColor Green
# Copy the OperationsManager module to the server
if (Test-Path $SCOMLocalPath)
Write-Host "Module already exists, no action needed." -ForegroundColor Green
Write-Host "Module doesn´t exist, copying." -ForegroundColor Green
Copy-Item $SCOMModuleRemotePath -Destination $SCOMLocalPath -Recurse
Set-Location -Path $SCOMLocalPath
$Folders = "Console","Server"
foreach ($Folder in $Folders)
Write-Host "The folder $Folder does not exist, creating..." -ForegroundColor Green -NoNewline
New-Item $Folder -type Directory -Force | Out-Null
Write-Host "done!" -ForegroundColor Green
# Load the DLL´s needed to import the module
[Reflection.Assembly]::LoadWithPartialName("System.EnterpriseServices") | Out-Null
[System.EnterpriseServices.Internal.Publish] $publish = New-Object System.EnterpriseServices.Internal.Publish
$publish.GacInstall("C:\Program Files\Microsoft System Center 2016\Operations Manager\Microsoft.EnterpriseManagement.Core.dll")
$publish.GacInstall("C:\Program Files\Microsoft System Center 2016\Operations Manager\Microsoft.EnterpriseManagement.OperationsManager.dll")
$publish.GacInstall("C:\Program Files\Microsoft System Center 2016\Operations Manager\Microsoft.EnterpriseManagement.Runtime.dll")
#Get the PowerShell Module Path
$PSModulePath = [Environment]::GetEnvironmentVariable(“PSModulePath”)
#Add to the existing path the additional path to the Operations Manager module
$PSModulePath += “;C:\Program Files\System Center 2016\Operations Manager\Powershell\”
#Set the new path to the PSModulePath variable
# Import the module and connect to Operations Manager
# collect the computer name running the script and set as a parameter
$ServerName = $env:computername + '.' + $Domain
# Put the server in to maintenance mode for the appointed amount of minutes
$AgentInstance = Get-SCOMClassInstance -Name $ServerName
$Time = ((Get-Date).AddMinutes($Minutes))
Write-Host "The server $ServerName will be put in maintenande mode for $Minutes minutes. Starting maintenance mode." -ForegroundColor Green
Start-SCOMMaintenanceMode -Instance $AgentInstance -EndTime $Time -Reason "PlannedOther" -Comment "Script generated maintenance mode."
Start-Sleep -Seconds 5
First, let’s verify that you can reach the “OperationsManager” folder by using the UNC path. The path should look like \\YourManagementServer\Shared$\OperationsManager.
Now, copy these three DLL files from “C:\Program Files\System Center 2016\Operations Manager\Server\SDK Binaries”;
Copy these three files and place them in the “OperationsManager” folder you just created.
You then need to copy the folder named “PowerShell” from “C:\Program Files\Microsoft System Center 2016\Operations Manager” on your management server and place it directly under the “OperationsManager” folder.
Run the script
Now that the share has been created and the files are copied, you should be able to run the script.
Run the script from the desktop by right clicking it and choose “Run with PowerShell”. Next, you need to type in how many minutes the server should be placed in maintenance mode. The minimum value is 5 minutes.
The script will now start to copy the files from the management server to the server where you´re running the script. This means that the next time you run the script from the server, no files need to be copied and you will save a few seconds executing it.
The script will copy the files, import the Operations Manager PowerShell module and start maintenance mode for the given number of minutes you typed in. This shouldn’t take more than 10 seconds tops, but it depends on your bandwidth between the servers.
The only known issue that might affect this script is related to PowerShell and has really nothing to do with the script. Normally PowerShell don’t allow for custom scripts to run on a server. To go around this issue, the execution policy needs to be set to Unrestricted.
This is done by running the following command: Set-ExecutionPolicy -ExecutionPolicy Unrestricted
What you´ve seen here is how you can easily put a server into maintenance mode without having to go through a Management Server or a server with the Operations Console installed. The script has been built for SCOM 2016 but should work fine for SCOM 2012 as well, just change the paths to match your SCOM 2012 environment instead.
Another thing I want to point out is that the lines 1 through 96 is aiming to import the Operations Manager PowerShell Module. What you do after those lines are completely up to you. Feel free to update the script to do something else with the module if you want to use it for something else.
If you have any questions, drop a comment below and I´ll get back as soon as I can.
I got a request for a logging function upon release of the script which I have now added. Once the script runs an event with the ID 1217 will be written to the “Operations Manager” event log, stating that the server has been put into maintenance mode, including the number of minutes and also which user ran the script. A perfect thing to either make an informational rule or collect it with OMS Log Analytics. This way you can track how often servers are put to maintenance mode and who did it.