ZeZe Posted August 8, 2016 Report post Posted August 8, 2016 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! Quote Share this post Link to post Share on other sites More sharing options...
tregelen Posted August 22, 2016 Report post Posted August 22, 2016 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 1 Quote Share this post Link to post Share on other sites More sharing options...
ZeZe Posted August 29, 2016 Report post Posted August 29, 2016 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!! Quote Share this post Link to post Share on other sites More sharing options...
tempus Posted January 12, 2017 Report post Posted January 12, 2017 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 Quote Share this post Link to post Share on other sites More sharing options...