Announcement

Collapse
No announcement yet.

Add user to a collection (SCCM) powershell code

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Add user to a collection (SCCM) powershell code

    Hi,

    I have the following powershell code that creats a collection and add a machine to that collection using NetBIOS.

    I would like to have a powershell code that can take a given collection name (not new) and add a given machine name to that collection


    This code will create "FooCollection1" and add "CMP-CLT64" to it
    Code:
    $collClass = [WMIClass] "root\SMS\Site_CMP:SMS_Collection"
    $collInstance = $collClass.CreateInstance() 
    $collInstance.Name = "FooCollection1"
    $collInstance.OwnedByThisSite = $true 
    $collPath = $collInstance.Put() 
    $collection = [WMI] $collPath.Path 
    $subcollClass = [WMIClass] "root\SMS\Site_CMP:SMS_CollectToSubCollect"
    $subcollInstance = $subcollClass.CreateInstance() 
    $subcollInstance.parentCollectionID = "COLLROOT"
    $subcollInstance.subCollectionID = $collection.CollectionID 
    $subcollection = $subcollInstance.Put() 
    $directruleClass = [WMIClass] "root\SMS\Site_CMP:SMS_CollectionRuleDirect"
    $directruleInstance = $directruleClass.CreateInstance() 
    $directruleInstance.ResourceClassName = "SMS_R_System"
    $computer = Get-WmiObject -class "SMS_R_System" -namespace "root\SMS\Site_CMP" -Filter "NetbiosName = "CMP-CLT64""
    $directruleInstance.ResourceID = $computer.ResourceId 
    $collection.AddMembershipRule($directruleInstance) 
    $collection.RequestRefresh()

    Thanks!
Working...
X