locked
adding batch machines to collection RRS feed

  • Question

  • I have been trying to create a Power shell script that will grab a txt file and add machines to a certain collection but i keep getting the same error cant seem to figure it out, I have enough PS smarts to be really dangerous... 

    ERROR: 

    Add-CMDeviceCollectionDirectMembershipRule : Cannot bind argument to parameter 'ResourceId' because it is null.
    At line:3 char:88
    + ... ID -ResourceId $PCID
    +                    ~~~~~
        + CategoryInfo          : InvalidData: (:) [Add-CMDeviceCol...tMembershipRule], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.ConfigurationManagement.Cmdlets.Collections.Commands.AddDeviceCollectionDirectMembershipRuleCommand

    Script : 

    CD PS1:
    Set-Location PS1:
    #Get the content of the file and for each line run the CMDLET to add the machine to the collection
    $content = "path of txt file"
    $CollectionID = "BW10????"
    Get-Content $content
    ForEach($m in $content)
     { $PCID = $(Get-CMDevice -Name $m).ResourceId
       Add-CMDeviceCollectionDirectMembershipRule  -CollectionId $CollectionID -ResourceId $PCID
       Write-Host "$m was added to the collection"
    }

    thanks for the help 

    Dangerous PS user 

    Tuesday, August 2, 2016 5:47 PM

Answers

  • Hi,

    actually, I meant for you to run the two commands on the console, without all the other script (and I messed up in the second command, leaving in the variable) ^^

    Hm, this sounds like your text-file is buggy. Let's see, how does this work out for you:

    #Get the content of the file and for each line run the CMDLET to add the machine to the collection
    $path = "path of txt file"
    $CollectionID = "BW10????"
    $content = Get-Content $path
    ForEach($m in $content)
    {
        Write-Host "Processing: $m"
        if ($m.Length -lt 1)
        {
            Write-Host "Empty entry, skipping"
            continue
        }
        $PCID = (Get-CMDevice -Name $m).ResourceId
        Write-Host "Entry: $m | $PCID"
        try
        {
            Add-CMDeviceCollectionDirectMembershipRule -CollectionId $CollectionID -ResourceId $PCID -ErrorAction Stop
            Write-Host "$m was added to the collection" -ForegroundColor Green
        }
        catch { Write-Host "Failed to add $m to the collection! $_" -ForegroundColor Red }
    }

    Now, does this happen for all values, the first, any specific? Is there an empty line in there somewhere?

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by Texanaggie Wednesday, August 3, 2016 5:25 PM
    Wednesday, August 3, 2016 12:49 PM

