none
How to add devices in bulk to a collection with PowerShell RRS feed

  • Question

  • Hi,

    I have a large number of devices that need to be added to a collection as direct members. After some research, I am seeing a common command line that is recommended to be used:

    Get-Content "D:\CollectionMembers.txt" | foreach { Add-CMDeviceCollectionDirectMembershipRule -CollectionID "USA003DA" -ResourceID (Get-CMDevice -Name $_).ResourceID }

    Every time I run this command, an error message is produced for each device in my TXT:

    Add-CMDeviceCollectionDirectMembershipRule : Cannot bind argument to parameter 'ResourceId' because it is null.
    At line:1 char:132
    + ... lectionID "USA003DA" -ResourceID (Get-CMDevice -Name $_).ResourceID }
    +                                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Add-CMDeviceCol...tMembershipRule], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.ConfigurationManagement.Cmdlets.Collections.Commands.AddDeviceCollectionDirectMembershipRuleCommand

    If I do a simple query on v_FullCollectionMembership, I can see that every device has a ResourceID but the script error is suggesting that ResourceID is coming back Null. If I use this command:

    (Get-CMDevice -Name MyComputer).ResourceID

    I also see no value returned. Does anyone happen to know why I would get this error when so many others claim that this command line works to add devices? Is the command line incorrect or could it be a permissions issue or something else? I am launching my PowerShell session using the SCCM console.

    Thank you!

    Rob


    • Edited by robwm1 Wednesday, July 3, 2019 3:00 PM
    Wednesday, July 3, 2019 2:59 PM

Answers

All replies

  • What version of ConfigMgr are you running?

    I don't see any specific issue with what you are doing here.

    When I run (Get-CMDevice -Name MyComputer).ResourceID against a valid computer in my 1902 lab, it returns the resource ID properly.

    Does Get-CMDevice -Name MyComputer return any valid info at all (when MyCOmputer is set to a valid computer name of course)?


    Jason | https://home.configmgrftw.com | @jasonsandys

    Wednesday, July 3, 2019 5:40 PM
  • Hi Jason,

    SCCM current branch 1902

    I had a suggestion to include the code given by the console when you start a PowerShell session. So I used that code and added the command line above along with the existing code. I still receive the same error, ResourceID is null.

    When I run your test, Get-CMDevice -Name MyComputer (with valid computer name) and nothing is returned at all. It just goes back to the command prompt with no data.

    Should I be able to run Get-CMDevice -Name MyComputer without invoking the code that connects the session to the console? It doesn't return any data regardless of the method used.

    Could this be a permissions issue? I am certain that I do not have full admin rights to SCCM but I am told I have permissions to run scripts.

    -Rob


    • Edited by robwm1 Wednesday, July 3, 2019 6:24 PM
    Wednesday, July 3, 2019 6:23 PM
  • > Should I be able to run Get-CMDevice -Name MyComputer without invoking the code that connects the session to the console?

    Well, that's a trick question really. You need to import the ConfigMgr module into the PowerShell session and you need to change the current location (aka cd) to the ConfigMgr drive after importing the module to run cmdlets from that module. This is standard PowerShell. The code that is automatically loaded into ISE when you launch it from the ConfigMgr console does this all automatically for you although there are other ways to do this including adding the commands to your profile. 

    If the module wasn't loaded in the current session though, you would get an actual error about the cmdlet being unknown so I doubt that's the source of the issue here.

    Your permissions in ConfigMgr could be the issue although I'm assuming that you're pretty much a full admin and can view the resource ID for resources in the console.

    Have you tried with another account just to validate?

    Have you restarting the WMI service on the system hosting SMS Provider (or rebooted completely)?

    Does the ConfigMgr PowerShell module load successfully and are you able to set the current location to the ConfigMgr drive?

    Have you tried accessing the data directly using WMI?

    Have you tried the operation on another system where the console is loaded?


    Jason | https://home.configmgrftw.com | @jasonsandys

    Wednesday, July 3, 2019 6:50 PM
  • Just as an alternative the now micro right click tools (recast which is free) have this built in.

    https://recastsoftware.com/

    Just grab 3.2 community edition f you don't already have it that is.


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


    • Edited by Richard.Knight Wednesday, July 3, 2019 7:27 PM
    • Marked as answer by robwm1 Thursday, July 11, 2019 5:45 PM
    Wednesday, July 3, 2019 7:25 PM
  • Hi Richard,

    I did bump into that possibility with Recast after I manually populated my collection.  :(

    I might use that going forward but I do have a strong interest in leveraging PowerShell against SCCM.

    Thank you very much for sharing that!

    -Rob

    Wednesday, July 3, 2019 8:13 PM
  • will try what you are trying in the lab as soon as i get some stuff out the way, might be a day or two max.

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

    Thursday, July 4, 2019 6:52 AM
  • Hello Rob,
     
    Try to just run "Get-CMDevice", can it returns results?
     
    If yes, try to run "Get-CMDevice | select name,resourceid", can it return results which include computer names you have tested? 
     
    If yes, pick the ResourceID and run "Get-CMDevice -ResourceId <YourResourceID>"? can it return the specific device?
     
    It would be very helpful if you could provide the results of above commands. It will help us identify the problem.
     
    Best Regards,
    Ray

    Please remembers to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Thursday, July 4, 2019 11:06 AM
  • Hello,
     
    I notice that you have not updated for several days. Has your issue been solved? Or is there any update?
     
    Feel free to feedback.
     
    Best Regards,
    Ray

    Please remembers to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Monday, July 8, 2019 12:33 PM
  • Hi Ray,

    I was on holiday so no work was done on this. There were a number of things suggested and it will take me a few days to respond.

    Thank you,

    Rob

    Monday, July 8, 2019 1:41 PM
  • have a good holiday!
     
    Best Regards,
    Ray


    Please remembers to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Tuesday, July 9, 2019 8:50 AM