Tag Archives: powershell

SCSM workflow to add UserInput to work item description

I often get the question from customers that are using SCSM how can we get the user input to the description. Out-of-the-box SCSM will place this in a field called UserInput.

This can be frustrating for some customers because they want to be able to send out an email like this”your request is now registered with the following information”. We would just use the normal email templates and add UserInput it would look something like this.

This is not a very good formatting to use in a email to the end-user. Therefor i have created a SCSM PowerShell workflow that will check for all new SR and IR if they contain any UserInput and if it does it will add that to the description. If there already is a description the UserInput will be added below that text. So if we look at the example a before it will look like this instead.

Now this is much better for an end-user to read. The solution will take all the questions on every request offering there is in the system and add it to the description. The solution is not sealed so feel free to modify the code to fit your need.

Below is a code snip from the service request part where i will explain what i am doing.

First we get the service request object so we have something to work with.

Save the UserInput to a variable

We need an array to save all the question with answers so we call that one $UserInput and we have to check if there is a description already. If it is save it to the userinput array.

The next step is to go throw the Questions in the XML-code. We are doing a split to get each question/answer to a row and then go row to row and extract the information we need. And save the question with answers to the array  $userinput

The last step is to add all information saved in $userinput to the description on the work item.

Tip: If you want to send out an email with your case is now registered in our system i recommend to modify this workflow some. Make the workflow set a tag in the SR class for example and when that value is met you can trigger a new workflow to send out an email.

Why should i do this? If you have an email subscription with criteria send on object created the email can be sent before the user input workflow has run and you don’t get a complete description to the end-user.

This function is not excessive tested so it’s up to you to test it and find possible bugs. The full code is available at Technet gallery, Click here for a link


SCSM: Update child incidents with parent ID in the title using PowerShell

Here is a small post i wanted to share.

A customer wanted a easy way to see if a incident is related to a parent incident without having to go into the case. They are using the Cireson Portal and the portal does not show if an Incident is a child unless you go into the Incident (See picture below).

So in order to make a quick fix that does not require Cireson to make changes to the portal i created a PowerShell script that will look for active parent incidents and go into each child and update the title so it says <title> (Child object of IR213) for example (See picture below).

The script is pretty simple done and it might generate performance loss if you schedule it on a fast interval. But that’s up to you to decide what interval the script should run at.

The script


Azure Automation – Start Azure runbooks from your own computer with PowerShell

I have been using Orchestrator for a few year and SMA also. But SMA didn’t really take off at many customers due to the requirements of Azure Pack. However i recently started looking to Azure Automation because i really like it. So i will start doing some blogposts about Azure Automation now. As i am kind of new to this some posts will be really basic but as i progress to more advance runbooks etc. the posts will advance also.

The first post will be how can you connect with PowerShell to Azure Automation. This can be done with at least two-way. One is that you enter your own credentials and the other is with certificate.

Before you start you have to download the Azure PowerShell cmdlets. They can be found here: http://go.microsoft.com/fwlink/p/?linkid=320376&clcid=0x409

Enter your own credentials

This way might not be the best if you want to automate something that requires your own credentials. I recommend using certificate. However it’s easy to do this way.

Just enter the command

Now a browser till open and you can sign in to your account

To verify that it worked you can now run

The account you just added should be listed now.

Using Certificate

Run the command

A Internet browser will open and you have to login to azure and a certificate file will be downloaded.

When the file is downloaded we have to import it into our certificate storage.

To verify that the certificate is active we can run get-azureAccount

And the output should be something like this.

Id Type Subscriptions Tenants

— —- ————- ——-

BFB4C2F0B3A6000000009AE56D87F0C46B1C639BB6 Certificate 0e0000-e6de-40009f-b000c-a751f000bb651

We can also open a mmc.exe and load the certificate plugin and check the personal storage.

So what can we do now?

There are lots of commands we can utilize just see the list below.

For this post i will just show how we can call on a runbook in Azure from our computer.

    1. Create a runbook in Azure Automation

