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” )
systemdrive=oShell.ExpandEnvironmentStrings(“%systemdrive%”)
computername = oshell.ExpandEnvironmentStrings(“%computername%”)

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

‘Set the SCCM Server
SCCMServer = “<YOURSERVERNAME>”

‘ 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
Next

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

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

SUB Log( message )

‘ Log the given message

objfileoutput.writeline(message)
END SUB

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

Share This Post

Leave a Reply