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