Donnie Taylor at myITforum.com

January 2009 - Posts

VBScript for Computer Association with logging

Here is a quick and dirty vbscript that will generate a computer associate between 2 resourceids, logging information to your systemdrive\ConfigMgrComputerAssociation.log. 

To use the script, call it with three arguments - script.vbs <sccmservername> <source-computer-resouceid> <destination-computer-resourceid>

 

Enjoy!

 

'******************Code Below Here****************

set colargs = wscript.arguments
strSMSServer = colargs(0)
strSourcePC = colargs(1)
strDestinationPC = colargs(2)

'Set Environment Variables
Set oShell = CreateObject( "WScript.Shell" )
systemdrive=oShell.ExpandEnvironmentStrings("%systemdrive%")
computername = oshell.ExpandEnvironmentStrings("%computername%")

'Create Output Log
stroutputlog = systemdrive & "\ConfigMgrComputerAssociation.log"
if objfso.fileexists(stroutputlog) then
   objfso.deletefile(stroutputlog)
end if
Set objFileoutput = objFSO.CreateTextFile(stroutputlog)
log "Output log created."

'log the arguments
log "Argument 1: " & colargs(0)
log "Argument 2: " & colargs(1)
log "Argument 3: " & colargs(2)

'Setup Connection
Set objLoc = CreateObject("WbemScripting.SWbemLocator")
Set objSMS = objLoc.ConnectServer(strSMSServer, "root\sms")
Set Results = objSMS.ExecQuery ( "SELECT * From SMS_ProviderLocation WHERE ProviderForLocalSite = true" )
For Each Loc In Results
     If Loc.ProviderForLocalSite = True Then
           Set objSMS = objLoc.ConnectServer(Loc.Machine, "root\sms\site_" & Loc.SiteCode)
           log "Connection established to " & strsmsserver & "."
     End If
Next

'Create the Computer association
associatecomputer objsms, strSourcePC, strDestinationPC

log "Exiting Main Script Body"
WScript.Quit

Sub AssociateComputer(connection, referenceComputerResourceId, destinationComputerResourceId)
      log "Starting AssociateComputer Subroutine"
      Dim stateMigrationClass
      Dim inParams
      Dim outParams
      log "Connection: " & connection & " SourceID: " & referenceComputerResourceId & " DestinationID : " & destinationComputerResourceId
      ' Get the state migration class.
      Set stateMigrationClass = connection.Get("SMS_StateMigration")
      ' Set up the parameters.
      Set inParams = stateMigrationClass.Methods_("AddAssociation").InParameters.SpawnInstance_
      inParams.SourceClientResourceID = referenceComputerResourceId
      inParams.RestoreClientResourceID = destinationComputerResourceId
      ' Call the method.
      Set outParams = connection.ExecMethod( "SMS_StateMigration", "AddAssociation", inParams)
      log "Exiting Associate Computer Subroutine"
End Sub

SUB Log( message )
    '
    ' Log the given message
    '
    objfileoutput.writeline(message)
END SUB

'******************Code Above Here****************

Transcript from DCCU chat on DellTechCenter (1/6/2009)

http://www.delltechcenter.com/page/01-06-09+DCCU+3.0+%E2%80%93+Dell+Client+Configuration+Utility+3.0

Dennis led the chat - and a good one it was!  Everything you wanted to know about the Client Configuration Utility.

List of default Task Sequence Variables

Here is a handy link to the Technet library page that contains all of the default Task Sequence Variables.  Bookmark this one to keep it handy!

http://technet.microsoft.com/en-us/library/bb632442.aspx

It has recently gone through an update, so most of the variables are no longer on the same page - but the links are on this page to take you to the specific sections.

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*********

 

Let me know if you have any questions or comments.  Enjoy!

Posted: Jan 05 2009, 09:57 AM by dtaylor | with no comments
Filed under: ,