locked
Apply template to ChangeRequest Object via Powershell RRS feed

  • Question

  • I am trying to apply a notification template to a change request via powershell.

     $CR_ID = "CR611"
     
     $CRClass = get-scsmclass -name System.WorkItem.ChangeRequest$
     $CRobj = Get-SCSMObject -Class $CRClass -Filter "ID -eq $CR_ID"
     $CMN11 = Get-SCSMObjectTemplate Template_ae893ef64e1e4fd0a2e7f27753abdbe3
     
     #if ($CRobj.ScheduledEndDate -gt (get-date).today) {$CRobj.ApplyTemplate($CNM11)}
     
    

    Object information and error message:

    PS H:\> $CRobj.ApplyTemplate.OverloadDefinitions
    System.Void ApplyTemplate(Microsoft.EnterpriseManagement.Configuration.ManagementPackObjectTemplate template, Microsoft.EnterpriseManagement.Common.ObjectTemplateCallbac
    k callback)
    System.Void ApplyTemplate(Microsoft.EnterpriseManagement.Configuration.ManagementPackObjectTemplate template)
    
    ______________________________________________________________________________________________________________________________________________________________________
    PS H:\> $CMN11.GetType()
    
    IsPublic IsSerial Name                                     BaseType                                                                                                      
    -------- -------- ----                                     --------                                                                                                      
    True     True     ManagementPackObjectTemplate             Microsoft.EnterpriseManagement.Configuration.ManagementPackElement                                            
    
    
    
    ______________________________________________________________________________________________________________________________________________________________________
    PS H:\> $CRobj.GetType()
    
    IsPublic IsSerial Name                                     BaseType                                                                                                      
    -------- -------- ----                                     --------                                                                                                      
    True     True     EnterpriseManagementObject               Microsoft.EnterpriseManagement.Common.EnterpriseManagementObjectBaseWithProperties                            
    
    
    
    _____________________________________________________________________________________________________________________________________________________________________
    
    ______________________________________________________________________________________________________________________________________________________________________
    PS H:\> $CRobj.ApplyTemplate($CNM11)
    Exception calling "ApplyTemplate" with "1" argument(s): "Value cannot be null.
    Parameter name: template"
    At line:1 char:21
    + $CRobj.ApplyTemplate <<<< ($CNM11)
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : DotNetMethodException
     
    

    Not sure whats wrong??

    Joshua Fuente

    Tuesday, December 11, 2012 9:42 PM

Answers

  • If you're on SCSM 2012, when you configure the subscription, select "Periodically notify when objects meet a criteria" under "When to Notify".

    In your criteria, choose the Scheduled Start Date less than or equal to [now].

    You can then configure the notification to be sent once or on some interval.

    Give that a try and see if it accomplishes what you're looking for :)


    • Edited by Aaron Croasmun Wednesday, December 19, 2012 2:51 PM this works for SM2012
    • Marked as answer by NachoScript Friday, December 21, 2012 1:21 AM
    Wednesday, December 19, 2012 2:50 PM

