OpsMgr 2012 PowerShell: Quick Reference

I will use this post to summarize all commands, one-liners and short scripts that I identify overtime for PowerShell against OpsMgr 2012

Getting Oriented

Import-module OperationsManager

Adds the module into your session so that you can run the OpsMgr cmdlets.  Not required in PowerShell v3

Get-Help OpsMgr

Shows the about topics provided with the OperationsManager module

Get-Help about_OpsMgr_WhatsNew | more

 

Shows the “What’s New” topic one page at a time.  This topic gives a good overview of what was added and how it differs from the 2007 R2 interface

get-help about_OpsMgr_Cmdlet_Names | more

 

Shows the cmdlet names topic one page at a time.  This topic gives a good overview of how cmdlets are named and how the names align to the 2007 R2 cmdlets

get-command -module operationsmanager | more

 

Lists all commands provided with the OpsMgr 2012 module

Get-help <cmdletName> -detailed | more

This will provide the help content for the cmdlet, including the examples.

Get-help <cmdletName> -full| more

This will provide the verbose details on the parameters that are available for the cmdlet.

<cmdletName> | Get-Member

This will show you the possible type(s) that a given cmdlet can return and what the methods and properties are on those types.  This is especially useful for figuring out what you can use in pipelining cmdlets together.

Getting Connected

get-command -module operationsmanager -noun SCOMManagementGroupConnection

Get all cmdlets that are related to working with management group connections

get-help about_OpsMgr_Connections | more

Shows the about topic’s content related to connections

New-SCOMManagementGroupConnection -ComputerName <OpsMgrServerName>

Creates a new persistent connection to a management group

# Get all connections

Get-SCOMManagementGroupConnection

# Get all connections and just show their ID
Get-SCOMManagementGroupConnection | select-object -Property ID

# Get all connections to a specific MG and show server and whether the connection is the Active one
Get-SCOMManagementGroupConnection -ManagementGroup "OMCOR2" | format-table ManagementServerName, IsActive –Auto

Use the GET cmdlet to get one or more persistent connections

Get-SCOMManagementGroupConnection –Id e67d872b-815f-4d27-a812-a29a9a6fa438 | Set-SCOMManagementGroupConnection

Get a specific connection by its unique ID and set it to be the active connection

Get-SCOMManagementGroupConnection –Id e67d872b-815f-4d27-a812-a29a9a6fa438 | Remove-SCOMManagementGroupConnection

Get a specific connection by its unique ID and remove it

$connection = Get-SCOMManagementGroupConnection –ManagementGroupName “MyGroup”

Get-SCOMAlert –SCSession $connection

Get a connection to the management group named “MyGroup” and then explicitly use that connection to get alerts

# Create the connection
New-SCOMManagementGroupConnection –ComputerName “MyServer”

# Get alerts implicitly using the connection
$alerts = Get-SCOMAlert

# Check out the ManagementGroup property on an alert
$alerts[0] | select-object -Property ManagementGroup | get-member

Get a connection to the management server named “MyServer” and then implicitly use that connection to get alerts. 

The management group property on each alert object hold a small connection back to the MG it came from.

# Get the source connection
$Source = Get-SCOMManagementGroupConnection -ComputerName “OMServer1”

# Get all other connections
$Target = Get-SCOMManagementGroupConnection | Where-Object{$_.ManagementServerName -ne "OMServer1"}

When working with multiple management groups and one group is a source, store that connection into one object and then put all other connections into another object.

# Sync the Database grooming settings
Get-SCOMDatabaseGroomingSetting -SCSession $Source | Set-SCOMDatabaseGroomingSetting -SCSession $Target

Get the grooming settings from a source management group and replicate those settings (set) across multiple target management groups.

# See which management packs are not the same between MGs
Compare-Object -ReferenceObject (Get-SCOMManagementPack -SCSession $Source) -DifferenceObject (Get-SCOMManagementPack -SCSession $Target)

Compare the management packs between a source management group and a target management group.

# Get all connections into an object
$Connections = Get-SCOMManagementGroupConnection

# Get alerts from all connections
Get-SCOMAlert -SCSession ($Connections)

# Get all pending management actions and clear them out (Deny)
Get-SCOMPendingManagement -SCSession ($Connections) | Deny-SCOMPendingManagement

When working with multiple management groups, get the same data across all management groups.

 

Get all alerts across multiple management groups.

 

Get all pending management actions across multiple management groups and clear (deny) them all.

PowerShell.exe -Command "& {import-module operationsmanager; Get-SCOMAlert -ComputerName OMServer1}"

Pass a one-liner into PowerShell.exe and leverage a temporary connection to get a list of all alerts from a single management group.

Management and Gateway Servers

get-command -module operationsmanager -Noun *ManagementServer

 

Get all cmdlets related to management and gateway servers

Get-SCOMManagementServer

Get all management and gateway servers

Get-SCOMManagementServer | get-member

Review the member properties and methods of the management server object

Get-SCOMManagementServer | format-table HealthState, Name, Version, ActionAccountIdentity, IsGateway,IsRootManagementServer –AutoSize

Custom formatting for listing management servers so you can see which systems are holding which roles

# Get the action account that should be used
$CorrectRunAs = Get-SCOMRunAsAccount -Name ‘YourAccountNameHere’ | where-object {$_.AccountType -eq 'SCOMActionAccountSecureData'}

# Get the management server(s) that are not using that account
Get-SCOMManagementServer | where-object{$_.ActionAccountIdentity -ne $CorrectRunAs.Name}

Identify any management or gateway server that is not running the correct default action account

# Show the list of management servers, their health state and how many agents they manage
get-scommanagementserver | ft -Property Name, HealthState, @{Name='Agent Count'; Expression={$_.GetAgentManagedComputers().Count}} –AutoSize

# Get the list of agents reporting to a server
$Agents = (get-SCOMManagementServer -Name ‘YourServerName’).GetAgentManagedComputers()

# Show just the agents from that server that are not healthy
# Note: Use the HostedHealthService property to look at the real agent state.
$Agents.HostedHealthService | where-object{$_.HealthState -ne 'Success'}

# Given an agent, find the parent management or gateway server(s)
Get-SCOMAgent –Name “YourAgentName” | Get-SCOMParentManagementServer

Various commands to make the connection between servers and agents and vice versa

$UnhealthyServers = get-SCOMManagementServer | where-object{$_.HealthState -ne 'Success'}

Get a list of all management and gateway servers that are unhealthy

$UnhealthyServers | foreach-object{$_.HostedHealthService}

Get the related health service objects for the unhealthy management and gateway servers

# load the functions up into your session
. FullPathToFile\Write-SCOMMonitoringStateHierarchy.ps1

# Get the hosted health services, from those get the hierarchies
#  and with those do the output to the screen
$UnhealthyServers | foreach-object{$_.HostedHealthService} | foreach-object{$_.GetMonitoringStateHierarchy()} | Write-SCOMMonitoringStateHierarchy

Show a hierarchical view of state, similar to health explorer, of the unhealthy management and gateway servers, using a custom function.

The file Write-SCOMMonitoringStateHierarchy.ps1 is attached to the original blog post.