How to create CR and apply template?

Nov 17, 2010 at 11:01 AM
Edited Nov 17, 2010 at 11:59 AM

Hi Jim

I need  create new CR using smlets and apply CR template

I try this way


$class = Get-SCSMClass -name System.WorkItem.ChangeRequest$
$parameters = @{Title="New CR";ID="CR{0}";TemplateId="StandardChangeRequest"}
New-SCSMObject -Class $class -PropertyHashtable $parameters

In created CR I see TemplateId="StandardChangeRequest" but list of activity is empty.It means template not apply.

 I read http://blogs.technet.com/b/servicemanager/archive/2010/11/07/creating-an-object-from-a-template-programmatically.aspx but it is not clear for me :(
When I try

$SMDIR = "C:\Program Files\Microsoft System Center\Service Manager 2010"
$COREDLL = "${SMDIR}/SDK Binaries/Microsoft.EnterpriseManagement.Core.dll"
[reflection.assembly]::LoadFile($COREDLL)

$EMG = new-object Microsoft.EnterpriseManagement.EnterpriseManagementGroup localhost
$tmp = $EMG.Templates.GetObjectTemplates()  | ?{$_.name -match "StandardChangeRequest"}
$classChangeRequest = $emg.EntityTypes.GetClass("E6C9CF6E-D7FE-1B5D-216C-C3F5D2C7670C")
$cemo = new-object Microsoft.EnterpriseManagement.Common.CreatableEnterpriseManagementObject($emg, $classChangeRequest)
$cemo.ApplyTemplate($tmp)
$cemo.Commit()

I obtain error
Exception calling "ApplyTemplate" with "1" argument(s): "Only a template targeted at a class can be applied to a single object."

Alex

Nov 30, 2010 at 7:47 AM

Hi Alex,

 

I was facing a similar issue so switched to using an EnterpriseManagementObjectProjection instead of just a plain old EnterpiseManagementObject and it resolved the issue.

I'm a little pressed for time at the moment but if you want an example reply back and I'll post it.

 

Cheers,

Brett