dd Computers To a Collection in SCCM throw wmi in powershell script from remote machine RRS feed

  • Question

  • Hi guys

    I  use the below command to add machine in collection from remote machine.

    Set-WmiInstance -Namespace "Root\Sms\Site_APF" -Class 'SMS_CollectionRuleDirect' -ComputerName "APFLABSCCM01" -Credential $cred -Arguments @{
     ResourceClassName = 'SMS_R_System' ; ResourceID = 16777253 ; RuleName = 'DESKTOP-2RTC761' }

    and receive  below error

    Set-WmiInstance : Attempt to put an instance with no defined key.
    At line:1 char:1
    + Set-WmiInstance -Namespace "Root\Sms\Site_APF" -Class 'SMS_Collection ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [Set-WmiInstance], ManagementException
        + FullyQualifiedErrorId : SetWMIManagementException,Microsoft.PowerShell.Commands.SetWmiInstance

    can you suggest where i am doing wrong i will be a great help.

    Wednesday, October 9, 2019 10:50 AM

All replies

  • Hi Lokesh,

    Keith Garner has written an WMI function to add machines to a collection in SCCM.

    Function Add-ResourceToCollection {
            [string]   $SiteCode ,
            [string]   $SiteServer,
            [string]   $CollectionName,
            [parameter(Mandatory=$true, ValueFromPipeline=$true)]
        begin {
            $WmiArgs = @{ NameSpace = "Root\SMS\Site_$SiteCode"; ComputerName = $SiteServer }
            $CollectionQuery = Get-WmiObject @WMIArgs -Class SMS_Collection -Filter "Name = '$CollectionName' and CollectionType='2'"
            $InParams = $CollectionQuery.PSBase.GetMethodParameters('AddMembershipRules')
            $Cls = [WMIClass]"Root\SMS\Site_$($SiteCode):SMS_CollectionRuleDirect"
            $Rules = @()
        process {
            foreach ( $sys in $System ) {
                $NewRule = $cls.CreateInstance()
                $NewRule.ResourceClassName = "SMS_R_System"
                $NewRule.ResourceID = $sys.ResourceID
                $NewRule.Rulename = $sys.Name
                $Rules += $NewRule.psobject.BaseObject 
        end {
            $InParams.CollectionRules += $Rules.psobject.BaseOBject
            $CollectionQuery.PSBase.InvokeMethod('AddMembershipRules',$InParams,$null) | Out-null
            $CollectionQuery.RequestRefresh() | out-null


    Please try to load the function before use the command below:

    1. Use "get-cmdevice" cmdlet to get device:

    get-cmdevice -name test* -fast | Add-ResourceToCollection -SiteCode "Site_APF" -SiteServer "APFLABSCCM01" -CollectionName "YourColloectionName" 

    2. Try to save the device infor in the csv file,then import it.

    csv file example:

    Import-Csv -Path c:\test.csv | Add-ResourceToCollection -SiteCode "Site_APF" -SiteServer "APFLABSCCM01" -CollectionName "YourColloectionName" 

    Best regards,


    Just do it.

    Thursday, October 10, 2019 3:21 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, November 8, 2019 2:10 PM