Share This Post

Script Skeleton for ConfigMgr with Logging

If you are like me, you want a consistent look to your scripts, verbose yet readable output, and a quick connection to your SCCM server or client. Here is a script framework that I use when developing new code. Simply change the SCCMServer variable, change the output log file name, and put your code after the comment ‘Start the bulk of your code here”.

Now, whenever you want to write something to the output log, simply preface it with the keyword Log. For example:

Log “This is a sample line in the log file. I can append variable names, such as the SCCMServer variable (” & SCCMSERVER & “) anywhere I want.”

********CODE BELOW HERE***********

‘Creates an environment for the script to work
Set wshshell = WScript.CreateObject(“WScript.Shell”)

‘Set File System Object
set objFSO = CreateObject(“Scripting.FileSystemObject”)

‘Set Environment Variables
Set oShell = CreateObject( “WScript.Shell” )
computername = oshell.ExpandEnvironmentStrings(“%computername%”)

‘Create Output Log
stroutputlog = systemdrive & “\DefaultScript.log”
if objfso.fileexists(stroutputlog) then
end if
Set objFileoutput = objFSO.CreateTextFile(stroutputlog)
log “Output log created.”

‘Set the SCCM Server

‘ Setup a connection to the provider.
Set swbemLocator = CreateObject(“WbemScripting.SWbemLocator”)
Set swbemServices= swbemLocator.ConnectServer(SCCMServer, “root\sms”)
Set providerLoc = swbemServices.InstancesOf(“SMS_ProviderLocation”)

For Each Location In providerLoc
If location.ProviderForLocalSite = True Then
Set swbemServices = swbemLocator.ConnectServer(Location.Machine, “root\sms\site_” + Location.SiteCode)
siteCode = Location.SiteCode
log sitecode
Exit For
End If

‘Start the bulk of your code here
Log “Bulk code execution begins now.”

‘All Done with the script – time to exit
Log “Exiting Script”

SUB Log( message )

‘ Log the given message


*********CODE ABOVE HERE*********

Share This Post

Leave a Reply