Applying SR template using Powershell
So with a bit of looking around, I have found various parts of scripts and ideas and come up with the below to apply a template as a run.net script action.
Import-Module SMlets<br><br>#Variable / Class Setup<br>$srClass = Get-SCSMClass -name System.WorkItem.ServiceRequest$<br>$srprior = Get-SCSMEnumeration -Name ServiceRequestPriorityEnum.Medium<br>$srurg = Get-SCSMEnumeration -Name ServiceRequestUrgencyEnum.Medium<br>$ararea = get-SCSMEnumeration -Name ServiceRequestAreaEnum.Software.Installation<br>$title = "Application Template Test 3"<br><br>#Service Request Arguements<br>$srargs = @{<br>Title = $title;<br>Urgency = $srurg;<br>Priority = $srprior;<br>ID = "SR{0}";<br>Area = $ararea;<br>Description = "Remove Test"<br>}<br><br><br>#Create Service Request<br>$newserviceRequest = New-SCSMOBject -Class $srClass -PropertyHashtable $srargs<br>$serviceRequestId = $newServiceRequest.ID<br><br>#Pause<br>Start-Sleep -s 40<br><br>#Get Projection & Object for Created Service Request<br>$srTypeProjection = Get-SCSMTypeProjection -name System.WorkItem.ServiceRequestProjection$<br>$SRProj = Get-scsmobjectprojection -ProjectionName $srTypeProjection.Name -filter “title -eq $title”<br><br>#Get Template<br>$template = Get-SCSMObjectTemplate -DisplayName "SC REQUEST: Software Request"<br>#Set Template<br>Set-SCSMObjectTemplate -Projection $SRProj -Template $template<br>}
It all works fine until we have to get the SR to apply template, its returning a null value:
Get-SCSMObjectProjection : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ProjectionName'. Specified method is not supported.<br>At line:33 char:52<br>+ $SRProj = Get-scsmobjectprojection -ProjectionName $srTypeProjection.Name -filte ...<br>+ ~~~~~~~~~~~~~~~~~~~~~~<br> + CategoryInfo : InvalidArgument: (:) [Get-SCSMObjectProjection], ParameterBindingException<br> + FullyQualifiedErrorId : CannotConvertArgument,SMLets.GetSMObjectProjectionCommand<br> <br>Set-SCSMObjectTemplate : Cannot bind argument to parameter 'Projection' because it is null.<br>At line:38 char:36<br>+ Set-SCSMObjectTemplate -Projection $SRProj -Template $template<br>+ ~~~~~~~<br> + CategoryInfo : InvalidData: (:) [Set-SCSMObjectTemplate], ParameterBindingValidationException<br> + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,SMLets.SetSCSMObjectTemplateCommand
I've tried several variations of the script and it always returns null at the get SR stage.
Really appreciate if anyone has any ideas?
Comments
I would have a read of this article, it should point you in the right direction:
https://blogs.technet.microsoft.com/servicemanager/2011/04/04/properly-querying-scsm-using-smlets-get-scsmobject-cmdlet/
Allows you to create any type of work item with a template from orchestrator.
Problem solved
I have a script where submit a Request Offering and then apply a template to the newly made Service Request.
Submit SCSM Request Offering PowerShell Script
https://gallery.technet.microsoft.com/Submit-SCSM-Request-22b0488cSet-SCSMTemplateWithActivities powershell script
http://blog.coretech.dk/mme/set-scsmtemplatewithactivities-powershell-script/