VB script to get current site code for multiple clients and reasign their site code
Here is a VB script to read clients from a txt file and fetch themto a dictionary and ping test them, get their current site code and reassign their site code.
Option Explicit
On Error Resume Next
Dim objDictionary
Dim objFSO
Dim objTextFile
Dim strNextLine
Dim strComputer
Dim result
Dim objItem
Dim TxtFile
Dim i
Dim Dping
Dim Pinged
Dim pingresult
Dim arrPingResult
Dim u
Dim oCCMNamespace
Dim smsClient
Dim inparam
Dim Newresult
Dim NewSite
Const ForReading = 1
'fetch clients list from a txt file to a dictionary
TxtFile = "C:\Myworkplace\Clientlist.txt"
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
(TxtFile, ForReading)
i = 0
Do Until objTextFile.AtEndofStream
strNextLine = objTextFile.Readline
objDictionary.Add i, strNextLine
i = i + 1
Loop
For Each objItem in objDictionary
strComputer = objDictionary.Item(objItem)
Set Dping = Wscript.Createobject("Dynu.Ping")
Pinged = False
'ping clients
pingresult = DPing.ping(strComputer, 1, 1, 2000)
arrPingResult = Split(PingResult,vbCRLF)
For u = 0 To UBound(arrPingResult)
If InStr(1,arrPingResult(u),"bytes=1",1) > 0 Then
Pinged = True
'get the current site code
set oCCMNamespace = GetObject("winmgmts://" & strComputer & "/root/ccm")
Set smsClient = oCCMNamespace.Get("SMS_Client")
Set result = smsClient.ExecMethod_("GetAssignedSite")
WScript.Echo strComputer & " Current Assigned Site is: " & result.sSiteCode
' Reassign the site code
Set inParam =smsClient.Methods_.Item("SetAssignedSite").inParameters.SpawnInstance_()
inParam.sSiteCode = "004"
Set Newresult = smsClient.ExecMethod_("SetAssignedSite", inParam)
Set NewSite = smsClient.ExecMethod_("GetAssignedSite")
WScript.Echo strComputer & " New Assigned Site is : " & NewSite.sSiteCode
End If
Next
If Pinged = False Then
WScript.Echo("Unable to ping " & strComputer)
End if
Next