SCOM: 10 useful PowerShell scripts
Applies to
This guide applies to System Center Operations Manager 2016, 1801, 1807
Introduction
System Center Operations Manager (SCOM) comes with a good PowerShell module, there are many occasions where you want something done quick and easy.
There are a total of 183 different PowerShell cmdlets in the SCOM 1807 PowerShell module, that already speaks for itself how much you can do with this PowerShell module!
This Wiki article will provide useful SCOM PowerShell scripts, the purpose of this article is to help you make your SCOM management easier and quicker.
It will provide an example for most commands, if you do not see the examples clearly, it's suggested you open the GIF in another browser tab.
Note: Some of the SCOM PowerShell cmdlets below are not found in the Microsoft documentation page for the System Center PowerShell 2016, these scripts have been tested to work with the following SCOM versions: 2016, 1801 and 1807.
The SCOM PowerShell scripts
Exporting sealed management packs
Get-SCOMManagementPack | where {$_.Sealed -eq $True } | Export-SCOMManagementpack -Path "C:\Management Packs\Sealed"
Example:
Exporting unsealed management packs
Get-SCOMManagementPack | where {$_.Sealed -eq $False} | Export-SCOMManagementpack -Path "C:\Management Packs\Unsealed"
Example:
Setting a windows computer into maintenance mode
$Agent = "yourcomputer.domain.com"
$Time = (Get-Date).addMinutes(60)
$Instance = (Get-SCOMClass -DisplayName "Windows Computer" | Get-SCOMClassInstance | ? { $_.DisplayName -eq $Agent} )
Start-SCOMMaintenanceMode -Instance $Instance -EndTime $Time -Comment "Maintenance Mode started!" -Reason "PlannedOther"
Example:
Retrieve the maintenance mode history for a windows computer
Get-SCOMMaintenanceMode -Instance (Get-SCOMClassInstance -Class (Get-ScomClass -Name "Microsoft.Windows.Computer") |
Where-Object {$_.DisplayName -match "yourcomputer.domain.com"}) -History -ErrorAction 0
Example:
Enable the proxy for all SCOM agents
Get-SCOMAgent | where {$_.ProxyingEnabled -match "False"} | Enable-SCOMAgentProxy
Retrieve all SCOM agents that are grayed out or unavailable
$Agent = Get-SCClass -Name "Microsoft.SystemCenter.Agent"
$GrayAgents = Get-SCOMMonitoringObject -class:$Agent | where {$_.IsAvailable –eq $False}
$GrayAgents
Example:
Retrieve all the SCOM alerts within the last day (highest priority first)
$Date = (Get-Date).AddDays(-1)
(Get-SCOMAlert -Criteria 'ResolutionState = "0"' | Where-Object {$_.LastModified -gt $Date }) | Sort-Object Priority -Descending
Resolution States | Serverity Values for Alerts |
0 = New | 0 = Informational |
255 = Closed | 1 = Warning |
2 = Critical |
Example:
Retrieve all the subscriptions that a specific subscriber belongs to
Get-SCOMNotificationSubscription | ForEach {
$Subscriber = $_.DisplayName
$_.ToRecipients | foreach {
If ($_.Name -Like "*Enter Name Here*") {
Write-Host $Subscriber
}
}
}
Example:
Export all the SCOM agents to a CSV
Get-SCOMAgent | Select DisplayName, ManagementGroup, Version, ProxyingEnabled | Export-Csv -NoType C:\Temp\SCOMAgents.csv
Example:
Retrieve the amount of agents reporting to a SCOM Management / Gateway Server
$MgmtServer = Get-SCOMManagementServer | where { $_.DisplayName -eq "yourSCOMmanagementserver.domain.com"
$TotalAgents = (Get-SCOMAgent -ManagementServer $MgmtServer).count
$TotalAgents
Example:
Note: If you need to retrieve the amount of agents that are reporting to a SCOM Gateway server, change the DisplayName to the SCOM Gateway server name (without FQDN).