Thursday, January 14, 2016

Powershell to create Collections in Configmgr





Remember to Update your Powershell Module first



-----------

# Variables
$PWDBegin = $PWD
$CMSiteCode = "XXX" # Change this - This should be your site code
$CMModule = "D:\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1" # Change this - This should point to the CM module

Import-Module $CMModule
$CMSiteCode = $CMSiteCode + ":"
Set-Location $CMSiteCode
$schedule = New-CMSchedule -RecurInterval Days -RecurCount 7

New-CMDeviceCollection -Name "Windows 8" -LimitingCollectionId "SMS00001" -RefreshSchedule $schedule
Add-CMDeviceCollectionQueryMembershipRule -CollectionName "Windows 8" -QueryExpression "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.OperatingSystemNameandVersion like 'Microsoft Windows NT Workstation 6.2%'" -RuleName "Windows 8"
New-CMDeviceCollection -Name "Windows 8.1" -LimitingCollectionId "SMS00001" -RefreshSchedule $schedule
Add-CMDeviceCollectionQueryMembershipRule -CollectionName "Windows 8.1" -QueryExpression "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.OperatingSystemNameandVersion like '%Microsoft Windows NT Workstation 6.3%'" -RuleName "Windows 8.1"
New-CMDeviceCollection -Name "Windows 10" -LimitingCollectionId "SMS00001" -RefreshSchedule $schedule
Add-CMDeviceCollectionQueryMembershipRule -CollectionName "Windows 10" -QueryExpression "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.OperatingSystemNameandVersion like '%Microsoft Windows NT Workstation 10%'" -RuleName "Windows 10"
New-CMDeviceCollection -Name "Windows 7" -LimitingCollectionId "SMS00001" -RefreshSchedule $schedule
Add-CMDeviceCollectionQueryMembershipRule -CollectionName "Windows 7" -QueryExpression "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.OperatingSystemNameandVersion like '%Microsoft Windows NT Workstation 6.1%'" -RuleName "Windows 7"
New-CMDeviceCollection -Name "Windows Vista" -LimitingCollectionId "SMS00001" -RefreshSchedule $schedule
Add-CMDeviceCollectionQueryMembershipRule -CollectionName "Windows Vista" -QueryExpression "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.OperatingSystemNameandVersion like '%Microsoft Windows NT Workstation 6.0%'" -RuleName "Windows Vista"
New-CMDeviceCollection -Name "Windows XP" -LimitingCollectionId "SMS00001" -RefreshSchedule $schedule
Add-CMDeviceCollectionQueryMembershipRule -CollectionName "Windows XP" -QueryExpression "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.OperatingSystemNameandVersion like '%Microsoft Windows NT Workstation 5.1%'" -RuleName "Windows XP"

New-CMDeviceCollection -Name "Windows Server 2012" -LimitingCollectionId "SMS00001" -RefreshSchedule $schedule
Add-CMDeviceCollectionQueryMembershipRule -CollectionName "Windows Server 2012" -QueryExpression "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.OperatingSystemNameandVersion like '%Microsoft Windows NT Server 6.2%'" -RuleName "Windows Server 2012"
New-CMDeviceCollection -Name "Windows Server 2012 R2" -LimitingCollectionId "SMS00001" -RefreshSchedule $schedule
Add-CMDeviceCollectionQueryMembershipRule -CollectionName "Windows Server 2012 R2" -QueryExpression "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.OperatingSystemNameandVersion like '%Microsoft Windows NT Server 6.3%'" -RuleName "Windows Server 2012 R2"
New-CMDeviceCollection -Name "Windows Server 2016" -LimitingCollectionId "SMS00001" -RefreshSchedule $schedule
Add-CMDeviceCollectionQueryMembershipRule -CollectionName "Windows Server 2016" -QueryExpression "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.OperatingSystemNameandVersion like '%Microsoft Windows NT Server 10%'" -RuleName "Windows Server 2016"
New-CMDeviceCollection -Name "Windows Server 2008 R2" -LimitingCollectionId "SMS00001" -RefreshSchedule $schedule
Add-CMDeviceCollectionQueryMembershipRule -CollectionName "Windows Server 2008 R2" -QueryExpression "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.OperatingSystemNameandVersion like '%Microsoft Windows NT Server 6.1%'" -RuleName "Windows Server 2008 R2"
New-CMDeviceCollection -Name "Windows Server 2008" -LimitingCollectionId "SMS00001" -RefreshSchedule $schedule
Add-CMDeviceCollectionQueryMembershipRule -CollectionName "Windows Server 2008" -QueryExpression "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.OperatingSystemNameandVersion like '%Microsoft Windows NT Server 6.0%'" -RuleName "Windows Server 2008"
New-CMDeviceCollection -Name "Windows Server 2003" -LimitingCollectionId "SMS00001" -RefreshSchedule $schedule
Add-CMDeviceCollectionQueryMembershipRule -CollectionName "Windows Server 2003" -QueryExpression "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.OperatingSystemNameandVersion like '%Microsoft Windows NT Server 5.2%'" -RuleName "Windows Server 2003"

New-CMDeviceCollection -Name "All Windows Servers" -LimitingCollectionId "SMS00001" -RefreshSchedule $schedule
Add-CMDeviceCollectionQueryMembershipRule -CollectionName "All Windows Servers" -QueryExpression "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.OperatingSystemNameandVersion like '%Microsoft Windows NT Server%'" -RuleName "All Windows Servers"
New-CMDeviceCollection -Name "All Windows Workstations" -LimitingCollectionId "SMS00001" -RefreshSchedule $schedule
Add-CMDeviceCollectionQueryMembershipRule -CollectionName "All Windows Workstations" -QueryExpression "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.OperatingSystemNameandVersion like '%Microsoft Windows NT Workstation%'" -RuleName "All Windows Workstations"

Set-Location $PWDBegin

----------

Onedrive link to Powershell Script:
https://1drv.ms/u/s!AggfKwD7Gzp_grFSbG2DlaEXVxB4Sw


Use at your own risk.


Nice if you let me know if this is usefull for you in comments below.

Thanks

No comments:

Post a Comment