none
Powershell Set-CMTSStepSetDynamicVariable -AddRule RRS feed

  • Question

  • I'm trying to figure out, how to automate rules step in "Set Dynamic Variables", in the documentation it says :

    Set-CMTaskSequenceStepSetDynamicVariable -AddRule ($rule5,$rule6)

    How would one write $Rule5 ?

    If I run Get-CMTSStepSetDynamicVariable -StepName "Set Dynamic Variables" I get:

    Rules                 : {
                            instance of SMS_TaskSequence_Rule
                            {
                                Condition =
                            instance of SMS_TaskSequence_Condition
                            {
                                Operands = {
                            instance of SMS_TaskSequence_VariableConditionExpression
                            {
                                Operator = "like";
                                Value = "*T460s";
                                Variable = "Model";
                            }};

    Do I need to put it all in a Array  or a Hashtable?



    • Edited by B.R1984 Tuesday, June 11, 2019 10:16 AM
    Tuesday, June 11, 2019 10:08 AM

Answers

  • There may be a prettier way, but i got it to work like this somewhat based on the previous example:

    $ts = Get-CMTaskSequence -Name TestTS
    $StepName = 'Dynamic Variables'
    
    # A rule to be replaced:
    $hashRule1 = @{'isTablet' = 'True'}
    $rule1 = New-CMTSRule -Variable $hashRule1 -Make 'Microsoft' -Model 'Surface'
    $ts | Add-CMTaskSequenceStep -Step (New-CMTaskSequenceStepSetDynamicVariable -Name $StepName -AddRule $rule1)
    
    # A rule that should stay the same:
    $hashRule2 = @{'isServer' = 'False'; 'isWorkstation' = 'True'}
    $rule2 = New-CMTSRule -Variable $hashRule2 -Make 'HP' -Model 'Probook'
    $ts | Set-CMTaskSequenceStepSetDynamicVariable -StepName $StepName -AddRule $rule2
    
    # A rule to be added:
    $hashRule3 = @{'isServer' = 'True'; 'isWorkstation' = 'False'}
    $Rule3 = New-CMTSRule -Variable $hashRule2 -Make 'Dell' -Model 'PowerEdge'
    

    Then to actually replace rule1 with rule3:

    # Get the current rules:
    $Rules = ($ts | Get-CMTaskSequenceStep -StepName $StepName).Rules | Where {
        ($_.Condition.Operands.Make -notmatch 'Microsoft' -and $_.Condition.Operands.Model -notmatch 'Surface')
    }
    
    # Add the new rule and wipe all the others:
    $ts | Set-CMTaskSequenceStepSetDynamicVariable -StepName $StepName -AddRule $rule3 -CleanRule
    
    # Re-add the previously existing ones:
    $Rules | ForEach-Object {
        $ts | Set-CMTaskSequenceStepSetDynamicVariable -StepName $StepName -AddRule $_
    }


    • Marked as answer by B.R1984 Friday, August 2, 2019 9:08 AM
    Thursday, August 1, 2019 1:18 PM

All replies

  • I think it want's an IResultObject, you might have to create a rule, reference it in PowerShell and store in a variable then pass that var. Not sure tbh.

    Also came across this

    https://docs.microsoft.com/en-us/powershell/module/configurationmanager/convertto-cmiresultobject?view=sccm-ps

    I have not played with it tbh.


    Richard Knight | Collection Refresh Manager | Automate detection rules for patch \ msp files | Twitter

    Tuesday, June 11, 2019 1:53 PM
  • Create a hashtable with desired Variables and their values for each Rule.
    Create the Rule based on available criteria for the Cmdlet, same options as in the wizard.

    Then add the step to a TS or modify an existing one.
    The below example first adds it, then updates it with a second rule:

    $ts = Get-CMTaskSequence -Name TestTS
    
    $hashRule1 = @{'SomeVariable' = 'True'}
    $rule1 = New-CMTSRule -Variable $hashRule1 -Make 'Microsoft' -Model 'Surface'
    $ts | Add-CMTaskSequenceStep -Step (New-CMTSStepSetDynamicVariable -Name 'Dynamic Variables' -AddRule $rule1)
    
    $hashRule2 = @{'SomeOtherVariable' = 'False'; 'AndEvenMoreVariables' = 'True'}
    $rule2 = New-CMTSRule -Variable $hashRule2 -Make 'HP' -Model 'Probook'
    $ts | Set-CMTaskSequenceStepSetDynamicVariable -StepName 'Dynamic Variables' -AddRule $rule2
    


    Tuesday, July 16, 2019 8:15 AM
  • Thanks, Matthijs - Will try it, and get back to you when I’m back at the office again.
    Tuesday, July 16, 2019 9:25 AM
  • Did you get it to work?

    If not, let us know if there's anything else we can help with.

    Tuesday, July 30, 2019 1:09 PM
  • Hi, I just got back from holiday

    This is working perfectly, thanks :)

    How would you approach it, if you were to edit a Rule step ? There does not seem to be a Set or Get -CMTSRule ?

    Would you just delete the old rule, and create a new one, or is there a better way of doing it ?

    The "Set-CMTaskSequenceStepSetDynamicVariable" has a "-CleanRule" parameter, but that clears all rules on that step.






    • Edited by B.R1984 Thursday, August 1, 2019 11:03 AM
    Thursday, August 1, 2019 7:32 AM
  • There may be a prettier way, but i got it to work like this somewhat based on the previous example:

    $ts = Get-CMTaskSequence -Name TestTS
    $StepName = 'Dynamic Variables'
    
    # A rule to be replaced:
    $hashRule1 = @{'isTablet' = 'True'}
    $rule1 = New-CMTSRule -Variable $hashRule1 -Make 'Microsoft' -Model 'Surface'
    $ts | Add-CMTaskSequenceStep -Step (New-CMTaskSequenceStepSetDynamicVariable -Name $StepName -AddRule $rule1)
    
    # A rule that should stay the same:
    $hashRule2 = @{'isServer' = 'False'; 'isWorkstation' = 'True'}
    $rule2 = New-CMTSRule -Variable $hashRule2 -Make 'HP' -Model 'Probook'
    $ts | Set-CMTaskSequenceStepSetDynamicVariable -StepName $StepName -AddRule $rule2
    
    # A rule to be added:
    $hashRule3 = @{'isServer' = 'True'; 'isWorkstation' = 'False'}
    $Rule3 = New-CMTSRule -Variable $hashRule2 -Make 'Dell' -Model 'PowerEdge'
    

    Then to actually replace rule1 with rule3:

    # Get the current rules:
    $Rules = ($ts | Get-CMTaskSequenceStep -StepName $StepName).Rules | Where {
        ($_.Condition.Operands.Make -notmatch 'Microsoft' -and $_.Condition.Operands.Model -notmatch 'Surface')
    }
    
    # Add the new rule and wipe all the others:
    $ts | Set-CMTaskSequenceStepSetDynamicVariable -StepName $StepName -AddRule $rule3 -CleanRule
    
    # Re-add the previously existing ones:
    $Rules | ForEach-Object {
        $ts | Set-CMTaskSequenceStepSetDynamicVariable -StepName $StepName -AddRule $_
    }


    • Marked as answer by B.R1984 Friday, August 2, 2019 9:08 AM
    Thursday, August 1, 2019 1:18 PM
  • Thanks Matthijs, for pointing me in the right direction, you have been really helpful

    I can now continue automating the rest of my script.

    I'm planing to make a executable file as a Powershell script, so my colleagues can easily update the ts to all the latest drivers and BIOS packages, whenever new packages have been imported (that task is automated as well) 

    It could be a lot easier if the SCCM command-lets could be used in a TS environment, but from my understanding this can't be done, due to TS is running under system context, and we don't want to be depending on a web service.

    Friday, August 2, 2019 9:15 AM