For this example i am using a parameter in the script to show you how we can pass a paramenter from our on-premis to Azure runbook. Remember to publish the runbook after you are done typing in the code.

workflow HelloWorld

In order to call the runbook from PowerShell we need to know the name of our AzureAutomation Account. You can see this easy on the portal.

  1. Start the runbook

Now we can call the runbook from PowerShell.

And if we look in the portal we can see the runbook is starting.

Why do we want to call a runbook from PowerShell on-prem?

Well i won’t go into details here but there are many reasons why we would like to do this. One example is if we have a Offering in Service Manager for example that is provision a new Azure VM. There is no AzureAutomation Connector so we can talk direct to Azure. But we could use a normal Orchestrator runbook or SMA-Runbook to call on AzureAutomation.

Close ALL work items in SCSM with PowerShell

I was at a customer that had several 1000 work items, Incident, Service Requests, Change, Activities etc. I wanted to close all items and make the test environment clean. So i created a PowerShell script based on SMlets that will take all work items and close them.

Be careful so you don’t run this script on a production environment :)


Service Manager 2012 PowerShell Exchange “Connector”

There is a Exchange connector for Service Manager but in my experience i think it lacks a bit of features. And it is not possible to configure it as much as i want to. To create a work around for the exchange connector i created a Powershell based “connector”

This is a kind of concept that shows you that you can create awesome stuff with Powershell and tweak you’re mail integration a lot more then with the normal connector.

The “connector” works this way, you schedule it on a management server for a interval of 5 minutes and it will use the exchange web service to look in a mailbox for emails. The script also depends on SMlets. You can download The Exchange web service here and SMlets here

By default it process all emails that has a IR prefix or SR prefix. If it finds a email that does not contain a valid prefix it is processed as trash and an email is sent to the mail sender that it contained a invalid case number.

You can change this behavior to allow all mail that does not contain a prefix or SR/IR so the “connector” will create a new incident with that content. The new incident will use a specified Incident template.

I have tested the script from a Windows 2012 R2 server with Service Manager 2012 R2 (works with UR4 also) and on a office 365 mailbox. It should work against Exchange 2010 for example if auto discovery is configured.

Functions in version 1.0

  • Incident Comments from email
  • Service Requests comments from email
  • Incident attached files added to the Incident
  • Service Request attached files added to the service request
  • Ability to create new incidents from email based on incident templates
  • If the sender of the email does not exists as a CI in service manager database an internal account will be created for that user.
  • All processed emails are kept in a folder in the mailbox
  • If the incident or service request is in the status closed the user gets an email saying the mail was not processed due to cases being closed.

Here is an example for a email that contains attached file. The subject for this email was IR122 and contained the file IR122.txt and the text “This is a test comment sent into IR122 with an attached file”


After the powershell script has run we can look in the action log and you can now see that there is an entry from me and a attached file (Bifogad fil in Swedish)


And on the related object page we can see the attached file.email_after_incident_process_relatedItems

Functions that i am working on in version 1.1

  • Be able to resolve incident by sending [resolved] in the email tag
  • Be able to complete service requests by sending [completed] in the email tag
  • Splitting the code into more functions and modules to get an more easy overview of the code


The script is available on technet gallery. Here is the link.

If you want something more added in future versions just throw in a comment below.


Service Manager PowerShell examples

I have almost used service manager for 1,5 year now and over the time i have created a bunch of PowerShell scripts to do lots of different things. But i have noticed that sometime it was really hard to find some examples to the code. So here is a list of some code examples that i hope will help you.

Not all examples are completed so you need to fill in some information but the basic of how you can do it are there. Got any question just add a comment and i will answer as soon as i can.

Almost all script examples requires you to install SMlets you can find it on codeplex or here

Add an analyst comment to a Incident

This example is for adding a comment to a incident. You can use the smlets set-scsmincident -id 333 -comment “Hello” if you want but in this case we get more flexible and can set if it’s private and so on.

Add an end-user comment to incident

In this case we can use this example or look at the example below “Add an analyst comment to an incident” and modify it so the AnalystComment is set to EndUserComment instead. More like “Add an end-user comment to service request”