All replies

  • One of the items in your text file does not match a named resource in ConfigMgr.

    You need to add a check after the Get-CMDevice to validate that it actually returned a device object and thus a ResourceID.


    Jason | http://blog.configmgrftw.com | @jasonsandys

    • Proposed as answer by Frank Dong Wednesday, August 3, 2016 5:39 AM
    Tuesday, August 2, 2016 6:22 PM
  • Hi,

    well, you actually need to store the content of your textfile somewhere, in order for it to do you any good:

    #Get the content of the file and for each line run the CMDLET to add the machine to the collection
    $path = "path of txt file"
    $CollectionID = "BW10????"
    $content = Get-Content $path
    ForEach($m in $content)
    {
        $PCID = (Get-CMDevice -Name $m).ResourceId
        try
        {
            Add-CMDeviceCollectionDirectMembershipRule -CollectionId $CollectionID -ResourceId $PCID -ErrorAction Stop
            Write-Host "$m was added to the collection" -ForegroundColor Green
        }
        catch { Write-Host "Failed to add $m to the collection! $_" -ForegroundColor Red }
    }

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Proposed as answer by Frank Dong Wednesday, August 3, 2016 5:40 AM
    Tuesday, August 2, 2016 7:17 PM
  • Dear Sir,

    Make sure all the PCs in the txt files are already known (Discovered) by Configuration Manager. If no, then there will be no ResourceID so that the script will throw out error just like you got.

    Best regards

    Frank


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, August 3, 2016 5:42 AM
  • Frank, 

    What kind of check are you talking about after the Get-cmdevice to validate the data 

    Wednesday, August 3, 2016 11:26 AM
  • Thanks for the ideas as a test I removed all the machine names in the TXT file and put in only one machine name that i know is online and working ; same error 

    FWN I tried adding your suggestion and still errors out... 

    Wednesday, August 3, 2016 11:42 AM
  • Hi,

    when you run this command:

    (Get-CMDevice -Name "<Name of online computer>").ResourceId

    Does it return the id?

    If it doesn't, try running this:

    Get-CMDevice -Name "<Name of online computer>" | fl *

    And post a screenshot of the result.

    Cheers,
    Fred


    There's no place like 127.0.0.1


    • Edited by FWN Wednesday, August 3, 2016 12:45 PM Typo
    Wednesday, August 3, 2016 12:03 PM
  • Fred 

    When i run :  

    (Get-CMDevice -Name "<Name of online computer>").ResourceId

    the above command works eventually it adds it to the correct colleciton without error , I have tried another known online machine and worked 

    I removed the online machine and put it in the .txt file and then i got the same error as always ...

    Get-CMDevice -Name $m | fl *

    error: 

    Add-CMDeviceCollectionDirectMembershipRule : Cannot bind argument to parameter 'ResourceId' because it is null.
    At E:\Scripts\Batch add PC.ps1:18 char:89
    + ... ID -ResourceId $PCID
    +                    ~~~~~
        + CategoryInfo          : InvalidData: (:) [Add-CMDeviceCol...tMembershipRule], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.ConfigurationManagement.Cmdlets.Collections.Commands.AddDeviceCollectionDirectMembershipRuleCommand
     
    e:\scripts\amp.txt was added to the collection



    • Edited by Texanaggie Wednesday, August 3, 2016 12:34 PM
    Wednesday, August 3, 2016 12:20 PM
  • Hi,

    actually, I meant for you to run the two commands on the console, without all the other script (and I messed up in the second command, leaving in the variable) ^^

    Hm, this sounds like your text-file is buggy. Let's see, how does this work out for you:

    #Get the content of the file and for each line run the CMDLET to add the machine to the collection
    $path = "path of txt file"
    $CollectionID = "BW10????"
    $content = Get-Content $path
    ForEach($m in $content)
    {
        Write-Host "Processing: $m"
        if ($m.Length -lt 1)
        {
            Write-Host "Empty entry, skipping"
            continue
        }
        $PCID = (Get-CMDevice -Name $m).ResourceId
        Write-Host "Entry: $m | $PCID"
        try
        {
            Add-CMDeviceCollectionDirectMembershipRule -CollectionId $CollectionID -ResourceId $PCID -ErrorAction Stop
            Write-Host "$m was added to the collection" -ForegroundColor Green
        }
        catch { Write-Host "Failed to add $m to the collection! $_" -ForegroundColor Red }
    }

    Now, does this happen for all values, the first, any specific? Is there an empty line in there somewhere?

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by Texanaggie Wednesday, August 3, 2016 5:25 PM
    Wednesday, August 3, 2016 12:49 PM
  •  E:\Scripts\Batch add PC.ps1
    machine name

    Processing: e:\scripts\amp.txt
    Entry: e:\scripts\amp.txt | 
    Failed to add e:\scripts\amp.txt to the collection! Cannot bind argument to parameter 'ResourceId' because it is null.

    this is  what comes up when i plugged  the above change in to the script. the machine name is the only thing in the txt file no spaces, no extra spaces after the machine name , I have verified the machine is in  Config Manager 

    Wednesday, August 3, 2016 1:18 PM
  • Have you downloaded and installed the latest PoSh cmdlets for ConfigMgr?
    What happens if you run
    Get-CMDevice -Name ExistingComputerName
    then? (You have to replace ExistingComputerName with a real device name for sure)

    Torsten Meringer | http://www.mssccmfaq.de

    Wednesday, August 3, 2016 1:36 PM
  • Hi,

    from the logs, it looks like in your text file there is only a path to another file ("e:\scripts\amp.txt").

    You'll need to fix the content of the file.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Wednesday, August 3, 2016 1:52 PM
  • yes i have the latest  cmdlets for ConfigMgr. 

    when i run the get-CMDevice -name existingComputerName 

    it returns the information for that machine name no errors 

    Wednesday, August 3, 2016 1:58 PM
  • However your output proves, that your script does not find a computername in the file, but the name of a file. It's as if you fused part of my fix of your script with the original, broken one.

    Can you post the full content of "Batch add PC.ps1" and a screenshot of the console with its output?


    There's no place like 127.0.0.1

    Wednesday, August 3, 2016 2:20 PM
  • not sure what you are saying about the Text file .. 

    Wednesday, August 3, 2016 2:23 PM
  • The revision that I posted (and which you used) writes more to the console, right?

    It writes the individual computers it tries to process and what the ID is, that was found.

    Currently, it doesn't write a computername, but instead it is trying to process a filename ("e:\scripts\amp.txt"). This means that either ...

    • The content of the file is wrong
      or
    • The script doesn't retrieve the correct information

    If the script called is fully (and solely) the snippet I posted, with the path added in, then either the content in the textfile is broken, or the path you added is wrong.


    There's no place like 127.0.0.1

    Wednesday, August 3, 2016 2:48 PM
  • Wednesday, August 3, 2016 4:02 PM
  • Hi,

    and there we have the issue:

    You did not copy the part above the loop, which is critically important and the original issue:

    You are reading the content of the file and then discard it. Check my last posted snippet and look above the loop - the difference between that and the code you actually use may be small, but it is important.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Wednesday, August 3, 2016 4:58 PM
  • really appreciate your time sir , helps when i copied everything ... works  perfect 

    Wednesday, August 3, 2016 5:25 PM