WordPress.com



#################################################

## Created By: Luke Salter ##

## Created On: 07/05/2014 ##

## SCCM Collection Creation Based on CSV Input ##

#################################################

## Load Configuration Manager Module and Switch to EH Site Server

CD "C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin"

import-module .\ConfigurationManager.psd1

CD EH1:

function CreateInstallCollection {

## Creates Collection based on the Name read in the CSV File

New-CMUserCollection -name $InstallCollectionName -LimitingCollectionName "All USERS" -RefreshType ConstantUpdate

## Creates a Query based rule for the Collection just created. Query looks at AD for name read in the CSV File

Add-CMUserCollectionQueryMembershipRule -RuleName $InstallCollectionName -Collectionname $InstallCollectionName -QueryExpression "Select SMS_R_USER.ResourceID,SMS_R_USER.ResourceType,SMS_R_USER.Name,SMS_R_USER.UniqueUserName,SMS_R_USER.WindowsNTDomain from SMS_R_User where SMS_R_User.UserGroupName = 'EH\\SCCM_DL_$InstallCollectionName'"

## Connects to the site WMI namespace and reads the CollectionID for the collection created.

$collection = gwmi -Namespace root\sms\site_EH1 -Class SMS_Collection | where {$_.Name -eq "$InstallCollectionName"}

$collID = $collection.CollectionID

## Creates Variables for moving the collection

[Array]$DeviceCollectionID = $collid

$TargetFolderID = 16777234 ## Target Folder = Applications\Live

$CurrentFolderID = 0 ## Current Directory = Root

$ObjectTypeID = 5001 ## Object Type = User (5000 for Computer)

## Moves the collection to the applications sub folder

Invoke-WmiMethod -Namespace "Root\SMS\Site_EH1" -Class SMS_objectContainerItem -Name MoveMembers -ArgumentList $CurrentFolderID,$DeviceCollectionID,$ObjectTypeID,$TargetFolderID

$InstallCollectionName | Out-File C:\Temp\Powershell\'Collection Creation'\CreatedCollections.txt -append

$DeviceCollectionID | Out-File C:\Temp\Powershell\'Collection Creation'\CreatedCollections.txt -append

}

function CreateUninstallCollection {

## Creates Collection based on the Name read in the CSV File

New-CMUserCollection -name $UninstallCollectionName -LimitingCollectionName "All USERS" -RefreshType ConstantUpdate

## Creates a Query based rule for the Collection just created. Query looks at AD for name read in the CSV File

Add-CMUserCollectionQueryMembershipRule -RuleName $UninstallCollectionName -Collectionname $UninstallCollectionName -QueryExpression "Select SMS_R_USER.ResourceID,SMS_R_USER.ResourceType,SMS_R_USER.Name,SMS_R_USER.UniqueUserName,SMS_R_USER.WindowsNTDomain from SMS_R_User where SMS_R_User.UserGroupName = 'EH\\SCCM_DL_$UninstallCollectionName'"

## Connects to the site WMI namespace and reads the CollectionID for the collection created.

$collection = gwmi -Namespace root\sms\site_EH1 -Class SMS_Collection | where {$_.Name -eq "$UninstallCollectionName"}

$collID = $collection.CollectionID

## Creates Variables for moving the collection

[Array]$DeviceCollectionID = $collid

$TargetFolderID = 16777235 ## Target Folder = Applications\Live

$CurrentFolderID = 0 ## Current Directory = Root

$ObjectTypeID = 5001 ## Object Type = User (5000 for Computer)

## Moves the collection to the applications sub folder

Invoke-WmiMethod -Namespace "Root\SMS\Site_EH1" -Class SMS_objectContainerItem -Name MoveMembers -ArgumentList $CurrentFolderID,$DeviceCollectionID,$ObjectTypeID,$TargetFolderID

$UninstallCollectionname | Out-File C:\Temp\Powershell\'Collection Creation'\CreatedCollections.txt -append

$DeviceCollectionID | Out-File C:\Temp\Powershell\'Collection Creation'\CreatedCollections.txt -append

}

## Read Applications CSV File

import-csv 'C:\Temp\Powershell\Collection Creation\Applications1.csv' | foreach {

## Collection Name Variable for each row in the CSV File

$InstallCollectionName=$_.APPNAME+'_Install'

$UninstallCollectionName=$_.APPNAME+'_Uninstall'

## Check Existing Collection

IF(!(Get-WmiObject -Namespace "root\SMS\Site_EH1" -Query "Select * from SMS_Collection where name = '$InstallCollectionName'" | Select-Object Name))

{

CreateInstallCollection

CreateUnInstallCollection

}

ELSE

{$InstallCollectionname | Out-File C:\Temp\Powershell\'Collection Creation'\ExistingCollections.txt -append}

}

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download