Home Orchestrator

Error when trying to use PowerShell ApplyTemplate

Brian_WinterBrian_Winter Customer Advanced IT Monkey ✭✭✭

I am using a Runbook to create another RB (further down the workflow), if certain criteria is met.

First, I use a Create Object widget to create the RB, then pass the ID to a PowerShell script. When trying to execute:


Import-Module Smlets

$Projection = Get-SCSMTypeProjection Microsoft.SystemCenter.Orchestrator.RunbookAutomationActivity.Projection -ComputerName "w903smmq01"

$RBObject = get-scsmobjectprojection $projection -filter "Id -eq 'RB410165'" -ComputerName "w903smmq01"

$template = Get-SCSMObjectTemplate -displayname "Update New Workstation RB" -ComputerName "w903smmq01"

$RBObject.__base.ApplyTemplate($template) 

$RBObject.__base.Commit()

Remove-Module SMLets


On the ApplyTemplate command (3rd from the bottom), I get the following error:


Exception calling "ApplyTemplate" with "1" argument(s): "The key value of an object cannot be changed."

At line:1 char:1

+ $RBObject.__base.ApplyTemplate($template)

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  + CategoryInfo     : NotSpecified: (:) [], MethodInvocationException

  + FullyQualifiedErrorId : InvalidOperationException


Any help is appreciated!

Best Answer

  • Brian_WinterBrian_Winter Customer Advanced IT Monkey ✭✭✭
    Answer ✓

    Took a lot of digging but finally found the problem. We were running PowerShell in the "Run .Net" activity which was failing to access the SMLets module (even though it's installed and configured correctly).

    We had to use the "Execute PS Script" from the PowerShell IP. The script executed perfectly the first time! Lesson learned.

Answers

  • Adam_DzyackyAdam_Dzyacky Product Owner Contributor Monkey ✭✭✭✭✭

    I have several questions, but I think the best one to start with is:

    • What's your Activity process flow? Could you share a picture of the current state/where you're trying to create the Runbook?
  • Brian_WinterBrian_Winter Customer Advanced IT Monkey ✭✭✭
  • Adam_DzyackyAdam_Dzyacky Product Owner Contributor Monkey ✭✭✭✭✭

    My bad, I meant the SCSM Activity layout not the SCO layout.

  • Brian_WinterBrian_Winter Customer Advanced IT Monkey ✭✭✭

    Funny, even tho we get the error, it appears the template was applied. RB10160 generates the rest of the workflow. RB410158 is the RB that the template is being applied.



  • Brian_WinterBrian_Winter Customer Advanced IT Monkey ✭✭✭

    Stand by... RB410169 is the RB that SHOULD have been updated, not RB410165. Looks like the script is trying to update the wrong RB object.

  • Adam_DzyackyAdam_Dzyacky Product Owner Contributor Monkey ✭✭✭✭✭

    Excellent! Thanks for clearing that up because here's my thought:

    Why not just put the Runbook Activity there in the SR template so it's always there and always mapped. But! if your criteria is met, via Orchestrator just mark the downstream RB as Skipped so it never engages.

  • Brian_WinterBrian_Winter Customer Advanced IT Monkey ✭✭✭

    That's a good plan B. There is a small risk that an Analyst will unskip the RB. Would be better to delete the RB if not needed.

  • Brian_WinterBrian_Winter Customer Advanced IT Monkey ✭✭✭

    Here's the kicker. We're moving from Orch 2012 to 2016. The exact same code works for the 2012 Production (creates the RB and applies the template, np). Exported and imported the RB into 2016 and no dice. I even removed and reinstalled SMLets.

  • Adam_DzyackyAdam_Dzyacky Product Owner Contributor Monkey ✭✭✭✭✭

    Possible issue with the 32 vs 64 bit PS support? In which case your options are

    • PowerShell sessions for 64bit
    • Modifying the UseOnlyLatestCLR registry key to force 64 bit PowerShell
  • Brian_WinterBrian_Winter Customer Advanced IT Monkey ✭✭✭
    Answer ✓

    Took a lot of digging but finally found the problem. We were running PowerShell in the "Run .Net" activity which was failing to access the SMLets module (even though it's installed and configured correctly).

    We had to use the "Execute PS Script" from the PowerShell IP. The script executed perfectly the first time! Lesson learned.

Sign In or Register to comment.