Jump to content


ZeZe

Powershell - adding device

Recommended Posts

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!

 

Share this post


Link to post
Share on other sites


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
  • Like 1

Share this post


Link to post
Share on other sites

 

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!!

Share this post


Link to post
Share on other sites

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 

Share this post


Link to post
Share on other sites

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...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...