Donnie Taylor at myITforum.com

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

Comments

No Comments