<#

.SYNOPSIS

Powershell script to buildout SCCM Device collections

.DESCRIPTION

Grabs OU from AD, and generates a collection group for the corp, and adds collections with the desktops\laptops computers based on teach,stu,or admin of the OU being a membership rule

.PARAMETER debug

turns on visible debugging and debug logging, warning all ye who enter here, your eyes shall bleed

.EXAMPLE

& .\corpDeviceCollectionBuildout.ps1

.OUTPUTS

c:\windows\Temp\logs\corpDeviceCollectionBuildout$date.log logfile outputting changes

.NOTES

Name: sanitizedDeviceCollectionBuildout.ps1

Author: Smartguy5000

Last Modified: 03/24/2016 14:07

#>

<#

.CHANGELOG

v1.0 - initial write

.VERSION

1.0

#>

#Parent IDs of Device Collection Folders

<#

Name objectType ParentContainerNodeID ContainerNodeID

---- ---------- --------------------- ---------------

TEST Computers 5000 0 781

TEST corps Computers 5000 781 782 corps OU

TEST Administrative Office Computers 5000 781 783 Employees OU

#>

[ CmdletBinding ( SupportsShouldProcess = $True ) ]

Param (

)

Function New - CollectionFolder

{

Param (

[ parameter ( Mandatory = $true ) ] $folderName ,

[ parameter ( Mandatory = $true ) ] $ParentCollectionNodeID

)

$CollectionFolderArgs = @ {

Name = $FolderName ;

ObjectType = "5000" ; # 5000 means Collection_Device, 5001 means Collection_User

ParentContainerNodeid = "$ParentCollectionNodeID" # 0 is root.

}

set - ciminstance -Class "SMS_ObjectContainerNode" - arguments $CollectionFolderArgs -namespace "root\SMS\Site_$sitecode" -ComputerName $siteserver - PutType CreateOnly | Out-Null

}

Function Move - CMObject

{

[ CmdLetBinding ( ) ]

Param (

[ Parameter ( Mandatory = $True , HelpMessage = "Please Enter Site Server Site code" ) ]

$SiteCode ,

[ Parameter ( Mandatory = $True , HelpMessage = "Please Enter Site Server Name" ) ]

$SiteServer ,

[ Parameter ( Mandatory = $True , HelpMessage = "Please Enter Object ID" ) ]

[ ARRAY ] $ObjectID ,

[ Parameter ( Mandatory = $True , HelpMessage = "Please Enter current folder ID" ) ]

[ uint32 ] $CurrentFolderID ,

[ Parameter ( Mandatory = $True , HelpMessage = "Please Enter target folder ID" ) ]

[ uint32 ] $TargetFolderID ,

[ Parameter ( Mandatory = $True , HelpMessage = "Please Enter object type ID" ) ]

[ uint32 ] $ObjectTypeID

)

Try {

invoke - ciminstance -Namespace "Root\SMS\Site_$SiteCode" -Class SMS_objectContainerItem -Name MoveMembers -ArgumentList $CurrentFolderID , $ObjectID , $ObjectTypeID , $TargetFolderID -ComputerName $SiteServer -ErrorAction STOP

}

Catch {

$_ .Exception.Message

}

}

$origVerbPref = $VerbosePreference

Start-Transcript 'C:\Windows\Temp\computerCollectionBuildout.log'

import - module 'C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1'

$OriginalLocaiton = Get-Location

$VerbosePreference = 'Continue'

$sitecode = "CEN"

Set-Location "${SiteCode}:"

$siteserver = "CM_CAS.$env:userdnsdomain"

$objectType = '5000' #DeviceCollectionFolder

###########

$ParentNodeID = '782' #TEST corps Computers Folder

$SearchBase = 'OU=Computers,OU=Organization,DC=corp,DC=contoso,DC=com'

###########

$OUArray = Get - ADOrganizationalUnit - Filter * - SearchBase $SearchBase - SearchScope OneLevel

[ int ] $Hour = '00'

[ int ] $Min = '00'

$i = 0

ForEach ( $OU in $OUArray )

