[CmdletBinding()] param( [string]$SiteCode = 'LCI', [string]$CollectionId = 'LCI00014', [string]$CollectionName = 'Centro de Experiencia Digital', [string]$OuCanonicalName = 'LCI.LASALLE.MX/LABORATORIO/CENTRO DE EXPERIENCIA DIGITAL', [string]$ConsoleModule = 'C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1' ) $ErrorActionPreference = 'Stop' if ($OuCanonicalName -match '["\r\n]') { throw 'Invalid OU canonical name.' } Import-Module $ConsoleModule Push-Location ($SiteCode+':\') try { $collection = Get-CMDeviceCollection -CollectionId $CollectionId if (-not $collection) { throw 'Expected managed collection is missing.' } $rules = @(Get-CMDeviceCollectionQueryMembershipRule -CollectionId $CollectionId) if ($rules.Count -gt 1) { throw 'Review unexpected additional membership rules before changing this collection.' } $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 SMS_R_System.SystemOUName = "'+$OuCanonicalName+'"' if (-not $rules.Count -or $rules[0].QueryExpression -cne $query) { if ($rules.Count) { Remove-CMDeviceCollectionQueryMembershipRule -CollectionId $CollectionId -RuleName $rules[0].RuleName -Force } Add-CMDeviceCollectionQueryMembershipRule -CollectionId $CollectionId -RuleName 'Centro de Experiencia Digital OU' -QueryExpression $query } if ($collection.Name -cne $CollectionName) { Set-CMDeviceCollection -CollectionId $CollectionId -NewName $CollectionName -RefreshType Both } else { Set-CMDeviceCollection -CollectionId $CollectionId -RefreshType Both } Invoke-CMSystemDiscovery -SiteCode $SiteCode Invoke-CMCollectionUpdate -CollectionId $CollectionId [pscustomobject]@{CollectionId=$CollectionId;Name=$CollectionName;OU=$OuCanonicalName;Evaluation='Periodic and incremental'} } finally { Pop-Location }