shifting machine from one collection to another using vb script


Last month i was working on refresh scenario using SCCM 2007...
I have to save all users data & the task should be performed in a way where none of the users data is lost.
I was using normal refresh task sequence which we create in SCCM with the following steps :-
  • Store users state from XP machine.
  • Deploy Windows 7 on the same machine.
  • And restore users data.
now when i tried this TS i was not sure if the data is restored ,
as we divided the TS into two TS :-
TS#1 - Stores the users state from XP machine.
TS#2 - Deploy Windows 7 and Restores users data.
these TS were advertised to a single collection with the priority of TS#1 set to High and the Priority of TS#2 set to Low.so that the TS executes one after the other.TS#2 was set for PXE boot.
Now,the problem we were facing was, consider if the data is not stored in TS#1 and machine restarts after the execution of TS#1 and Execution of TS#2 starts.TS#2 gets Executed then the deployment of Windows 7 begins and after the deployment the restore step runs without restoring users data.so what user gets is a machine with windows 7 and no users data.

we wanted to make sure that the data gets restored.

so we created two collections one which holds XP machine and the other holds the machine in which TS#1 has been executed.(lets say first collection as coll1 & second collection coll2)

At the end of the TS#1 we executed a script which move the machine's to the coll2 if the restore phase has been executed successfully.

It made our work easier as when TS#1 gets executed on a machine and if all the task in the TS are successfully executed then the machine is automatically moved to coll2.
we advertised the TS#1 to coll1 & TS#2 to coll2(PXE boot).
TS#1 runs on the machine,stores user state and then moves the machine to coll2 and then restarts the machine now we have advertised TS#2 on coll2 so when the machine restarts it PXE Boots and Windows 7 is deployed with users data.

This is one of the use of this script it can be used in many other ways.

script :-


On Error Resume Next
Dim arrArguments
Set arrArguments = WScript.Arguments
If arrArguments.Count <> 3 Then
    WScript.Echo "Usage: cscript AddMeToCollection.vbs <SiteServerName> <CollectionID> %_SMSTSClientIdentity%"
    WScript.Echo "<SiteServerName> and <CollectionID> needs to be specified, but last parameter needs to be used as is."
    WScript.Quit
End If

Dim strServer, strCollID, strProvNamespace
Dim strComputerName, strGUID, strResourceID
Dim strUser, strPassword
strResourceID = 0

strServer = arrArguments(0)
strCollID = arrArguments(1)
strGUID = arrArguments(2)

WScript.Echo ""
WScript.Echo "==================================="
WScript.Echo "   ADDING COMPUTER TO COLLECTION"
WScript.Echo "==================================="
WScript.Echo "Site Server specified: " & strServer
WScript.Echo "Collection ID specified: " & strCollID
WScript.Echo "SMS Client " & strGUID

'Get the computer name
Set oNet = CreateObject("WScript.Network")
strComputerName = oNet.ComputerName
WScript.Echo "Computer Name: " & strComputerName
Set oNet = Nothing

'Connect to root/sms namespace on SMS Site Server to find the Provider Namespace
Set objLocator = CreateObject("WbemScripting.SWbemLocator")   
Set oWbem = objLocator.ConnectServer(strServer, "root/sms") 
If Err.number <> 0 Then
    WScript.Echo "Error connecting to root\sms namespace to find Provider Location. Exiting!"
    WScript.Echo "Error = " & Err.number & " - " & Err.Description
    WScript.Quit
End If
Set colNameSpace = oWbem.ExecQuery("SELECT * FROM SMS_ProviderLocation")
For Each item in colNameSpace
       WScript.Echo "SMS Provider Namespace = " & item.NamespacePath
    strProvNamespace = item.NamespacePath
Next

'Connect to the Provider Namespace
Set oWbem = objLocator.ConnectServer(strServer, strProvNamespace)
If Err.number <> 0 Then
    WScript.Echo "Error connecting to SMS Provider namespace. Exiting!"
    WScript.Echo "Error = " & Err.number & " - " & Err.Description
    WScript.Quit
Else
    WScript.Echo "Successfully Connected to the SMS Provider Namespace"
End If

'Find out the Resource ID of the computer by querying SMS_R_System Class against the SMS GUID
Set colResources = oWbem.ExecQuery("SELECT ResourceID FROM SMS_R_System WHERE SMSUniqueIdentifier = '" & strGUID & "'")
For Each oResource In colResources
    strResourceID = oResource.ResourceID
    WScript.Echo "Resource ID = " & strResourceID
Next

'If Resource ID was not found, exit gracefully
If strResourceID = 0 Then
    WScript.Echo "Could not find the Resource ID for the computer. Exiting!"
    WScript.Quit
End If

'Verify if the specified collection exists
Set oCollection = oWbem.Get("SMS_Collection.CollectionID=" & """" & strCollID & """")
If oCollection.Name = "" Then
    WScript.Echo "Specified Collection (" & strCollID & ") was Not Found. Exiting!"
    WScript.Quit
End If

'Create a Direct Membership rule
Set oDirectRule = oWbem.Get("SMS_CollectionRuleDirect").SpawnInstance_ ()
oDirectRule.ResourceClassName = "SMS_R_System"
oDirectRule.ResourceID = strResourceID
oDirectRule.RuleName = strComputerName & " - SMSTS"

'Add the Direct Membership Rule to the specified collection
oCollection.AddMembershipRule oDirectRule
If Err.Number <> 0 Then
    WScript.Echo "Could not add the computer to the specified collection. Exiting!"
    WScript.Echo "Error = " & Err.number & " - " & Err.Description
    WScript.Quit
Else
    WScript.Echo strComputerName & " successfully added To " & strCollID
End If

WScript.Echo "==================================="
WScript.Echo ""

Set objLocator = Nothing
Set oWbem = Nothing
Set oCollection = Nothing
Set oDirectRule = Nothing

Just create a package for the script and you need to pass 3 arguments in the command line :-


  • SCCM site server name.
  • Collection id of the collection where the machine needs to be moved.
  • GUID of the machine.
In my case these were the arguments :-


DC – SCCM site server name.
SVR00021 – collection id of the collection where the machine needs to be moved.
%_SMSTSClientIdentity% - GUID of the machine.(you don't have to change this it will remain as it is and this variable is set automatically when the TS is executed).

Make sure that you execute this step using an account which have rights to access SMS Provider.
(Generally the account which you created for the sccm machine).

Also in the TS "run this step with following account" will appear only if you are using SCCM 2007 sp2 or sp3 so make sure you have this.




 

No comments:

Post a Comment