Jump to content


  • 0
thadkew

Modify script to add computers to collection

Question

I found an HTA that will create a computer association within SCCM and it works great.

 

What I'd like now is to figure out a way to add these PCs to the collections (ie. ComputerA is associated to ComputerB and I'd like ComputerA to be placed in the USMT Capture group and ComputerB to be added to the Restore group).

 

Is it possible?

 

Attached is the hta that I used...

 

<html>

<head>

<title>User State Migration Tool</title>

<HTA:APPLICATION

ID="objAutoRefresh"

APPLICATIONNAME="User State Migration Tool"

SCROLL="auto"

SINGLEINSTANCE="yes"

>

</head>

 

<body STYLE="font:14pt arial; color:black;

filter:progid:DXImageTransform.Microsoft.Gradient

(GradientType=0, StartColorStr='#800000', EndColorStr='#DB7093')">

 

<center><b>Using this interface we prepare to take a source computer and a destination computer and copy the

OS and Application files from the source to the destination</b></center>

<p></p><b>The source computer is the user's old computer<br> and the destination computer would be the user's new computer</b>

<p></body>

 

<script LANGUAGE="VBScript">

 

'Set Variables: Site Server, Username and Password

Server = "server"

userName = "flerm\SCCM"

userPassword = "blerm"

 

Sub TestSub

 

Dim computera, computerb, ResIDA, ResIDB

strcomputera = document.getElementById("computera").value

strcomputerb = document.getElementById("computerb").value

 

Set connection = Connect(Server, userName, userPassword)

Set connection1 = Connect(Server, userName, userPassword)

ResIDA = GetResourceIDFromName (connection, strcomputera)

ResIDB = GetResourceIDFromNameB (Connection, strcomputerb)

 

AssociateComputer Connection, ResIDA, ResIDB', ResIDA, ResIDB

 

End Sub

 

Sub AssociateComputer(connection, referenceComputerResourceId, destinationComputerResourceId)

Dim stateMigrationClass

Dim inParams

Dim outParams

'On Error Resume Next

' 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

'inParams.MigrationType = "1"

'MsgBox inParams.SourceClientResourceID

'msgbox inParams.RestoreClientResourceID

' Call the method.

Set outParams = connection.ExecMethod( "SMS_StateMigration","AddAssociation",inParams)

'connection.ExecMethod stateMigrationClass.AddAssociation,inParams

' MsgBox Err

if Err.number <> 0 Then

MsgBox "Failed to create a Computer Association, please try again or contact the GOC"

Exit Sub

Else

General_OnClick()

End If

End Sub

' Utility function to search for the site code

 

Function Connect(server, userName, userPassword)

'On Error Resume Next

 

Dim net

Dim localConnection

Dim swbemLocator

Dim swbemServices

Dim providerLoc

Dim location

Set swbemLocator = CreateObject("WbemScripting.SWbemLocator")'

 

swbemLocator.Security_.AuthenticationLevel = 6 'Packet Privacy.

' If the server is local, don't supply credentials.

Set net = CreateObject("WScript.NetWork")

If UCase(net.ComputerName) = UCase(server) Then

localConnection = True

userName = ""

userPassword = ""

server = "."

End If

' Connect to the server.

Set swbemServices= swbemLocator.ConnectServer _

(server, "root\sms",userName,userPassword)

If Err.Number<>0 Then

' Wscript.Echo "Couldn't connect: " + Err.Description

Connect = Null

Exit Function

End If

 

' Determine where the provider is and connect.

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,userName,userPassword)

If Err.Number<>0 Then

' Wscript.Echo "Couldn't connect:" + Err.Description

Connect = Null

Exit Function

Else

'Wscript.Echo "Yay, we connected!!"

End If

Set Connect = swbemServices

Exit Function

End If

Next

Set Connect = null ' Failed to connect.

End Function

 

Function GetResourceIDFromName(connection, strcomputera)

 

'On Error Resume Next

' Build Query string.

query = "SELECT ResourceId FROM SMS_R_System WHERE NetbiosName = """ & strcomputera & """"

' Execute the query

Set result = connection.ExecQuery(query)

If Err then

CheckError

'MsgBox "error on a"

Exit Function

End If

' Walk through the elements of the enumerator.

' Assume there will only be 1 computer with the requested name, if any.

resourceID = 0 ' Assume the worst

For each r in result

GetResourceIDFromName = r.ResourceID

'MsgBox r.resourceID

Next

 

End Function

Function GetResourceIDFromNameB(connection, strcomputerb)

 

On Error Resume Next

' Build Query string.

query = "SELECT ResourceId FROM SMS_R_System WHERE NetbiosName = """ & strComputerB & """"

' Execute the query

Set result = connection.ExecQuery(query)

If Err then

CheckError

Exit Function

End If

' Walk through the elements of the enumerator.

' Assume there will only be 1 computer with the requested name, if any.

resourceID = 0 ' Assume the worst

For each b in result

GetResourceIDFromNameB = b.ResourceID

'MsgBox b.resourceID

Next

 

End Function

Sub General_OnClick()

 

strHtm = "<table class=""tbl"">"

strHtm = strHtm & "<tr><td>_________________________</td><td>_____________</td></tr>"

strHtm = strHtm & "<tr><td>Source Computer </td><td>" & document.getElementById("computera").value & "</td></tr><tr></tr>"

strHtm = strHtm & "<tr><td>_________________________</td><td>_____________</td></tr>"

strHtm = strHtm & "<tr><td>Destination Computer </td><td>" & document.getElementById("computerb").value & "</td></tr>"

strHtm = strHtm & "</table>"

'Next

dataarea.innerhtml = strHtm

End Sub

 

</SCRIPT>

 

<body>

 

This is the Source Computer = <input type="text" id="ComputerA" size="30"> <p>This is the Destination Compouter = <input type="text" id="ComputerB" size="30"><P>

<input id=runbutton type="button" value="Create Computer Association" name="run_button" onClick="TestSub">

<input type="button" value="Create A New Association" onClick="window.location.reload()">

<div style="margin: 200px 0px 0px 0px; background-color: transparent; padding: 10px;">

<div id="dataarea"></div>

</div>

 

</body>

Share this post


Link to post
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.