{

$OUName = $OU .Name

$FolderName = 'TEST ' + $OUName + ' Computers'

Write-Verbose "$OUName"

$SearchBase = $OU .DistinguishedName

$SubOUArray = Get - ADOrganizationalUnit - Filter * - SearchBase $SearchBase - SearchScope OneLevel - Properties Name , DistinguishedName , CanonicalName | Select-Object Name , DistinguishedName , CanonicalName

Write-Verbose "Adding $FolderName Folder"

New - CollectionFolder - folderName $FolderName $ParentNodeID

start-sleep - s '7'

Write-Verbose 'Awake'

ForEach ( $SubOU in $SubOUArray )

{

$SubOUName = $SubOU .Name

$DepartmentNumber = $SubOUName .SubString ( $SubOUName .Length - 4 )

$SubFolderName = 'TEST ' + $SubOUName + ' Computers'

$ParentFolderWMI = get - ciminstance -namespace "ROOT\SMS\Site_$siteCode" -computername $siteserver -Query "SELECT * FROM SMS_ObjectContainerNode WHERE ObjectType=$objectType And Name like " "%${FolderName}%" ""

Write-Verbose "Adding $Subfoldername Subfolder"

$SubParentNodeID = $ParentFolderWMI .ContainerNodeID

New - CollectionFolder $SubFolderName $SubParentNodeID

start-sleep - s '7'

#add error checking here to see if new folder exists

[ string ] $Hour = $Hour .ToString ( )

[ string ] $Min = $Min .ToString ( )

If ( $Hour .Length -eq 1 )

{

$Hour = '0' + $Hour

}

If ( $Min .Length -eq 1 )

{

$Min = '0' + $Min

}

$CollectionName = 'TEST ' + $SubOUName + ' All Computers'

$SubFolderWMI = get - ciminstance -namespace "ROOT\SMS\Site_$siteCode" -computername $siteserver -Query "SELECT * FROM SMS_ObjectContainerNode WHERE ObjectType=$objectType And Name like " "%${SubFolderName}%" ""

$SubSubParentNodeID = $SubFolderWMI .ContainerNodeID

$Schedule = New - CMSchedule -Start "2016-04-01 ${Hour}:${Min}" - RecurInterval Days - RecurCount 1

$NewcorpComputerCollection = New - CMDeviceCollection -Name $CollectionName - RefreshType 'Periodic' - RefreshSchedule $Schedule - LimitingCollectionName "TEST Workstations" -Confirm : $False

start-sleep - S '5'

Move - CMObject - SiteCode $sitecode - SiteServer $siteserver - ObjectID $NewcorpComputerCollection .CollectionID - CurrentFolderID '0' - TargetFolderID $SubSubParentNodeID - ObjectTypeID $objectType | Out-Null

start-sleep - s '5'

Add - CMDeviceCollectionQueryMembershipRule - CollectionName $CollectionName - RuleName "$($SubOU.Name) OU" - QueryExpression "select * from SMS_R_System where SMS_R_System.SystemOUName = '$($SubOU.CanonicalName)'" | Out-Null

Write-Verbose "Added Collection"

start-sleep - s '3'

[ int ] $Hour = $Hour

[ int ] $Min = $Min

$Min = $Min + 5

If ( ( $Min % 12 ) -eq '0' )

{

$Hour = $Hour + 1

$Min = '0'

}

If ( [ int ] $Hour -gt '23' )

{

$hour = '0'

}

[ int ] $i = 0

While ( $i -le '5' )

{

Switch ( $i )

{

0 {

$OUSuffix = 'Laptop Computers'

$SubCollectionName = 'TEST ' + $SubOUName + ' ' + $OUSuffix

$Schedule2 = New - CMSchedule -Start "2016-04-01 ${Hour}:${Min}" - RecurInterval Days - RecurCount 1

Write-Verbose "Adding Collection"

$NewComputerCollection = New - CMDeviceCollection -Name $SubCollectionName - RefreshType 'Periodic' - RefreshSchedule $Schedule2 - LimitingCollectionName $NewcorpComputerCollection .Name -Confirm : $False

Write-Verbose "Added Collection"

start-sleep - s '5'

Move - CMObject - SiteCode $sitecode - SiteServer $siteserver - ObjectID $NewComputerCollection .CollectionID - CurrentFolderID '0' - TargetFolderID $SubSubParentNodeID - ObjectTypeID $objectType | Out-Null

start-sleep - s '3'

Add - CMDeviceCollectionQueryMembershipRule - CollectionName $SubCollectionName - RuleName "$($SubOU.Name) Laptop Chassis Type" - QueryExpression "select * from SMS_R_System inner join SMS_G_System_SYSTEM_ENCLOSURE on SMS_G_System_SYSTEM_ENCLOSURE.ResourceID = SMS_R_System.ResourceId where SMS_G_System_SYSTEM_ENCLOSURE.ChassisTypes in ( " "8" ", " "9" ", " "10" ", " "14" " )" | Out-Null

}

1 {

$OUSuffix = 'Desktop Computers'

$SubCollectionName = 'TEST ' + $SubOUName + ' ' + $OUSuffix

$Schedule2 = New - CMSchedule -Start "2016-04-01 ${Hour}:${Min}" - RecurInterval Days - RecurCount 1

Write-Verbose "Adding Collection"

$NewComputerCollection = New - CMDeviceCollection -Name $SubCollectionName - RefreshType 'Periodic' - RefreshSchedule $Schedule2 - LimitingCollectionName $NewcorpComputerCollection .Name -Confirm : $False

Write-Verbose "Added Collection"

start-sleep - s '5'

Move - CMObject - SiteCode $sitecode - SiteServer $siteserver - ObjectID $NewComputerCollection .CollectionID - CurrentFolderID '0' - TargetFolderID $SubSubParentNodeID - ObjectTypeID $objectType | Out-Null

start-sleep - s '3'

Add - CMDeviceCollectionQueryMembershipRule - CollectionName $SubCollectionName - RuleName "$($SubSubOU.Name) Desktop Chassis Type" - QueryExpression "select * from SMS_R_System inner join SMS_G_System_SYSTEM_ENCLOSURE on SMS_G_System_SYSTEM_ENCLOSURE.ResourceID = SMS_R_System.ResourceId where SMS_G_System_SYSTEM_ENCLOSURE.ChassisTypes not in ( " "8" ", " "9" ", " "10" ", " "14" " )" | Out-Null

}

2 {

$OUSuffix = 'Teacher Computers'

$SubCollectionName = 'TEST ' + $SubOUName + ' ' + $OUSuffix

$Schedule2 = New - CMSchedule -Start "2016-04-01 ${Hour}:${Min}" - RecurInterval Days - RecurCount 1

Write-Verbose "Adding Collection"

$NewComputerCollection = New - CMDeviceCollection -Name $SubCollectionName - RefreshType 'Periodic' - RefreshSchedule $Schedule2 - LimitingCollectionName $NewcorpComputerCollection .Name -Confirm : $False

Write-Verbose "Added Collection"

start-sleep - s '5'

Move - CMObject - SiteCode $sitecode - SiteServer $siteserver - ObjectID $NewComputerCollection .CollectionID - CurrentFolderID '0' - TargetFolderID $SubSubParentNodeID - ObjectTypeID $objectType | Out-Null

start-sleep - s '3'

Add - CMDeviceCollectionQueryMembershipRule - CollectionName $SubCollectionName - RuleName "$($SubOU.Name) Teacher Computers" - QueryExpression "select * from SMS_R_System where SMS_R_System.SystemOUName like " "$($SubOU.CanonicalName)/${DepartmentNumber}Computers/%/${DepartmentNumber}-Teachers" "" | Out-Null

}

3 {

$OUSuffix = 'Student Computers'

$SubCollectionName = 'TEST ' + $SubOUName + ' ' + $OUSuffix

$Schedule2 = New - CMSchedule -Start "2016-04-01 ${Hour}:${Min}" - RecurInterval Days - RecurCount 1

Write-Verbose "Adding Collection"

$NewComputerCollection = New - CMDeviceCollection -Name $SubCollectionName - RefreshType 'Periodic' - RefreshSchedule $Schedule2 - LimitingCollectionName $NewcorpComputerCollection .Name -Confirm : $False

Write-Verbose "Added Collection"

start-sleep - s '5'

Move - CMObject - SiteCode $sitecode - SiteServer $siteserver - ObjectID $NewComputerCollection .CollectionID - CurrentFolderID '0' - TargetFolderID $SubSubParentNodeID - ObjectTypeID $objectType | Out-Null

start-sleep - s '3'

Add - CMDeviceCollectionQueryMembershipRule - CollectionName $SubCollectionName - RuleName "$($SubOU.Name) Student Computers" - QueryExpression "select * from SMS_R_System where SMS_R_System.SystemOUName like " "$($SubOU.CanonicalName)/${DepartmentNumber}Computers/%/${DepartmentNumber}-Students" "" | Out-Null

}

4 {

$OUSuffix = 'Administration Computers'

$SubCollectionName = 'TEST ' + $SubOUName + ' ' + $OUSuffix

$Schedule2 = New - CMSchedule -Start "2016-04-01 ${Hour}:${Min}" - RecurInterval Days - RecurCount 1

Write-Verbose "Adding Collection"

$NewComputerCollection = New - CMDeviceCollection -Name $SubCollectionName - RefreshType 'Periodic' - RefreshSchedule $Schedule2 - LimitingCollectionName $NewcorpComputerCollection .Name -Confirm : $False

Write-Verbose "Added Collection"

start-sleep - s '5'

Move - CMObject - SiteCode $sitecode - SiteServer $siteserver - ObjectID $NewComputerCollection .CollectionID - CurrentFolderID '0' - TargetFolderID $SubSubParentNodeID - ObjectTypeID $objectType | Out-Null

start-sleep - s '3'

Add - CMDeviceCollectionQueryMembershipRule - CollectionName $SubCollectionName - RuleName "$($SubOU.Name) Administration Computers" - QueryExpression "select * from SMS_R_System where SMS_R_System.SystemOUName like " "$($SubOU.CanonicalName)/${DepartmentNumber}Computers/%/${DepartmentNumber}-Administration" "" | Out-Null

}

}

$Min = $Min + 5

If ( ( $Min % 12 ) -eq '0' )

{

$Hour = $Hour + 1

$Min = '0'

}

If ( [ int ] $Hour -gt '23' )

{

$hour = '0'

}

$i = $i + 1

}

}

}

$VerbosePreference = $origVerbPref

Set-Location = $OriginalLocaiton