Archive

Archive for February, 2012

Modifying SCCM collections from the command line

February 8th, 2012 No comments

Automating SCCM OSD collection removal and addition.

I  created a little command line app that allows modification of SCCM collections from the command line  SCCMd – it requires various licensed MS components, but these should all be present on your SCCM server.

Usage: ScCmd.exe [/h:<hostname>] [/u:<username>] [/p:<password>] [/i] [/d] [/a:<CollectionName>] [/r:<CollectionName>] [/rd] [/c:<SccmclientName>
] [/m:<MacAddress>] [/f] [/q:<MacAddress or ClientName>]

/h SCCM HostName <required>
/u UserName <Defaults to current user>
/p Password <Only required when specifying user>
/i Import client to SCCM <MacAddress and HostName required> (use -f if Overwrite required)
/d Delete client from SCCM <ClientName or macAddress required>
/a Add Client to Collection <ClientName and CollectionName required>
/r Remove direct rule client from a Collection <Clientname and CollectionName required>
/rd Remove all direct Collection rules <clientName required>
/f Force Overwrite of existing records when importing new clients
/c Sccm Client Name
/m MacAddress
/q Query SCCM for client record – returns client details

I use this to manage the removal from collection (and addition to new collections) by monitoring events on my build jobs.

So by settings up an Eventlog monitor to execute a bacth file like the following:

@ECHO OFF
if “%1″==”” goto :NOARGS

REM Check if server is in one of my deployment collections
c:\Windows\sccmcmd.exe /h:mysccmserver /q:%1 > %temp%\%1.txt
findstr /C:”Windows Server 2008 R2″  %temp%\%1.txt >nul&&(GOTO POSTBUILD)||(GOTO 2K3)

:2K3
findstr /C:”Windows Server 2003 R2 x64″  %temp%\%1.txt >nul&&(GOTO POSTBUILD)||(GOTO FLUSH)

:FLUSH
REM Remove from all collections
c:\Windows\SCCMCmd.exe /h:mysccmserver /rd /c:%1
GOTO END

:POSTBUILD
REM Schedule PostBuild Job by adding the server to the Postbuild collection
c:\Windows\SCCMCmd.exe /h:mysccmserver /rd /c:%1
c:\Windows\SCCMCmd.exe /h:mysccmserver /a:”R2 PostConfig – LIVE – R2.CMD” /c:%1
GOTO END

:NOARGS
ECHO “No Arguments”
GOTO END

:END

The Server gets removed from all collections after a successful build, then added to the ‘PostBuild’ collection – which of course launches all of my PostBuild scripts.

This means I am able to manage which collections my server is in at the end of my build process.

Categories: SCCM / SMS Tags: