Skip to content

Powershell - adding device

Featured Replies

Hi all,

 

I've created a simple powershell just to add a new device into ConfigMgr, plus add this same computer to more collections.

However, when I run the script there's a strange behavior.

 

Script:

 

$MAC = "00:29:15:80:4E:4A"

$PCName = "TESTPC"
Import-CMComputerInformation -CollectionName "All Systems" -ComputerName $PCName -MacAddress $MAC
Add-CMDeviceCollectionDirectMembershipRule -CollectionId 00100120 -ResourceId $(get-cmdevice -Name $PCName).ResourceID
(the last part of the script contains more collections, but for the purpose of the problem this is enough).
If I run this script as it is, I've the following error:
Add-CMDeviceCollectionDirectMembershipRule : Cannot validate argument on parameter 'ResourceId'. The argument is null or empty. Supply an
argument that is not null or empty and then try the command again.

 

If I wait a couple of seconds and I run the script again, with the same variables, the script runs without any issues. If I add "Start-Sleep -Seconds 30" before the adding the computer to the collection, the script also runs without issues. I'm assuming that I need to give time to ConfigMgr to refresh the data? Question: Is it possible to force this (refresh)? Does anyone had this before?

 

Thank you in advanced!

 

  • 2 weeks later...

You're right, you need to allow for the database to refresh. The following worked for me in a test.

$MAC = "00:29:15:80:4E:4A"
$PCName = "TESTPC"
Import-CMComputerInformation -CollectionName "All Systems" -ComputerName $PCName -MacAddress $MAC
while (!$ResourceID)
{
	$ResourceID = $(get-cmdevice -Name $PCName).ResourceID
	Start-Sleep -Seconds 5
}
Add-CMDeviceCollectionDirectMembershipRule -CollectionId 00100120 -ResourceId $ResourceID
  • Author

 

You're right, you need to allow for the database to refresh. The following worked for me in a test.

$MAC = "00:29:15:80:4E:4A"
$PCName = "TESTPC"
Import-CMComputerInformation -CollectionName "All Systems" -ComputerName $PCName -MacAddress $MAC
while (!$ResourceID)
{
	$ResourceID = $(get-cmdevice -Name $PCName).ResourceID
	Start-Sleep -Seconds 5
}
Add-CMDeviceCollectionDirectMembershipRule -CollectionId 00100120 -ResourceId $ResourceID

Thanks, I'm going to try this one!!

  • 4 months later...

OK, just want to tell that it is not necessary to wait this long time. If you try to get Resource ID by WMI you get it immediately:

$ComputerName = "testPC"
$SCCMServer = "sccmServer"
$SiteCode = "DE0"
$Collection = "Windows 10 x64"

$Res = Get-WmiObject -Namespace "Root\SMS\Site_$SiteCode" -ComputerName "$SCCMServer" -Class SMS_R_SYSTEM -Filter "Name = '$ComputerName'"
Add-CMDeviceCollectionDirectMembershipRule  -CollectionName "$Collection" -ResourceId $Res.ResourceID 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.