1

I'm attempting to create a query using PowerShell to count the number of Lenovo ThinkPad devices with a specific IP range. Code derived from https://computergarage.org/how-to-create-sccm-device-collections-via-powershell.html

# Set Site Code
$SiteCode = Get-PSDrive -PSProvider CMSITE
Set-Location “$($SiteCode.Name):”
# Set Details
$DeviceCollectionName = "ThinkPads"
$FolderPath = “.\DeviceCollection\ThinkPads”
$Query = 'select *  from  SMS_R_System inner join SMS_G_System_NETWORK_ADAPTER_CONFIGURATION on SMS_G_System_NETWORK_ADAPTER_CONFIGURATION.ResourceId = SMS_R_System.ResourceId inner join SMS_G_System_COMPUTER_SYSTEM_PRODUCT on SMS_G_System_COMPUTER_SYSTEM_PRODUCT.ResourceId = SMS_R_System.ResourceId where SMS_G_System_NETWORK_ADAPTER_CONFIGURATION.IPAddress like 
"10.1.%" and SMS_G_System_COMPUTER_SYSTEM_PRODUCT.Version like "%ThinkPad%"'
# Push Query
$NewCollection = Add-CMDeviceCollectionQueryMembershipRule -CollectionName $DeviceCollectionName -QueryExpression $Query -RuleName $DeviceCollectionName
Move-CMObject -FolderPath $FolderPath -InputObject $NewCollection




# Error
Add-CMDeviceCollectionQueryMembershipRule : No object corresponds to the specified parameters. At line:23 char:5
+     Add-CMDeviceCollectionQueryMembershipRule -CollectionName $Device ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (Microsoft.Confi...yMembershipRule:AddDeviceCollectionQueryMembershipRule) [Add-CMDeviceCol...yMembershipRule], ItemNotFoundException
+ FullyQualifiedErrorId : ItemNotFound,Microsoft.ConfigurationManagement.Cmdlets.Collections.Commands.AddDeviceCollectionQueryMembershipRule
 

Can someone explain what the error refers to? If I run the Add-CMDeviceCollectionQueryMembershipRule by itself, it requests all required info, then fails.

  • Output and observe the `$Query` string. Mutually swap the single and double quotes while defining it (see [about_Quoting_Rules](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules)). – JosefZ Jan 08 '22 at 09:53

1 Answers1

0

Figured it out. The command above requires you to have a Device Collection with the same name, and it will not auto create one for you. Added device collection with proper name and set, fixed everything.