Add a Device to a Collection during a Task Sequence

Here’s a “quick and dirty” way to add a computer to a collection during a task sequence.  It uses PSRemoting to your ConfigMgr Site server and will happily run where only PowerShell 2.0 is installed.

It uses the Run Command Line step to invoke PowerShell and run the necessary commands on the site server to add the machine to the specified collection.  You run this step with an account that has the necessary permissions.

  1. Add a new TS step “Run Command Line”
  2. Paste the code below into the Command line window
  3. Change the following items in CAPS in the code:
    1. MYSITESERVER (ConfigMgr Site Server Name, or anywhere with the ConfigMgr Console / Cmdlets installed)
    2. ABC (Site Code)
    3. COLLECTIONNAME (The name of the target collection)
  4. Change the “Run this step as…” account to an account that has the necessary permissions on the site server

Code:


cmd /c powershell.exe -Command "&{$Me = $Env:COMPUTERNAME; Invoke-command -Computername MYSITESERVER -ArgumentList $Me -scriptblock {param($Me)Import-Module ConfigurationManager; cd ABC:; $Computer = Get-CMDevice -Name $Me; if ($Computer){Add-CMDeviceCollectionDirectMembershipRule -CollectionName 'COLLECTIONNAME' -Resource $Computer}}}"

Capture

Advertisements

6 thoughts on “Add a Device to a Collection during a Task Sequence

  1. Hi could you advise me as im not so clever
    I am running your Script but doesn’t appear to Run
    when i run it directly on a client i recieve the following could you please comment

    Missing condition in if statement after ‘if (‘.
    At line:1 char:169
    + &{ = ROGERTESTSCCM2; Invoke-command -Computername CCISCCM1P-ArgumentList -sc
    riptblock {param()Import-Module ConfigurationManager; cd ABC:; = Get-CMDevice
    -Name ; if ( <<<< ){Add-CMDeviceCollectionDirectMembershipRule -CollectionName
    'NewBuild_TestDeploy' -Resource }}}
    + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordEx
    ception
    + FullyQualifiedErrorId : IfStatementMissingCondition

    1. Can you post the code you are running? Based on the variable values I can see in the error, this code should work. You will need to change the site code “ABC” and the ‘COLLECTIONNAME’ first:

      cmd /c powershell.exe -Command “&{$Me = ‘ROGERTESTSCCM2’; Invoke-command -Computername ‘CCISCCM1P’ -ArgumentList $Me -scriptblock {param($Me)Import-Module ConfigurationManager; cd ABC:; $Computer = Get-CMDevice -Name $Me; if ($Computer){Add-CMDeviceCollectionDirectMembershipRule -CollectionName ‘COLLECTIONNAME’ -Resource $Computer}}}”

  2. THis is what i am running
    i had Omitted changing the site code !

    cmd /c powershell.exe -Command “&{$Me = $Env:COMPUTERNAME; Invoke-command -Computername ccisccm1p -ArgumentList $Me -scriptblock {param($Me)Import-Module ConfigurationManager; cd PRI:; $Computer = Get-CMDevice -Name $Me; if ($Computer){Add-CMDeviceCollectionDirectMembershipRule -CollectionName ‘NewBuild_Testdeploy’ -Resource $Computer}}}”

    THis is the reply error
    At line:1 char:164
    + … ce -Name ; if (){Add-CMDeviceCollectionDirectMembershipRule -CollectionName ‘New …
    + ~
    Missing condition in if statement after ‘if (‘.
    + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : IfStatementMissingCondition

    Thanks

    1. It seems the $computer variable is not being populated. It’s possible the ConfigurationManager module is not being imported, you might need to make it visible first, check out this script for that: https://gallery.technet.microsoft.com/scriptcenter/Make-Configuration-Manager-04474a87

      If it’s still not working after that, I suggest to run each command individually to see what’s not working. Are you sure the account you are using has the appropriate permissions and PowerShell remoting is enabled?

      From the client you can try, in PowerShell ISE (run each line one at a time):
      $Me = $Env:COMPUTERNAME
      $Session = New-PSSession -ComputerName ccisccm1p
      Invoke-command -Session $Session -ScriptBlock { Import-Module ConfigurationManager }
      Invoke-command -Session $Session -ScriptBlock { cd PRI: }
      Invoke-command -Session $Session -ArgumentList $Me -ScriptBlock { Param($Me); $Computer = Get-CMDevice -Name $Me }
      Invoke-command -Session $Session -ScriptBlock { if ($Computer){Add-CMDeviceCollectionDirectMembershipRule -CollectionName ‘NewBuild_Testdeploy’ -Resource $Computer} }
      Remove-PSSession $Session

      You can also try each command on the site server itself, without the pssession / invoke-commands.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s