How to add devices listed in a text file to an existing collection of SCCM

I had this command

Get-Content "D:\TEMP\mycolmembers.txt" | foreach { Add-CMDeviceCollectionDirectMembershipRule -CollectionName "TEST-CERTURAN" -ResourceID (Get-CMDevice -Name $_).ResourceID }

when I ran this command in powershell, I got the error as in attached file.
Could you tell me What is wrong with the command.
PowershellScreen.JPG
certuranAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kanti PrasadCommented:
Hi

try the below
Get-Content "D:\TEMP\mycolmembers.txt"| foreach { Add-CMDeviceCollectionDirectMembershipRule  -CollectionName "TEST-CERTURAN" -ResourceId (get-cmdevice -Name $_).ResourceID }

the code snippet is as below

$computers = Get-Content "D:\TEMP\mycolmembers.txt"
foreach($computer in $computers) {
   Add-CMDeviceCollectionDirectMembershipRule  -CollectionName Collection_01 -ResourceId $(get-cmdevice -Name $computer).ResourceID}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
certuranAuthor Commented:
unfortunately I got the error. please kindly See the attachment.
PowershellScreen-2.JPG
0
Kanti PrasadCommented:
Hi

As it is  missing cmdlet try importing the import ConfiguratioManager module

http://blogs.technet.com/b/configmgrteam/archive/2013/03/27/powershell-connecting-to-configuration-manager.aspx

so run the pipeline one  
pipeline.Commands.Add("Import-Module").AddArgument("C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1");

then run your command
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Kanti PrasadCommented:
$computers = Get-Content "D:\TEMP\mycolmembers.txt"
foreach($computer in $computers) {
   Add-CMDeviceCollectionDirectMembershipRule  -CollectionName Collection_01 -ResourceId $(get-cmdevice -Name $computer).ResourceID}
0
certuranAuthor Commented:
should i do all the things that take place in the page which you sent the link?
0
Kanti PrasadCommented:
Hi

Go to powershell as below
https://henryhazot.wordpress.com/2013/10/31/sccm-2012-sp1-powershell-3-adding-computers-to-collection-from-file/

run the below as I changed your code accordingly

$ErrorActionPreference= 'silentlycontinue'
$CollectionName = "TEST-CERTURAN"
$Computers = get-content D:\TEMP\mycolmembers.txt
Foreach ($Computer in $Computers){ add-cmdevicecollectiondirectmembershiprule -collectionname $CollectionName -resourceid (Get-CMDevice -name $Computer).ResourceID }


if you get cmdlet error then run the below and run the above code again

pipeline.Commands.Add("Import-Module").AddArgument("C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1");
0
certuranAuthor Commented:
as far as I understand, I have to open power shell from the menu of SCCM "Connect via Windows Powershell.
However, I need to PowerSehll3.0 need powershell 3You directing me to install this first and you are giving me a link to install PS 3.0. when I get in the link it says I have to install .net 4.0install .net 40. Where is PS3.0? It says me to install .net. Whereas in my server already have. isn't it already have 4.0I am scared to install something SCCM server. You know, somethings get damaged. Because when I installed dotNetFx40_FullSetup.exe it reuired to be updated latest SP and all patches. So it seems dangereous.
Could you please kindly write me your precious opinion for me. Thank You.
0
Kanti PrasadCommented:
Hi certuran

Can you run the below the same way you ran your first command when you got errors and don't install anything new.

$ErrorActionPreference= 'silentlycontinue'
$CollectionName = "TEST-CERTURAN"
$Computers = get-content D:\TEMP\mycolmembers.txt
Foreach ($Computer in $Computers){ add-cmdevicecollectiondirectmembershiprule -collectionname $CollectionName -resourceid (Get-CMDevice -name $Computer).ResourceID }
0
certuranAuthor Commented:
I ran it. Error free. However collection has not been updated. I ran it as seen in picture.
ranCommand
0
Kanti PrasadCommented:
Hi

Sorry as I don't have the software myself I cannot test the below so can you try both as I am not sure if the path requires double quotes but doing assignment this is correct
$Computers = get-content D:\TEMP\mycolmembers.txt

1st try this

Foreach ($Computer in Get-Content D:\TEMP\mycolmembers.txt){ add-cmdevicecollectiondirectmembershiprule -collectionname "TEST-CERTURAN" -resourceid (Get-CMDevice -name $Computer).ResourceID }

