Tuesday, December 10, 2024

Adding devices to SCCM collection using query method

 Overview

 The "Add Machines to Collection" tool is a simple, user-friendly UI-based solution designed to quickly add machines to an SCCM collection. By specifying a collection ID and providing a text file with machine names, this tool automatically updates the collection and displays the results on the screen.

 Features

                 - Collection ID Input: Specify the target SCCM collection ID.

- Text File Selection: Upload a text file containing the list of machine names or IDs.

- Run Button: Start the process to add the listed machines to the specified collection.

- Output Display: View the result of the operation directly on the tool's interface.

 Instructions

1. Launch the Tool: Open the executable or script for the "Add Machines to Collection" tool.

 2. Enter Collection ID:

   - In the provided field, input the desired SCCM collection ID where machines will be added.

3. Select Text File:

   - Click on the "Browse" button to select a text file containing the list of machine names.

   - Ensure the file contains one machine name per line.

 4. Click Run:

   - Hit the "Run" button to execute the process.

 5. View Results:

   - Once the operation completes, the tool will display the output below the interface. This output includes a summary of the machines added and any potential errors encountered during the process.

 Requirements

- A valid SCCM environment.

- Collection ID must exist in SCCM.

- A properly formatted text file with machine names (one per line).

 

Troubleshooting

 - If an error occurs, verify the following:

                  1. The collection ID is correct and exists in SCCM.

  2. The text file is formatted correctly with valid machine names.



Here is the download link from Github

Friday, December 6, 2024

Add-MachinesToSCCMCollection

The function takes the SCCM collection ID, a path to a text file containing the list of machines, and a batch size (defaulting to 700 machines per collection). It divides the machines into batches and adds them to the collection using query membership rules.


Usage:

Save this script to a .ps1 file.

Call the Add-MachinesToSCCMCollection function, providing the necessary parameters like CollectionID and TextFilePath.

Optionally, modify the BatchSize parameter if you want a different number of machines per query.



function Add-MachinesToSCCMCollection {

    param (

        [string]$CollectionID,                

        [string]$TextFilePath,                

        [int]$BatchSize = 700                

    )

    if (-not (Test-Path -Path $TextFilePath)) {

        Write-Error "File not found: $TextFilePath"

        return

    }

    $machineNames = Get-Content -Path $TextFilePath

    if ($machineNames.Count -eq 0) {

        Write-Error "The file is empty or contains invalid data: $TextFilePath"

        return

    }

    $batches = @()

    for ($i = 0; $i -lt $machineNames.Count; $i += $BatchSize) {

        $batches += ,@($machineNames[$i..[Math]::Min($i + $BatchSize - 1, $machineNames.Count - 1)])

    }

    foreach ($batch in $batches) {

        $query = @"

select SMS_R_SYSTEM.ResourceID,

       SMS_R_SYSTEM.ResourceType,

       SMS_R_SYSTEM.Name,

       SMS_R_SYSTEM.SMSUniqueIdentifier,

       SMS_R_SYSTEM.ResourceDomainORWorkgroup,

       SMS_R_SYSTEM.Client

from SMS_R_System

where Name in ('$($batch -join "','")')

"@

        $ruleName = "query-$($batches.IndexOf($batch) + 1)"

        try {

            Add-CMDeviceCollectionQueryMembershipRule -CollectionId $CollectionID -RuleName $ruleName -QueryExpression $query

            Write-Host "Successfully added rule '$ruleName' to collection '$CollectionID'."

        } catch {

            Write-Error "Failed to add rule '$ruleName': $_"

        }

    }

}


Adding devices to SCCM collection using query method

  Overview   The "Add Machines to Collection" tool is a simple, user-friendly UI-based solution designed to quickly add machines...