1

I need to get all the users being part of a collecionID on a SCCM server

Variables

$CollectionID = COLLID046
$SiteCode = XYZ
$SiteServer = SITEXYZ0
$Users = Get-WmiObject -Class SMS_FullCollectionMembership -Namespace root/SMS/site_$($SiteCode) -ComputerName $SiteServer | Where-Object -FilterScript {$_.CollectionId -eq $CollectionID} | Select-Object SMSID

This work fine and return all the users, but has the side effect that if you have a lot of collectionID the where clause will circle all the collection before find the one you are looking and could take long time.

There is way to point directly to a specific collection?

Sterling Archer
  • 22,070
  • 18
  • 81
  • 118

2 Answers2

0

I prefer this method:

Get-CMCollectionMember -CollectionId <Collection ID> | Select-Object -ExpandProperty Name

Here's the link to all the info on the Get-CMCollectionMember Cmdlet.

SolidSid
  • 319
  • 1
  • 10
  • Yep, you'll need to make sure you're connect to your CM drive with the ConfigurationManager module loaded. The easiest way to do this, is to open Config Manager, then select 'Connect with PowerShell ISE: https://learn.microsoft.com/en-us/powershell/module/configurationmanager/?view=sccm-ps – SolidSid Mar 21 '19 at 10:49
  • Your suggestion needs to have imported the module ConfigurationManager.psd1. But also once imported if I issue the PS:> Get-CMCollectionMember -CollectionId 'COLLID' | Select-Object -ExpandProperty SMSID I have this error. "Get-CMCollectionMember : This command cannot be run from the current drive. To run this command you must first connect to a Configuration Manager drive." Seems to be not applicable in my case because I need to run the script via WMI over the network. – Giuseppe Barillari Mar 21 '19 at 10:59
  • You need to be connected to the PS Drive for your Config Manager. Using the 'Connect via Windows PowerShell ISE' option gets you a script which loads the module and sets the PS Drive. – SolidSid Mar 21 '19 at 11:05
  • Take a look at this: https://learn.microsoft.com/en-us/sccm/develop/core/understand/connecting-to-configuration-manager-with-windows-powershell – SolidSid Mar 21 '19 at 11:17
  • Yes! on the server itself works fine. Now I need to move the script to another server where the Management console is not installed. In order to import the module ConfigurationManager.psd1 I need to install the Management console? Or there is any other way? – Giuseppe Barillari Mar 21 '19 at 11:47
  • Great! No need to install the Config Manager console. You can run the 'connection' script at the start of your script to first load the module and connect to the PS Drive, then run your script to accomplish what you need to do. Let me know if you have issues with this and I'll post what the full script should look like. – SolidSid Mar 21 '19 at 12:01
  • When I run the script on the server is ok. If I run the script on another server I have got the following error ----------------------------------------------------------------------------------Import-Module "$($ENV:SMS_ADMIN_UI_PATH)\..\ConfigurationManager. ... + CategoryInfo : ResourceUnavailable: (\..\ConfigurationManager.psd1:String) [Import-Module], FileNotFoundException + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand ---------------------------------------------------------------------------------Any idea? Thanks – Giuseppe Barillari Mar 21 '19 at 13:08
  • We asked MS about this several years ago and the answer was that the only officially supported way to install the sccm cmdlets is as part of the console installation. Do you have different information? – Syberdoor Mar 21 '19 at 13:20
  • Apologies, I misunderstood your situation. Yes you will need the console to run the cmdlets. If you don't want to install the console, you can use PSRemoting to a server which does have the console installed to run the CM cmdlets. – SolidSid Mar 21 '19 at 14:06
0

There should be a class for each collection that is called SMS_CM_RES_COLL_[collid]. I'm not sure if it has all the same information that SMS_FullCollectionMembership has but just to get who is a member it should be sufficient.

Syberdoor
  • 2,521
  • 1
  • 11
  • 14