All replies

  • Hi Joshua,

    the issue here's quite simple :) The template you are storing in the variable $CMN11. But for the ApplyTemplate method you're specifiying a variable called $CNM11. So it's just a small typo ;-)

    But even fixing the typo won't bring you success in what you're trying to do. Applying templates is a bit difficult using PowerShell (at least I find it difficult).

    The thing is you can't apply templates which have been created via console on single objects. This only works on object projections. That's because you can define relationships and other references to other classes within the template. But a single object can't 'handle' these information unlike a projection.

    You can get the object projection for your CR using these commands:

    $CRClassProjection = get-SCSMTypeProjection System.WorkItem.ChangeRequestProjection$
    Get-SCSMObjectProjection -Projection $CRClassProjection -Filter "Id -eq $CR_ID"

    Unfortunately the object you get returned does not have a member method called ApplyTemplate() like a standard CR object using the get-scsmobject cmdlet...

    That's as far as I got so far... Maybe it will help you a bit and point you in the right direction.

    Here's some interesting material to read through concerning this topic:

    http://social.technet.microsoft.com/Forums/en-IN/customization/thread/f9b06575-4f80-4f8d-bcea-576d4b975789

    http://smlets.codeplex.com/discussions/235007

    Cheers

    Alex

    EDIT: Okay it's easier than I thought :-D

    Just store the object projection into a variable and use the set-scsmobjecttemplate cmdlet with -projection and -template attribute.

    $CRClassProjection = get-SCSMTypeProjection System.WorkItem.ChangeRequestProjection$
    $CRP = Get-SCSMObjectProjection -Projection $CRClassProjection -Filter "Id -eq $CR_ID"
    Set-SCSMObjectTemplate -Projection $CRP -Template $CMN11

    So you can't use the member method .applytemplate() directly but there's another way to do that after all ;-)

    Good luck.


    Wednesday, December 12, 2012 8:50 AM
  • Thanks I will give that a try, I still don't quite understand the whole projections, relationships and enums just yet.


    Joshua Fuente

    Wednesday, December 12, 2012 2:24 PM
  • Having a bit of trouble with this approach, and I think it might be a bit of a bug in the SMlets.

    I get an error when trying to set the template,

    PS C:\>>> Set-SCSMObjectTemplate -Projection $CRP -Template $CMN11
    Set-SCSMObjectTemplate : The template cannot be applied to the instance because the instance is not of t
    he template targets type.
    At line:1 char:23
    + Set-SCSMObjectTemplate <<<<  -Projection $CRP -Template $CMN11
        + CategoryInfo          : InvalidOperation: (Template_ae893e...2e7f27753abdbe3:ManagementPackObject
       Template) [Set-SCSMObjectTemplate], InvalidOperationException
        + FullyQualifiedErrorId : ApplyTemplate,SMLets.SetSCSMObjectTemplateCommand
    

    And I think this is the cause,

    PS C:\>>> $CRP = Get-SCSMObjectProjection -Projection $CRClassProjection -Filter "Id -eq $CR
    _ID"
    PS C:\>>> $CRP.GetType()
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     False    PSCustomObject                           System.Object


    PS C:\>>> $CRClassProjection.GetType()
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     False    PSCustomObject                           System.Object

    And according to the cmdlet help, we are looking for a specific type of object,

    PS C:\>>> help Set-SCSMObjectTemplate
    
    NAME
        Set-SCSMObjectTemplate
    
    SYNTAX
        Set-SCSMObjectTemplate [-Projection] <EnterpriseManagementObjectProjection[]> [-Name <String>] [-Tem
        plate <ManagementPackObjectTemplate>] [-ComputerName <String>] [-Credential <PSCredential>] [-SCSMSe
        ssion <EnterpriseManagementGroup>] [-Verbose] [-Debug] [-ErrorAction <ActionPreference>] [-WarningAc
        tion <ActionPreference>] [-ErrorVariable <String>] [-WarningVariable <String>] [-OutVariable <String
        >] [-OutBuffer <Int32>] [-WhatIf] [-Confirm]
        Set-SCSMObjectTemplate [-Object] <EnterpriseManagementObject[]> [-Name <String>] [-Template <Managem
        entPackObjectTemplate>] [-ComputerName <String>] [-Credential <PSCredential>] [-SCSMSession <Enterpr
        iseManagementGroup>] [-Verbose] [-Debug] [-ErrorAction <ActionPreference>] [-WarningAction <ActionPr
        eference>] [-ErrorVariable <String>] [-WarningVariable <String>] [-OutVariable <String>] [-OutBuffer
         <Int32>] [-WhatIf] [-Confirm]
    
    
    
    

    Do you think this is a bug in SMlets?

    Can I "cast" my way around this? 


    Joshua Fuente

    Monday, December 17, 2012 2:11 PM
  • That's quite strange actually. It's working fine for me with variables of type PSCustomObject. No error messages at all.

    You could actually cast an EnterpriseManagementObjectProjection object var by using the SCSM DLLs (see also the thread on codeplex in my 1st reply). However, I don't think that's the issue in your case. Looking at the exception...

    + CategoryInfo          : InvalidOperation: (Template_ae893e...2e7f27753abdbe3:ManagementPackObject
    Template) [Set-SCSMObjectTemplate], InvalidOperationException

    ... it seems the template you're trying to apply is causing the issue. Have you tried it with another template? Please ensure the template you're trying to apply has the same class as its target as the object you're applying this template to (on a CR object you can only apply a CR template obviously). I suspect that's the problem.

    Cheers

    Alex 

    EDIT: Pleae be 'careful' with applying templates, this seems to require a lot of testing and also quite some PowerShell code to work 'properly'. I've just discovered for instance that activities which are added by the template don't have an appropriate activity prefix, only the workitem ID. Also the order of the activities got messed up (maybe because of the missing prefix?). Apparently you have to modifiy the template XML and specify the relevant prefixes for the Activities, and it seems to appear only when the template was applied by using any other method than the SCSM Console. See also this thread http://social.technet.microsoft.com/Forums/en-AU/systemcenterservicemanager/thread/2688c78f-38e5-49fd-9811-d6ff56158f55
    Tuesday, December 18, 2012 7:38 AM
  • Ok, so perhaps I am not using this cmdlet correctly.

    I was trying to apply a notification template to a CR, when several conditions are true.

    The functional people have not been able to accomplish this in the GUI, so I was trying with PowerShell.

    Is there a better approach for what I am attempting to do?


    Joshua Fuente

    Tuesday, December 18, 2012 5:54 PM
  • A notification template is for sending notifications, not for applying to work items. Your template needs to be a Change Request template. You can create them in the Library wunderbar in the console under Templates
    Tuesday, December 18, 2012 6:00 PM
  • yes, thats what I want to do, send notifcations, when certain conditions are true in the CR.

    My Activity templates, are all set and working as expected.

    My thinking was that I could apply a notification template, and have it fire off the notifcations.

    It seems I need to create the notifcation in the powershell script itself.(Send-MailMessage) 

    Would you agree?


    Joshua Fuente

    Tuesday, December 18, 2012 6:08 PM
  • If you want to send notifications when certain conditions become true in the CR, have you tried setting up a notification subscription?

    Tuesday, December 18, 2012 6:56 PM
  • I myself have not, the functional people on the team said they werent able to accomplish it that way.  Thats why I was tasked tryign to accomplish it with PS.

    What they want is for this email(I believe the template is already setup(CMN11)) to fire to change management when the implementation date on the CR has passed.

    I will take a look and see what I can figure out. 


    Joshua Fuente


    EDIT: sorry email to to be sent to the requestor to remidn them to fill in the implementation results fields.
    • Edited by NachoScript Tuesday, December 18, 2012 7:26 PM
    Tuesday, December 18, 2012 7:18 PM
  • I attempted to create the subscription, but when trying to add additional criteria,

    What I want is something like, Scheduled Start date -lessthan or equel to [Today] (there does not seem to be a function for [Today] or [now])

    Any ideas?


    Joshua Fuente


    • Edited by NachoScript Tuesday, December 18, 2012 7:27 PM
    Tuesday, December 18, 2012 7:26 PM
  • Well there's actually the possibility to work with relative dates. Please see this post:

    http://social.technet.microsoft.com/Forums/en-US/administration/thread/ec63ba64-2f5a-40e7-901a-06faefb17fb1

    For more complex notification triggers, I'm afraid, you have to work with Orchestrator Runbooks or PowerShell workflows and send the mail directly from Orchestrator / PowerShell (or change something on the work item you're monitoring and create a subscription to trigger when this particular change occured, then you could use the notifications templates created in SCSM.)

    Wednesday, December 19, 2012 7:42 AM
  • If you're on SCSM 2012, when you configure the subscription, select "Periodically notify when objects meet a criteria" under "When to Notify".

    In your criteria, choose the Scheduled Start Date less than or equal to [now].

    You can then configure the notification to be sent once or on some interval.

    Give that a try and see if it accomplishes what you're looking for :)


    • Edited by Aaron Croasmun Wednesday, December 19, 2012 2:51 PM this works for SM2012
    • Marked as answer by NachoScript Friday, December 21, 2012 1:21 AM
    Wednesday, December 19, 2012 2:50 PM
  • That does sound like it will work for this instance, I will give it a try.

    With regards to "Periodically notify when objects meet a criteria"

    Do you know that works, and if there is any way to modify it?  Ie, I would like reminders to come out in the morning hours...


    Joshua Fuente

    Wednesday, December 19, 2012 8:56 PM