Handy to Have – PowerShell Script to Add Collection Variables

We leverage Collection Variables a lot in SCCM 2012 for our OS deployments.  Using them to store things like the domain join account’s or the “run as” account’s passwords makes it simple to maintain and saves us the trouble of having to slog through the entire task sequence and possibly missing one when the time comes to change the passwords.

But this creates its own hassles.  Namely making sure that the variables are not only set but set correctly on any collections when setting up an OS deployment.  What’s the saying?  If you need to do something twice then automate it?

So I did…

This PowerShell script takes the name of a collection as a parameter and sets the required collection variables and their values.

$strCollectionName = $args[0]
$strSiteCode = $args[1]

$SCCMmoduleLoaded = $FALSE
$LoadedModules = Get-Module

FOREACH ($Module in $LoadedModules)
{
IF ($Module.Name -eq "ConfigurationManager") {$SCCMmoduleLoaded = $TRUE}
}
IF ($SCCMmoduleLoaded -eq $FALSE)
{
Import-Module 'C:\Program Files\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1'
}

Set-Location $strSiteCode`:\

New-CMDeviceCollectionVariable -CollectionName "$strCollectionName" -VariableName "OSDJoinPassword" -IsMask $False -Value "P@ssw0rd"

New-CMDeviceCollectionVariable -CollectionName "$strCollectionName" -VariableName "SMSTSRunCommandLineUserPassword" -IsMask $False -Value "P@ssw0rd"

New-CMDeviceCollectionVariable -CollectionName "$strCollectionName" -VariableName "SMSConnectNetworkFolderPassword" -IsMask $False -Value "P@ssw0rd"

New-CMDeviceCollectionVariable -CollectionName "$strCollectionName" -VariableName "SMSTSLocalDataDrive " -IsMask $False -Value "C:"

New-CMDeviceCollectionVariable -CollectionName "$strCollectionName" -VariableName "OSDTimeVer " -IsMask $False -Value "3.0.0"

Set-Location C:\

The script requires 2 parameters.  First the name of the collection and second the site code you want to configure.  [In our environment we have a production site and a development site so I wrote the script to work in either one.]  The first lines of the script just set those parameters to variables.

$strCollectionName = $args[0]
$strSiteCode = $args[1]

The next block of code is used to load the SCCM PowerShell module if it hasn’t already been loaded.

$SCCMmoduleLoaded = $FALSE
$LoadedModules = Get-Module

FOREACH ($Module in $LoadedModules)
{
IF ($Module.Name -eq "ConfigurationManager") {$SCCMmoduleLoaded = $TRUE}
}
IF ($SCCMmoduleLoaded -eq $FALSE)
{
Import-Module 'C:\Program Files\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1'
}

Next we change to the PowerShell drive for out intended SCCM site:

Set-Location $strSiteCode`:\

Lastly we just use a series of New-CMDeviceCollectionVariable cmdlets to set the variables that we want.

New-CMDeviceCollectionVariable -CollectionName "$strCollectionName" -VariableName "OSDJoinPassword" -IsMask $False -Value "P@ssw0rd"

New-CMDeviceCollectionVariable -CollectionName "$strCollectionName" -VariableName "SMSTSRunCommandLineUserPassword" -IsMask $False -Value "P@ssw0rd"

New-CMDeviceCollectionVariable -CollectionName "$strCollectionName" -VariableName "SMSConnectNetworkFolderPassword" -IsMask $False -Value "P@ssw0rd"

New-CMDeviceCollectionVariable -CollectionName "$strCollectionName" -VariableName "SMSTSLocalDataDrive " -IsMask $False -Value "C:"

New-CMDeviceCollectionVariable -CollectionName "$strCollectionName" -VariableName "OSDTimeVer " -IsMask $False -Value "3.0.0"

The command is pretty self-explanitory, but here is how the command breaks down.

-CollectionName “$strCollectionName”
This is just passing the name of the collection, specified as a parameter passed to the script and set as the variable “$strCollectionName”

-VariableName “OSDJoinPassword”
The name of the variable that we want to create.  In this case we’re setting the OSDJoinPassword variable.

-IsMask $False
This will allow the variable’s value to be visible when the collection’s properties are viewed in the console.

-Value “P@ssw0rd”
Simply the value that we want to assign to our variable.

Lastly we change back to our filesystem drive.

Set-Location C:\

This is a very simple script, more of a hammer than a power tool.  There are of course numerous ways to do the exact same thing with a twist (that’s what makes scripting so fun, there are endless ways of getting from Point “A” to Point “B”).  For example, I’m sure you could set all of your variables and values in arrays and then just cycle through and feed them to a function that creates them.

Advertisements

Posted on January 2, 2015, in Configuration Manager, Handy to Have, PowerShell, SCCM and tagged , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: