Link to home
Start Free TrialLog in
Avatar of Phil Mapfumo
Phil MapfumoFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Adding multiple machines in a text file to an SCCM collection via PowerShell

 I am trying to add multiple machines from a text file to an SCCM collection via PowerShell. I have created the collection however when l run this command via Config Manager PowerShell :


Get-Content "C:\Users\PhilM\Desktop\ChromeList.txt" | foreach { Add-CMDeviceCollectionDirectMembershipRule -CollectionName "Chrome Installed Desktops" -ResourceID (Get-CMDevice -Name $_).ResourceID }


I get this error :

Add-CMDeviceCollectionDirectMembershipRule : Cannot bind argument to parameter 'ResourceId' because it is null.

At line:1 char:168

+ ...  Installed Desktops" -ResourceID (Get-CMDevice -Name $_).ResourceID }

+                                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidData: (:) [Add-CMDeviceCol...tMembershipRule], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.ConfigurationManagement.PowerShell.Cmdlets.Collections.AddDeviceCollectionDirectM 

   embershipRule


I would appreciate any assistance thanks

ASKER CERTIFIED SOLUTION
Avatar of FOX
FOX
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of oBdA
oBdA

That means that at least one of the names was not found, so no ResourceID was passed.
Get-Content "C:\Users\PhilM\Desktop\ChromeList.txt" | ForEach-Object {
	Write-Host "Processing '$($_)'"
	$cmDevice = Get-CMDevice -Name $_ -Fast
	If ($cmDevice) {
		Add-CMDeviceCollectionDirectMembershipRule -CollectionName "Chrome Installed Desktops" -ResourceID $cmDevice.ResourceID
	} Else {
		Write-Warning "Found no CM device '$($_)'!"
	}
}

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Phil Mapfumo

ASKER

Many thanks for the assistance and apologies for the delay. What worked for me was to remove the nulls or the empty space in the in the txt file, which l wasn't aware there was.So thanks to Mike T and l also tried using the CSV as method which worked as well so many thanks for that to FOX