Script - Create Collection - v 1.0.0.0

A Script that you can use to create a single SCCM Device Collection or create multiple Device Collections based on a file with Collection names in it. The script will also allow you to set the refresh schedule for the collections.



<#
.SYNOPSIS
Creates a SCCM Device Collection

.DESCRIPTION
Creates a SCCM Device Collection from imput or a list of names in a text file and allows you to set the refresh type

.PARAMETER CollectionName
Name of the collection you want to create 

.PARAMETER CollectionsFile
Use this parameter to specify a list of collection names to create, must be full file path

.PARAMETER LimitingCollection
The Collection you want to limmit the collection members from

.PARAMETER LoadLocal
When used will load the Powershell Module from the local system rather then the server

.PARAMETER SiteServer
Set the SCCM Site Server

.PARAMETER SiteCode
Sets the SCCM Site Code

.PARAMETER ScheduleType
Weeks - Sets the Collection to update every x weeks on a spesific days of the week
Days - Sets the Collection to update every x days
Hours - Sets the Collection to update every x Hours
Minutes - Sets teh Collection to update every x Minutes
Continuous - sets the Collection to update Incrementally

.EXAMPLE
Create-Collection.ps1 -SiteServer -Collctionname "Testing Collection" SRV-SCCM -SiteCode MSN -LimmitingCollection "All Desktops" -ScheduleType "Hours"
Creates a Collection Named Testing Collection with alimmiting Collection of All Desktops that will refresh hourly. 

.NOTES
Created By: Kris Gross
Email: KrisGross@jackofalltech.org
Twitter: @kmgamd
Version: 1.0.0.0

.LINK
http://sccmtst.com

#>

Param(  
        $CollectionName,
        $CollectionsFile,
        [Parameter(Mandatory=$True)]
        $LimitingCollection,
        [switch]$LoadLocal,
        [Parameter(Mandatory=$True)]
        $SiteServer,
        [Parameter(Mandatory=$True)]
        $SiteCode,
        [Parameter(Mandatory=$True)]
        [ValidateSet('Weeks','Days','Hours','Minutes','Continuous')]
        [string]$ScheduleType 
        )
#checks to see if CollectionName and CollectionsFile are both being used 
if (($CollectionName) -and ($CollectionsFile)) {Write-Error "Cannot use both CollectionName and CollectionsFile paramiters"}
#checks is the script should load the local PowerShell Module
if ($LoadLocal -eq "$True") 
{
    Set-Location 'C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\'
    Import-Module .\ConfigurationManager.psd1 -verbose:$false   
}
else 
{
    Import-Module \\$SiteServer\SMS_$SiteCode\AdminConsole\bin\ConfigurationManager.psd1 -verbose:$false
}
#Sets the loacation to the SCCM Site so the script can run
$site = $SiteCode + ":"
Set-Location $site
#Sets the Schedule based on the ScheduleType parameter
If ($Scheduletype -eq "Hours") 
{   
    $Hours = Read-Host "How many hourse between refreshe"
    $Schedule = New-CMSchedule -RecurInterval Hours -RecurCount $Hours
}

If ($Scheduletype -eq "Days") 
{   
    $Days = Read-Host "How many days between refreshe"
    $Schedule = New-CMSchedule -RecurInterval Days -RecurCount $Days
}

If ($Scheduletype -eq "Weeks") 
{   
    $DayOfWeek = Read-Host "Day of the week for reshresh"
    $WeeksBetween = Read-Host "Weeks between refresh"
    $Schedule = New-CMSchedule -Start "01/01/2014 9:00 PM" -DayOfWeek $DayOfWeek -RecurCount $WeeksBetween
}

If ($Scheduletype -eq "Minutes") 
{   
    $Minutes = Read-Host "How many minutes between refreshe"
    $Schedule = New-CMSchedule -RecurInterval Minutes -RecurCount $Minutes
}
#If ether the CollectionName or CollectionsFile parameter are used then will run
if ((!($CollectionName)) -or (!($CollectionsFile)))
{
        If ($CollectionsFile)
        {
            $CollectionsFromFile = Get-Content "$CollectionsFile"
            If ($ScheduleType -eq "Continuous")
            {
                Foreach ($Collection in ($CollectionsFromFile))
                {
                    New-CmDeviceCollection -Name "$Collection" -LimitingCollectionName "$LimitingCollection" -RefreshType Continuous
                }
            }
            else 
            {
                Foreach ($Collection in ($CollectionsFromFile))
                {
                    New-CmDeviceCollection -Name "$Collection" -LimitingCollectionName "$LimitingCollection" -RefreshSchedule $Schedule
                }
            }
        }

        IF (!($CollectionsFile))
        {
            If ($ScheduleType -eq "Continuous") 
            {
                New-CmDeviceCollection -Name "$CollectionName" -LimitingCollectionName "$LimitingCollection" -RefreshType Continuous
            }       
            else 
            {
                New-CmDeviceCollection -Name "$CollectionName" -LimitingCollectionName "$LimitingCollection" -RefreshSchedule $Schedule
            }
        }
}
#Changes the location back to where the script was ran from
Set-Location $PSScriptRoot

Comments

Popular posts from this blog

Office 365 Deployment - Office 365 Upgrade Task Sequence

SCCM Task Sequence with GUI - How to install more then one image with a single Task Sequence

Windows 10 Setup Script - V 2.0.0.0