Azure Automation Week–Get Input from the SCSM Portal

2 minute read

Most of the time when we are imaging computers, there is a reason for it. This reason will be tracked in some form of ticketing system, so why are we asking questions in the task sequence? We have all the answers already! In this post, I’m going to show you how to get the answers to portal questions in SCSM, and then return those as JSON!

In order to do this, there are some hurdles we must overcome. They are:

1) HybridRunbookWorkers run under the System context 2) String is returned, not an object 3) SCSM

One of the newer features of Azure Automation is the ability to change the context your hybrid worker runs under. Up until a few months ago we could only run scripts under the System context. Now, we have the ability to run under any user context by changing one simple setting:

Go to portal.azure.com and open up your Azure Automation account.

image

Select Hybrid Worker Groups, then select your group name:

image

image

Notice how it says “Run As –“? This is telling us the run as account is the default. On the right side, click Hybrid worker group settings, and change it from Default to Custom:

image

Be sure to only give the hybrid worker the least amount of privileges it needs! In my case, I’ll select Domain Administrator…

image

Click Save and you’ll see Run As change:

image

Now that we have our hybrid worker running under the correct credentials, we can continue onto our script! I created a new PowerShell runbook called SCSMPortalQuestionsAnswers for this.

First, I need to ask for the ticket ID, get the name of the SCSM server, and open a PSSession to the SCSM Server:

param (
    $TicketID
)

$SCSMServer = Get-AutomationVariable -Name "SCSMServer"

$Session = New-PSSession -ComputerName $SCSMServer

Now I need to get all the questions and answers from the SCSM portal. I’ll use the SMELTS to get this:

$GetResultsScriptBlock = {
    $TicketID = $args[0]
    Import-Module 'C:\Program Files\Common Files\SMLets\SMLets.psd1'
    $RequestClass = Get-SCSMClass -Name System.WorkItem.ServiceRequest$ 
    $WorkItem = Get-SCSMObject -Class $RequestClass -Filter "ID -eq $TicketID"
    [xml]$UserInput = $WorkItem.UserInput
    $QuestionAnswers = @{}
    $UserInput.UserInputs.UserInput | ForEach-Object {
        $QuestionAnswers[$_.Question] = $_.Answer
    }
    return (ConvertTo-Json $QuestionAnswers)
}

If you notice, I’m converting the object I’m creating to json. If you ever want to send an object back through the REST API, this is how you have to do it.

Lastly, I need to call the scriptblock and return the string!

return (Invoke-Command -Session $Session -ScriptBlock $GetResultsScriptBlock -ArgumentList $TicketID)

Now, I simply need to create a request offering for imaging computers in SCSM and publish it to the portal.

image

Once I answer those questions, I can take the ticket ID and send it to my runbook:

image

The results are exactly what I expect, an object with the questions and answers:

image

And there you have it! I can now either take the HTA out of my task sequence or greatly reduce the number of questions I ask in my task sequence. For instance, I could simply ask for the ticket ID and get all the questions answered! Or I could ask for the MAC address in the ticket and make this a zero touch!

Leave a Comment