Working with the SCOM PSDrive

Ever wondered how you can navigate around in SCOM using Windows PowerShell? So click “Start” and load up the “Operations Manager Shell”.

When you load up the Operations Manager Shell you’ll notice that there’s a bit of text at the top of the window telling you how to find help and what to type to get a list of commands. You’ll also notice that it connects to a Management Server.

Firstly, let’s see what commands are available. To get a list of commands, type in:

Get-OperationsManagerCommand

This will return a list of Operations Manager cmdlets that you can use to help manage SCOM – some of these I will demonstrate in other posts on this blog.

Also notice that our PowerShell prompt looks a little like a regular old dos prompt. Typically on a dos window your prompt will look like C: or D: or even C:WindowsSystem32, depending on what drive or folder you are in at the time. With PowerShell we have drives as well. Not only hard drives, but drives that represent other things in our system. Things like the Windows Registry, we can navigate that like a drive and folder structure. Things like Aliases, we can connect to that drive and work with those things just like regular files and of course we have the Monitoring Drive.

You can get a list of these “drives” by typing in:

get-psdrive

Name       Provider      Root
—-       ——–      —-
Alias      Alias
C          FileSystem    C:
cert       Certificate
E          FileSystem    E:
Env        Environment
F          FileSystem    F:
Function   Function
G          FileSystem    G:
HKCU       Registry      HKEY_CURRENT_USER
HKLM       Registry      HKEY_LOCAL_MACHINE
Monitoring Operations…
Variable   Variable

So let’s say we want to connect to the “Alias” drive (or any of the other ones for that matter) we only need to type in CD followed by the name of the drive (with a colon: on the end). Don’t forget to add the colon or you’ll get an error…like this:

cd alias

Set-Location : System error.
At line:1 char:3
+ cd  <<<< alias
Set-Location : Cannot find path ‘Monitoring:scom-rms.thescomlab.comalias’ because it does not exist.
At line:1 char:3
+ cd  <<<< alias

If we add the colon:

cd alias:

The prompt changes to:

PS Alias:

Now if we type in “dir” or “Get-ChildItem” we get a directory listing and it will return a list of aliases on our system. You’ll also note that dir is an alias for Get-ChildItem anyway.

So let’s move out of the Alias drive and into the Monitoring one.

cd Monitoring:

PS Monitoring:scom-rms.thescomlab.com
>

Now if we type in “dir” we’ll get back a list of objects in the Monitoring Drive and you’ll see a bunch of “Groups” returned. Using the
“Get-MonitoringObjectGroup” cmdlet would have done the same thing by the way.

We can of course change into those groups.

cd Microsoft.Unix.ComputerGroup

The prompt changes to:

PS Monitoring:scom-rms.thescomlab.comMicrosoft.Unix.ComputerGroup
>

dir

Id               : 8c7f35b9-5ec5-6893-135d-14decddd45af
PathName         : unixserver1.thescomlab.com
DisplayName      : unixserver1.thescomlab.com
ManagementMode   :
ManagementGroup  : TheSCOMLab01
HealthState      : Success
OperationalState :

Id               : 8c4f35b6-5ec5-6493-136d-14dedfdd44fa
PathName         : unixserver2.thescomlab.com
DisplayName      : unixserver2.thescomlab.com
ManagementMode   :
ManagementGroup  : TheSCOMLab01
HealthState      : Error
OperationalState :

And now have some objects, which just happen to be members of this Microsoft.Unix.ComputerGroup. Of course, PowerShell allows us to do things that the GUI can’t. Let’s say that I wanted to find out what members I have in this Unix Group, but only show those that have a HealthState of Error. As you can see from my output above, I have 1 server that has an error, unixserver2.

A simple PowerShell one-liner would tell me that information:

Get-ChildItem | Where-Object {$_.HealthState -eq ‘Error’}

Id               : 8c4f35b6-5ec5-6493-136d-14dedfdd44fa
PathName         : unixserver2.thescomlab.com
DisplayName      : unixserver2.thescomlab.com
ManagementMode   :
ManagementGroup  : TheSCOMLab01
HealthState      : Error
OperationalState :

Now please understand that this one liner only worked because I was already in the right folder. Further up in this post I had changed into the “Microsoft.Unix.ComputerGroup” folder and because I was already in the right folder, my PowerShell one-liner worked. What if I type that command in when I am in the Root Folder or another folder? Well it won’t return anything since those objects are in a different location. So here’s an alternative method:

$UnixGroup=”Microsoft.Unix.ComputerGroup”
Push-Location $UnixGroup
Get-ChildItem | Where-Object {$_.HealthState -eq “Error”}
Pop-Location

So here I’ve set $UnixGroup to the group location and then I use Push-Location to change to that directory/group. I then run the exact same command (Get-ChildItem | Where-Object {$_.HealthState -eq “Error”}) which will list all objects in that group that have a Health State of “Error”. And finally, set my shell back to the original place I was before I ran the script by using Pop-Location.

The result is the same:

Id               : 8c4f35b6-5ec5-6493-136d-14dedfdd44fa
PathName         : unixserver2.thescomlab.com
DisplayName      : unixserver2.thescomlab.com
ManagementMode   :
ManagementGroup  : TheSCOMLab01
HealthState      : Error
OperationalState :

As a bonus, here’s an alternative blob of PowerShell which will do exactly same thing:

Get-MonitoringClass -name “Microsoft.Unix.Computer” | Get-MonitoringObject | Where-Object {$_.HealthState -eq “Error”}

The result is the same:

Id               : 8c4f35b6-5ec5-6493-136d-14dedfdd44fa
PathName         : unixserver2.thescomlab.com
DisplayName      : unixserver2.thescomlab.com
ManagementMode   :
ManagementGroup  : TheSCOMLab01
HealthState      : Error
OperationalState :

So as you can see, PowerShell is very useful in finding out these sorts of things and with only a couple of lines of code we can do a lot of work.

I’d encourage you to have a look around in the “Monitoring:” Drive and use dir (or Get-ChildItem) to perform directory listings and understand what you can find in there. Maybe even use this command and save the output to a text file so you can review it to get a better understand of SCOM and your own environment:

Get-ChildItem -recurse | out-file “C:Tempdir.txt”

 
Comments

No comments yet.

Leave a Reply