Command Shell: OpsMgr Automation Introduction

 

This is the first in a guest blog series from Marco Shaw, of Powershell MVP, Powershell Community and TechNet fame. In this post, Marco launches a discussion of automation with the OpsMgr 2007 Command Shell.

Marco, you have the floor…

Recently in a OpsMgr newsgroup a question was posted asking how to integrate a OpsMgr PowerShell script into a C# application. I’m slightly simplifying the original question, but it was basically asked how the PowerShell cmdlet get-alert could be used inside a C# application.

I replied listing two possibilities:

1. You can use your C# to call the Command Shell cmdlet

-or-

2. You can use your C# to call the SDK directly using the OpsMgr API.

I wanted to provide some more details and examples on how exactly this is done. I have come up with 5 different ways that this could be accomplished using PowerShell, the PowerShell APIs, and the OpsMgr SDK:

  1. The simplest way to list the alerts is via the OpsMgr Command Shell by simply using the get-alert cmdlet.
  2. Still using the Command Shell, I could use PowerShell’s .NET functionality to use the OpsMgr SDK .NET assemblies directly from within the console.
  3. Using the OpsMgr SDK .NET assemblies, a C# console application could be created to duplicate the same functionality.
  4. Similar to what was done for SQL Server 2008, a PowerShell “clone”, also known as a “minishell”, can be created that can also work as a console application.
  5. A custom PowerShell runspace can be used, which is the method the Exchange 2007 Management Shell (which is PowerShell with some add-ons for Exchange management/administration) must be integrated into C# applications because of the absence of any other public API.

My intent is to create a blog post covering each of these methods in a separate post, but to start off this series of 3, maybe even 4 or 5, posts on OpsMgr automation, I’m going to explain #1 above. Using the OpsMgr Command Shell, and simply invoking the cmdlet get-alert is as simple as you can get:

Command Shell>get-alert

Without any parameters or values passed, all the alerts on the system will be listed on the screen. I could get into a discussion about the provider and the “context” where the alert has been invoked, but you can find more information about that in the links below.

Now, I mention 5 ways to accomplish this… I’ve only been able to get the first 3 working at this point, so there may be technical issues with the last 2 that either aren’t possible to overcome or I don’t have enough C# experience at this point to know how to make these methods work in this particular case.

If you’ve not used the Command Shell, here’s some good links for some short videos and a TechNet Magazine article that may help:

http://marcoshaw.blogspot.com/search/label/opsmgr

Related Articles:

See also this link for more information on the get-alert cmdlet:

http://www.systemcenterforum.org/command-shell-reference-get-alert/

————-

Sign up for the next meeting of the Powershell Virtual User Group. Registration at the URL below.

http://marcoshaw.blogspot.com/2008/10/windows-powershell-virtual-user-group.html

Leave a Reply

privacy policy | terms of use | copyright © 2008 pete zerger