Orchestrator PowerShell Template

I recently did a webinar together with Cireson on CMDB, automation and self-service using System Center Service Manager, System Center Orchestrator and the Cireson Portal. In the webinar I showed how i always use only the .NET Script Activity in all our runbooks, and the same PowerShell “Template” to invoke scripts. The reason for only using PowerShell scripts instead of the Orchestrator activities is that you will need to add a lot of activities, which can get quite messy, to achieve the same that you can do with a few lines of PowerShell. Secondly you will also quickly realize that the Orchestrator activities are pretty limited, and finally if you want at some point to move to Azure Automation or SMA then you can quickly just copy and reuse all your scripts.

The template below is made to invoke the scripts on a targeted computer, and collect any errors in the Invoke-Command. When running a script block in Invoke-Command it will not return anything back so that is why the Try, Catch, Finally is there. Also if you want to return some data from your script you can add it in the Finally block to the $return and then split it afterward, below you can see an example on returning output from Get-NetIPAddress.

#Credentials and computer name where you wish to execute your script.
$secpasswd = ConvertTo-SecureString "PASSWORD" -AsPlainText -Force
$Creds = New-Object System.Management.Automation.PSCredential ("USERNAME", $secpasswd)

$Computer = "Server01.domain.local"

$return = Invoke-Command -ComputerName $Computer -credential $Creds  -ScriptBlock {
#Ensures that script will fail on error
$ErrorActionPreference = "Stop"

Try{
#CODE HERE, MUST NOT RETURN ANY OUTPUT, Note: Use "| Out-Null" if cmdlet returns output
#CODE HERE, MUST NOT RETURN ANY OUTPUT, Note: Use "| Out-Null" if cmdlet returns output
#CODE HERE, MUST NOT RETURN ANY OUTPUT, Note: Use "| Out-Null" if cmdlet returns output
#Example
$IPAddress = Get-NetIPAddress
#Example

#If all code succesfully executed then $status will be Success
$Status = "Success"
}

Catch{
#On script failure $Status will be Failed and the error message sent to $ErrorMessage
$ErrorMessage = $_.Exception.Message
$Status = "Failed"
}

Finally{
#Will always return $Status and $ErrorMessage
$return = @($Status, $ErrorMessage, $IPAddress)
$return
}
}
#Get Return values
$Status = $Return.Get(0)
$ErrorMessage = $Return.Get(1)
$IPAddress = $Return.Get(2)

You then need to create a published data for each variable you want to return from the activity. I always put the $Status and $ErrorMessage as published data as minimum to keep some sort of consistency.

runbokpub

Then make a new Run .Net Script activity below with the code to throw the error message from your script in the runbook.

runbookthrow

And make a link that will be triggered if $Status is not equal Success.

runbooklink

You could also add a invoke runbook after the Error activity if you have a runbook to add something to a error log of some sort, in my case i use the Analyst log in Service Manager.

runbookcomment

Here is the webinar recording

MPTool, Automate and Simplify Management Pack Development

This is something I have been looking forward to for a long time! The first public release of the PowerShell module MPTool, it is a module for automating and simplify OpsMgr management pack development I have been working on for almost a year together with Martin Dyrlund Arnoldi

Back in the end of 2014 we started introducing automation and self-service into our company and spend the most of 2015 automating common tasks around our daily server provisioning and de-provisioning work. In the end of 2015 we looked into automation for SCOM as we couldn’t keep up with the work the way we did it at the current point. We needed a way to automate our management pack creation to be able to create them faster and more standardized, and so we came by the awesome PowerShell module OpsMgrExtended from Tao Yang. We started to play around with Tao’s module and quickly figured out that the concept of module was exactly what we needed, but also that we required the ability to create more advanced monitoring solutions, so we decided to build our own PowerShell module, and the result is this, MPTool.

MPTool is created with the goal that if you know PowerShell you should pretty easily be able to create management packs. It contains a lot of build-in logic in each function, it automatically adds management pack references if needed, for a PowerShell discovery you only need to create a PowerShell array with the discovery data and the function it self will create the SCOM specific code needed, and so on.

We have now with great success used it internally for more than 6 months and is now ready to share it with the community, I hope you will all find it as useful as us!

Over the next weeks I will post some more articles on how you can use this module in different examples

Please report if you hit any Bugs or troubles so we can improve where it is needed, also if you have any idea for future releases we will take it in and evaluate.

Detailed documentation is available at GitHub

Download Link

GitHub Repository

16 CmdLets is available now, and a few more is already in testing and will be added soon.

New-MPToolManagementPackAlias
Get-MPToolManagementPackReferenceAlias
New-MPToolManagementPack
New-MPToolOverrideManagementPack
Add-MPToolManagementPackReference
New-MPToolApplicationComponentClass
New-MPToolComputerRoleClass
New-MPToolLocalApplicationClass
New-MPToolClass
New-MPToolWindowsEventAlertRule
New-MPToolFilteredRegistryDiscovery
New-MPToolPSDiscovery
New-MPToolPSStateMonitor
New-MPToolWindowsServiceMonitor
New-MPToolDependencyMonitor
New-MPToolHostingRelationship

 

Cleaning up unused management packs.

I recently did some house keeping in our OpsMgr environment, cleaning up old and unused Management Packs, and to easily identify potential unused Management Packs I of course turned to PowerShell!

This script can walk through the management packs and get the number of instances for each class. If there is no instances of a class it is very likely that it is not used. After running this script you will end up with a list of potential unused classes, you can now check each management pack for it’s content and see if there is other classes in it that is in use.

################################################################
#   Author: Andreas Sobczyk, CloudMechanic.net
#
#   Get Empty classes from Management Packs
################################################################

## Get all management packs starting with Contoso.
$MPs = Get-SCOMManagementPack -name Contoso*

## For each MP, For each Class, count classinstances.
$Overview = @()
Foreach($MP in $MPs){
$Classes = $mp.GetClasses()

foreach($Class in $Classes){
$ClassInstance = $null
$ClassInstance = Get-SCOMClassInstance -Class $class

## If you want you can change the number of class instances to sort on.
if($ClassInstance.Count -le 0){
$Overview += $Class
}
}
}

$Overview | Format-Table Name,Displayname,ManagementpackName

GitHub Download