How to Add device to collection using task sequence- troubleshooting

I was recently looking for a solution to add a device to SCCM Collection using the Task sequence. During the search, I have found Trevor has a blog post on this.

So before I use the code in the task sequence,  I plan to test it on a client device.

The full code is given below. You will have to change the parameters where needed.

cmd /c powershell.exe -Command "&{$Me = $Env:COMPUTERNAME; Invoke-command -Computername "SCCMSERVERFQDN" -ArgumentList $Me -scriptblock {param($Me)Import-Module ConfigurationManager; cd SITECODE:; $Computer = Get-CMDevice -Name $Me; if ($Computer){Add-CMDeviceCollectionDirectMembershipRule -CollectionID 'COLLECTIONID' -ResourceId $computer.resourceid}}}"

Query to add device to collection

Login to client computer that doesn't have any configuration manager module installed, open the cmd (run as an account that has RBAC permissions to collect to SCCM server)

Running the query, returned an error code.

The specified module 'ConfigurationManager' was not loaded because no valid module file was found in any module directory.

Cannot find drive. A drive with the name 'CB1' does not exist.

Since I am running the code on a client device, i would like to see if this works on SCCM server (local) , I experienced the same error code. Even though the server has configuration manager module loaded but for some reason, it is not picking up or identifying correctly.

During the troubleshooting, I have found that, the configuration manager module path is missing in the system variable.

The fix is given below.

On your SCCM server, go to system properties or you can run sysdm.cpl from run command.

Open system properties

Click on advance, Environment Variables

Copy the variable value for variable name SMS_ADMIN_UI_PATH.

Now, select PSModulePath and click on edit

Click on new and copy the variable value that you have copied earlier (remove i386).

G:\Program Files\Microsoft Configuration Manager\AdminConsole\bin

Click on Ok, Ok, Ok

Changes are now saved.

lets go back to the client PC and run the query again.

This time it executed successfully. I can now use this query in my task sequence to add the device to specific collection.

Hope you find this article useful!

Post Comment