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': $_"
}
}
}
No comments:
Post a Comment