Add an end-user comment to Service Request

This example shows you how you can add a end-user comment to a service request. You need the $SCSMObject from a get-scsmobject.

Apply a template to an incident

This examples lets you set a template on a incident. You need to get your template ID by issue get-scsmobjecttemplate. Se example below.

Now get the Name of your template and add it to the template variable below.

Upload an attachment to a service request

This is not my own script. A college of mine wrote it. Thanks Evert. But it allows you to add attachements to a service request. All files in the $directory will be added to a id in $SCSMID

Set affected service when creating an incident

This example allows you to set a affected service on your incident. In this example not all variables are visible but you can replace the $variables with your own on the propertyHashtable. In this example we are setting the business service Gnu as affected service.

Populate AD-user class with email-address from active directory

In this example i am populating the email to the note field. Best practice would be to extend the Microsoft.AD.User class and add a field called email or something.

Check for failed workflow jobs in SCSM using powershell

You might have seen that SCSM uses many workflows for different things. You might wanna take a look on the status of the jobs. If you go to your administration tab and workflows -> status you will se a big list.


You can manually go into each workflow and click on the need attention or all instance tab. This is time consuming and you might only want to see what jobs has failed.



One way to get this done is using powershell. If we run the script below.

The output will be something like this below. You can now easy parse all the workflows and check for failed jobs.



Be aware that this script is pretty resource heavy. So don’t let it run all the time.

Remove broken DPM Agents with Powershell

I ran into this small problem. I had a node in my DPM and the server was removed but not from the DPM-console. Now i can’t remove it from the normal rightclick on the agents tab. As you can se the Uninstall is grayed out. I can disable the agent but thats all i can do.


It is possible to remove the broken agent with powershell. Just run the command. Just replace the DPMSERVERNAME with the name of your DPM-server and replace the DPMAGENT with the agent you want to remove.


Now the agent is removed from the console.

Remove orphaned connector from Service manager 2012 SP1

Some time ago i had a problem with an connector and i decided to remove it. But i was unable to remove the connector from the console. It just gave me an error. To fix this i had to use powershell to remove the connector.

Step 1 – Identify the connector

Step 2 – Remove the connector

As you can se abow i have identifed an Exchange connector that i want to remove. So now i can run following powershell command to remove the connector.


SCSM2012: Howto customize forms to show email address populated form active directory

You might have seen that there is not a simple way to get the email address to the incident form or any form in service manager 2012. I don’t know why Microsoft did’nt just create a class that has the affected.user.email but it’s not there.

In my case i created a powershell script to update the CI of the user and i have chosen to add the email address to the notes field (i had to use a current field beacuse if i added a new the forms could not read from that field). There might be some better ways to do this but it works fine for me.

Extend the form
First thing to do is to add a field called email somewhere on your form. Se picture below.

If you are doing the same as me you need to change the binding of the field to affectedUser.Notes

Configure the powershell script
Create a powershell script on your ServiceManager managementserver. Now copy the code below. I will try to explain the code abit.

We first get the Microsoft.AD.User class and save it to the variable $UserCI. Now we loop thru all the users and get the emailaddress and saves it to ADUser variable. Now we compare if the CI.Notes have the same as the directory. If it’s empty we save the new email using Update-SCCLassInstance

$UserCIs = Get-SCClassInstance -class (Get-SCClass -name 'Microsoft.AD.User')
foreach ($UserCI in $UserCIs)
$ADUser = Get-ADUser -Filter "SamAccountName -eq '$($UserCI.UserName)'" -Server pulsen.prod -Properties EmailAddress
if ($UserCI.Notes -ne $ADUser.EmailAddress) {
$User = "UserName -eq $($UserCI.UserName)"
Get-SCClassInstance -class (Get-SCClass -name 'Microsoft.AD.User') -filter $User | %{ $_.Notes = $($ADUser.EmailAddress); $_ } | Update-SCClassInstance



Now set the script to run once each day or more if you need it to. And import the management pack you created for the form and it should look something like the picture below when you search for a user.