Error when trying to use PowerShell ApplyTemplate
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_Winter Customer Advanced IT Monkey ✭✭✭
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.
0
Answers
I have several questions, but I think the best one to start with is:
My bad, I meant the SCSM Activity layout not the SCO layout.
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.
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.
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.
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.
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.
Possible issue with the 32 vs 64 bit PS support? In which case your options are
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.