2nd try this
Foreach ($Computer in Get-Content "D:\TEMP\mycolmembers.txt"){ add-cmdevicecollectiondirectmembershiprule -collectionname "TEST-CERTURAN" -resourceid (Get-CMDevice -name $Computer).ResourceID }
0
certuranAuthor Commented:
don't matter. i always try.
both are unseccessful.
0
Kanti PrasadCommented:
Hi

Thanks!

What info you have in mycolmembers.txt  &  TEST-CERTURAN

Give this a last try

1. Can you change the data in  mycolmembers.txt and try again 1st try and 2nd  try again?

Also can you try pasting the below code in a notepad and save it as   yourfilename.ps1 and run the script ?

$CollectionName = "TEST-CERTURAN"
$Computers = get-content D:\TEMP\mycolmembers.txt
Foreach ($Computer in $Computers){ add-cmdevicecollectiondirectmembershiprule -collectionname $CollectionName -resourceid (Get-CMDevice -name $Computer).ResourceID

______________________________________________________
Also don't install anything but just read the info \ content  so that you will get an idea.

https://henryhazot.wordpress.com/2013/10/31/sccm-2012-sp1-powershell-3-adding-computers-to-collection-from-file/
0
certuranAuthor Commented:
What info you have in mycolmembers.txt  &  TEST-CERTURAN
Computer Names those all take place in SCCM device list.

Can you change the data in  mycolmembers.txt and try again 1st try and 2nd  try again?
I did it. but negative

when I ran last one ı got this screen. prompt changed.
run v4
how do i run ps1 file (but don't matter it is easy to run your lines with copy paste)
0
Kanti PrasadCommented:
Hi

Sorry I missed } above so you had that prompt changed  


Save code in a notepad and save a .ps1 then  type as below

PS> .\yourfilename.ps1

Try with one computer first copy the below and run it on PS

add-cmdevicecollectiondirectmembershiprule -collectionname "TEST-CERTURAN" -resourceid (Get-CMDevice -name "put_computer_name").ResourceID

then if it works copy the below code into a notepad and save as .ps1 and run it  .\yourfilename.ps1

$CollectionName = "TEST-CERTURAN"
$Computers = get-content D:\TEMP\mycolmembers.txt
Foreach ($Computer in $Computers){ add-cmdevicecollectiondirectmembershiprule -collectionname $CollectionName -resourceid (Get-CMDevice -name $Computer).ResourceID}
0
Kanti PrasadCommented:
Hi

replace  your 1st name of computer in "put_your_computername" that is in the .txt file and run it directly
PS> add-cmdevicecollectiondirectmembershiprule -collectionname "TEST-CERTURAN" -resourceid (Get-CMDevice -name "put_computer_name").ResourceID

Then put the below code in a   .ps1 and it should work

$CollectionName = "TEST-CERTURAN"
$Computers = get-content D:\TEMP\mycolmembers.txt
Foreach ($Computer in $Computers){ add-cmdevicecollectiondirectmembershiprule -collectionname $CollectionName -resourceid (Get-CMDevice -name $Computer).ResourceID}

but if adding 1 computer does not work then you need to check your read & write access to the relevant folders and the access to SCCM etc... as now there is no issue with the code
0
certuranAuthor Commented:
if I don't give command
$ErrorActionPreference= 'silentlycontinue'
giving error.
ı don't know should I use this, any way I tried both way. this command unsuccessful (put_computer_name replaced real device name)
add-cmdevicecollectiondirectmembershiprule -collectionname "TEST-CERTURAN" -resourceid (Get-CMDevice -name "put_computer_name").ResourceID
0
Kanti PrasadCommented:
Hi

Try without spaces directly as below

add-cmdevicecollectiondirectmembershiprule -collectionname "TEST-CERTURAN" -resourceid (Get-CMDevice -name "real_device_name").ResourceID
0
Kanti PrasadCommented:
Hi

Also try this way I have added additional quotes

$CollectionName = "TEST-CERTURAN"
$DeviceName = "real_device_name"
Add-CMDeviceCollectionDirectMembershipRule -CollectionName "$CollectionName" -ResourceID $(get-cmdevice -name "$DeviceName").ResourceID
0
certuranAuthor Commented:
LAST COMMAND GIVING THIS ERROR

The term 'add-cmdevicecollectiondirectmembershiprule' is not recognized as the name of a cmdlet, function, script file,
 or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and tr
y again.
At line:1 char:43
+ add-cmdevicecollectiondirectmembershiprule <<<<  -collectionname "TEST-CERTURAN" -resourceid (Get-CMDevice -name "ACA
LISKAN").ResourceID
    + CategoryInfo          : ObjectNotFound: (add-cmdevicecol...tmembershiprule:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
0
Kanti PrasadCommented:
Hi

don't put that error line  till this is solved
 
try this witout quotes

Add-CMDeviceCollectionDirectMembershipRule  -CollectionName TEST-CERTURAN -ResourceId $(get-cmdevice -Name real_device_name).ResourceID

try it with quotes
Add-CMDeviceCollectionDirectMembershipRule  -CollectionName "TEST-CERTURAN" -ResourceId $(get-cmdevice -Name "real_device_name").ResourceID
0
Kanti PrasadCommented:
Hi

Ignore all and just try the below

I just changed it to the correct format. Now try this on the command prompt

without quotes
Add-CMDeviceCollectionDirectMembershipRule -CollectionName TEST-CERTURAN -ResourceID $(Get-CMDevice -Name real_device_name).ResourceID

with quotes
Add-CMDeviceCollectionDirectMembershipRule -CollectionName "TEST-CERTURAN" -ResourceID $(Get-CMDevice -Name "real_device_name").ResourceID


Then basing on the above we can change the below accordingly....


$CollectionName = "TEST-CERTURAN"
$Computers = get-content "D:\TEMP\mycolmembers.txt"
foreach ($Computer in $Computers){
Add-CMDeviceCollectionDirectMembershipRule -CollectionName $CollectionName -ResourceID $(Get-CMDevice -Name $Computer).ResourceID
}
0
certuranAuthor Commented:
Add-CMDeviceCollectionDirectMembershipRule -CollectionName TEST-CERTURAN -ResourceID $(Get-CMDevice -Name real_device_name).ResourceID
THIS WITH QUOTES OR NOT GIVING THE ERROR:

The term 'Add-CMDeviceCollectionDirectMembershipRule' is not recognized as the name of a cmdlet, function, script file,
 or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and tr
y again.
At line:1 char:43
+ Add-CMDeviceCollectionDirectMembershipRule <<<<  -CollectionName TEST-CERTURAN -ResourceID $(Get-CMDevice -Name "ACAL
SIKAN").ResourceID
    + CategoryInfo          : ObjectNotFound: (Add-CMDeviceCol...tMembershipRule:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
0
Kanti PrasadCommented:
Hi

Please give me 15 \ 20 min as now the code is correct we just need the correct method names as some are with caps and some are not.
On your powershell do you have any help which gives the correct syntax?

in the mean time try to run the below again

Add-CMDeviceCollectionDirectMembershipRule -CollectionName TEST-CERTURAN -ResourceID $(Get -CMDevice -Name real_device_name).ResourceID
0
certuranAuthor Commented:
it's ok you get time. I am sorry I can't see any help on the powershell.
command is not successful.
The error, isn't it already deal with the command Add-CMDeviceCollectionDirectMembershipRule.
So, do you think the problem is about syntax.
0
Kanti PrasadCommented:
Hi

These are 4 versions I can get looking at various code some are ID some are id some as get some as Get so first try each of them on PS prompt directly and see if anything works



Add-CMDeviceCollectionDirectMembershipRule -CollectionName TEST-CERTURAN -ResourceId (get-CMDevice -name real_device_name).ResourceID



Add-CMDeviceCollectionDirectMembershipRule -CollectionName TEST-CERTURAN -ResourceId $(get-cmdevice -Name real_device_name).ResourceID



Add-CMDeviceCollectionDirectMembershipRule -CollectionName TEST-CERTURAN -ResourceID $(get-cmdevice -name real_device_name).ResourceID



Add-CMDeviceCollectionDirectMembershipRule -CollectionName TEST-CERTURAN -ResourceID $(Get-CMDevice -Name real_device_name).ResourceID
0
Kanti PrasadCommented:
Hi

Then try with quotes. I am sure out of the 8 one will work. We are very close to solving it. We just need to get the method names right...

Sorry again go make you do this as I don't have a way to test it before I give you the solution.

Add-CMDeviceCollectionDirectMembershipRule -CollectionName "TEST-CERTURAN" -ResourceId (get-CMDevice -name "real_device_name").ResourceID



Add-CMDeviceCollectionDirectMembershipRule -CollectionName "TEST-CERTURAN" -ResourceId $(get-cmdevice -Name "real_device_name").ResourceID



Add-CMDeviceCollectionDirectMembershipRule -CollectionName "TEST-CERTURAN" -ResourceID $(get-cmdevice -name "real_device_name").ResourceID



Add-CMDeviceCollectionDirectMembershipRule -CollectionName "TEST-CERTURAN" -ResourceID $(Get-CMDevice -Name "real_device_name").ResourceID
0
certuranAuthor Commented:
previous 4 unseccessful. and also see the attachment SCCM consol
4 together önceki mesajsccm.JPG
0
certuranAuthor Commented:
your last message tries unfortunately are not successful
4together v2The error, isn't it already deal with the command Add-CMDeviceCollectionDirectMembershipRule.
So, do you think the problem is about syntax.
0
Kanti PrasadCommented:
Hi

can you try this

PS> Get-Process -ComputerName
0
certuranAuthor Commented:
here is the result. (IS IT ABOUT ACALSIKAN? real_device_name: ACALISKAN. please see attachment)

Get-Process : Missing an argument for parameter 'ComputerName'. Specify a parameter of type 'System.String[]' and try a
gain.
At line:1 char:26
+ Get-Process -ComputerName <<<<
    + CategoryInfo          : InvalidArgument: (:) [Get-Process], ParameterBindingException
    + FullyQualifiedErrorId : MissingArgument,Microsoft.PowerShell.Commands.GetProcessCommand
ACALISKAN.JPG
0
Kanti PrasadCommented:
Hi

I am not sure if you need to create a collection first and then add it to the collection.
You need to import the below to make Add-CMDeviceCollectionDirectMembershipRule  also work

So try the below or else you have see if someone else can help

Import-Module ‘C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1′

New-CMDeviceCollection -Name "TEST-CERTURAN"

Add-CMDeviceCollectionDirectMembershipRule -CollectionName "TEST-CERTURAN" -ResourceId (get-CMDevice -name "ACALISKAN").ResourceID
0
Kanti PrasadCommented:
Hi

Don't run the above New-CMD.... just try the below

Try Step 2 first then
Search for  ConfigurationManager.psd1 and correct the path below
Run the Step 1 and Step 2 on PS

Step 1
Import-Module 'C:\Program Files\ConfigMgr\AdminConsole\bin\ConfigurationManager.psd1'

Step 2
$CollectionName = "TEST-CERTURAN"
$DeviceName = "ACALISKAN"
Add-CMDeviceCollectionDirectMembershipRule -CollectionName "$CollectionName" -ResourceID $(Get-CMDevice -name "$DeviceName").ResourceID
0
certuranAuthor Commented:
hi again,
I tried step to first. failed.
then serched CM.psd1. found it. tried step1. got this error:
impor module result
0
Kanti PrasadCommented:
Hi

1. Save the below links  script as a AddDeviceToCollection.ps1
2. don't type go to the CM path and then copy the path correctly  
          $CMModule='E:\ConfigMgr\AdminConsole\bin\ConfigurationManager.psd1'
3. remove  $CMSiteCode='PS1'
4. remove $PWDBegin = $PWD
5. change $Devices = get-content "$File"  to caps   as   $Devices = Get-Content "$File"
6. remove $CMSiteCode=$CMSiteCode + ":"
7. remove  Set-Location $CMSiteCode
8. change the resourceid as ResourceID
9. remove $Client
10. remove Set-Location "$PWDBegin"

copy your D:\TEMP\mycolmembers.txt to the same location as the script and run it. The change is you using a collection ID instead of a Collection Name

.\AddDeviceToCollection.ps1 -CollectionID PS100001 -File .\mycolmembers.txt

http://systemcenterme.com/add-devices-from-a-text-file-to-a-sccm-2012-collection/
0
certuranAuthor Commented:
is  AddDeviceToCollection.ps1 file content true? Could you check it. (I'll make its extension ps1 when I am running)
And should I copy this to powerShell and run?
AddDeviceToCollection.txt
0
Kanti PrasadCommented:
Hi

delete the lines that I mentioned remove
Replace the word which I meant change to caps
then if you wish send me the script
0
Kanti PrasadCommented:
Hi

1. Copy the below code  2. change your path 3. save it as AddDeviceToCollection.ps1 4. copy the mycolmembers.txt to the same folder  5. run
.\AddDeviceToCollection.ps1 -CollectionID PS100001 -File .\mycolmembers.txt



Param
(
    [Parameter(Mandatory=$True)]
    [String]$CollectionID,
    [Parameter(Mandatory=$True)]
    [String]$File
)
Process
{
####################################################################
#   Change the path
    $CMModule='E:\ConfigMgr\AdminConsole\bin\ConfigurationManager.psd1'

####################################################################

    $Devices = Get-Content "$File"
    Import-Module $CMModule

    Foreach ($Device in $Devices)
    {
        Add-CMDeviceCollectionDirectMembershipRule -CollectionId $CollectionID -ResourceID (Get-CMDevice -name $Device).ResourceID
           
    }
}
0
certuranAuthor Commented:
This is the psd1 file. coluld you check it.
AddDeviceToCollection.txt
0
certuranAuthor Commented:
The result is:
result
0
Kanti PrasadCommented:
Hi

Where do you have the ConfigurationManager.psd1? as the earlier message showed you ran it from

C:Program Files (86)\ ...... and not the below
C:Program Files\ConfigMgr\AdminConsole\bin\ConfigurationManager.psd1'
0
certuranAuthor Commented:
.\AddDeviceToCollection.ps1 -CollectionID PS100001 -File .\mycolmembers.txt
you give in command ps1
ps1 or psd1
which is correct?
0
certuranAuthor Commented:
I changed to extension both in folder and in command line.
ı didn't get any error.
notepad was opened when I ran it.
result2
0
Kanti PrasadCommented:
Hi

The configuration manager will have an extension as  ConfigurationManager.psd1 but the script you need to save as .ps1

Are both your .\AddDeviceToCollection.ps1  and mycolmembers.txt in D:\temp ?

What is the path of you ConfigurationManager.psd1?

Did you get the expected results? If there are no errors means it might not have ran correctly.
0
certuranAuthor Commented:
Are both your .\AddDeviceToCollection.ps1  and mycolmembers.txt in D:\temp ?
YES

What is the path of you ConfigurationManager.psd1?
C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\
I put this in script

However in command line
.\AddDeviceToCollection.ps1 -CollectionID PS100001 -File .\mycolmembers.txt
CollectionID was wrong I think.
Should I put TEST-CERTURAN collection id (BRI0006E)
I tried wtih this.
result3
0
Kanti PrasadCommented:
Hi

Yes use BRI0006E
0
certuranAuthor Commented:
I used so.
so :) result is above
0
Kanti PrasadCommented:
Hi

I don't know why you are getting that error. The script you have is correct (CollectionID \ CollectionName) you can change it accordingly and run it .\ giving the script , id\name filename.
Maybe see if some other experts can help you out.

http://myitforum.com/myitforumwp/2012/10/22/configmgr-2012-sp1-powershell-module-getting-started/
0
certuranAuthor Commented:
I think that we have to install PS3.
But, as I said, this needed to install somethings.
Nowadays I need SCCM.
may be later I may get the risk. Not now.
Any way,
Thank you very very much. You are great.
Best Wishes
0
Kanti PrasadCommented:
Hi

Thanks again for your patience!! Every bit is much appreciated!!

If possible repost your question with the CollectionName script below or Collection id script above and see if any other experts can help you!

For the below you can run it as after changing the cm.psd1 path

.\AddDeviceToCollection.ps1 -CollectionName TEST-CERTURAN -File .\mycolmembers.txt


Param
(
    [Parameter(Mandatory=$True)]
    [String]$CollectionName,
    [Parameter(Mandatory=$True)]
    [String]$File
)
Process
{
#--------------------
#   Change the path
    $CMModule='E:\ConfigMgr\AdminConsole\bin\ConfigurationManager.psd1'

#----

    $Devices = Get-Content "$File"
    Import-Module $CMModule

    Foreach ($Device in $Devices)
    {
        Add-CMDeviceCollectionDirectMembershipRule -CollectionName $CollectionName -ResourceID (Get-CMDevice -name $Device).ResourceID
           
    }
}
0
certuranAuthor Commented:
essentially I thank for your patience.

I did it and got plain error this time
result4
0
Kanti PrasadCommented:
Hi

Great!

I think you have overcome the code and import issues. Now you need to check and match the CollectionName or the CollectionID to what is in your system and then the code will work.....
0
certuranAuthor Commented:
this is another method that I used
select * from SMS_R_System where SMS_R_System.Name in ("client1","client2")
in Excel, I listed computers
copy paste as transpose
saved in csv format
I got the list client1,client2
I replace , with ","
I got this
"client1","client2"
embed in formula
bingo
:)))))
0
Kanti PrasadCommented:
Hi

Great to hear you got what you were working for....

Meet you again soon here...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Active Directory

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.