' This is a script to Move clients on SEPM based on the host name of the client. ' Created by: Elisha Riedlinger ' Last Modified: 03/31/2010 ' Version 2.02 Option Explicit '-------------------------------------------------------- 'Set the below values to your database password and username '-------------------------------------------------------- Dim DBUser : DBUser = "sem5" Dim DBPass : DBPass = "password" Dim TempGroupName : TempGroupName = "Default Group" '-------------------------------------------------------- 'Other settings '-------------------------------------------------------- Dim OnlyCheckClientsInTempGroup : OnlyCheckClientsInTempGroup = True Dim SilentMode : SilentMode = False Dim EnableLogging : EnableLogging = True Dim sLogFile : sLogFile = "MoveClients.log" Dim ConnectionString : ConnectionString = "DSN=SymantecEndpointSecurityDSN;UID=" & DBUser & ";PWD=" & DBPass '-------------------------------------------------------- '-------------------------------------------------------- '-------------------------------------------------------- '-------------------------------------------------------- 'Checking for 64-bit Windows Dim Wshshell : Set WshShell = WScript.CreateObject("WScript.Shell") Dim ProcessorType : ProcessorType = WshShell.RegRead("HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor\0\Identifier") If not (InStr(ProcessorType, "x86") > 0) Then ' MsgBox("64-bit Windows OS detected.") ' Checking if the script is running in 32-bit mode already If not (InStr(ucase(WScript.FullName), "SYSWOW64") > 0) Then ' ReRun script Wshshell.Run "%windir%\SysWOW64\wscript.exe """ & WScript.ScriptFullName & """" WScript.Quit(0) End If End If 'Declare variables Dim RootGroup, DomainID, TempGroupID Dim ListCount, Hostname(), HostGroup(), SubnetLow(), SubnetHigh(), TotalCount Dim SearchType, OutputFile, FoundClients Dim oFSO, connect Set oFSO = CreateObject("Scripting.FileSystemObject") '---------------------------- Vars for MD5 conversion --------------------- Private lngTrack Private arrLongConversion(4) Private arrSplit64(63) Private Const OFFSET_4 = 4294967296 Private Const MAXINT_4 = 2147483647 Private Const S11 = 7 Private Const S12 = 12 Private Const S13 = 17 Private Const S14 = 22 Private Const S21 = 5 Private Const S22 = 9 Private Const S23 = 14 Private Const S24 = 20 Private Const S31 = 4 Private Const S32 = 11 Private Const S33 = 16 Private Const S34 = 23 Private Const S41 = 6 Private Const S42 = 10 Private Const S43 = 15 Private Const S44 = 21 '---------------------------- End vars for MD5 conversion --------------------- 'Starting Log Output call StartLog 'Testing SQL Connection call SQLConnect 'Get Root Group Name call GetRootGroupName 'MoveClients call StartRun("new_IPgroupsSCHOOLS.txt", 1) call StartRun("Hostgroups.txt", 2) call StartRun("Usergroups.txt", 3) call StartRun("OSgroups.txt", 4) call StartRun("SwitchClientGroups.txt", 5) 'Close and exit call CloseLog() WScript.Quit(0) '-------------------------------------------------------- Sub StartLog () Const ForAppending = 8 On Error Resume Next Err.Clear ' Clear the error. If EnableLogging then If oFSO.FileExists(sLogFile) Then Set OutputFile = oFSO.OpenTextFile(sLogFile, ForAppending, True) Else Set OutputFile = oFSO.CreateTextFile(sLogFile, True) End if End if LogEvent("########################################################################" & vbCRLF & "# Begin MoveClient " & Date() & " " & Time() & vbCRLF & "########################################################################" & vbCRLF) LogEvent("::SQL User for this run: " & DBUser & vbCRLF) LogEvent("::SQL Pass for this run: " & DBPass & vbCRLF) LogEvent("::Temp Group for this run: " & TempGroupName & vbCRLF) End Sub Sub SQLConnect() Dim sql, sErrorMessage On Error Resume Next Err.Clear ' Clear the error. LogEvent(Date() & " " & Time() & " Testing connection to ODBC DSN: " & ConnectionString & vbCRLF) Set connect = CreateObject("ADODB.Connection") connect.Open ConnectionString If err.number = 0 Then LogEvent(" ::Success" & vbCRLF) Else LogEvent(" ::Failure. Return Code: " & err.number) If err.number = -2147217843 Then LogEvent(" - Incorrect Username/Password" & vbCRLF) sErrorMessage = "Incorrect Username/Password. Verify the username and password values set in this script." ElseIf err.number = -2147467259 Then LogEvent(" - Unable to find the ODBC DSN specified" & vbCRLF) sErrorMessage = "Unable to open a connection to the SEPM Database. Verify this script is running on the SEPM." Else LogEvent(" - This error was not experienced during testing of this tool." & vbCRLF) sErrorMessage = "Unhandled exception. Please review SEPMMoveClients.log on your root drive for further information." End If Err.Clear ' Clear the error. ShowMessage("Could not connect to the database using ODBC DSN. Verify the following:" & sErrorMessage & vbCRLF & "This script will now exit.") call CloseLog() End If End Sub Sub GetRootGroupName () Dim sql, resultSet 'SQL Connection On Error Resume Next Err.Clear ' Clear the error. sql="Select * from IDENTITY_MAP where type like 'SemClientGroup' and not name like '%\%';" 'LogEvent(" ::Root Group Name Query: " & sql & vbCRLF) Set resultSet = connect.Execute(sql) If err.number = 0 Then RootGroup = resultSet("name") DomainID = resultSet("domain_id") LogEvent(" ::Success" & vbCRLF & " ::Root Group Name: " & RootGroup & vbCRLF & " ::DomainID: " & DomainID & vbCRLF) resultSet.Close Else LogEvent(" ::Failed" & vbCRLF) ShowMessage("Failed to query the database for the Root Group Name and Domain ID." & vbCRLF & "This script will now exit.") Err.Clear ' Clear the error. resultSet.Close call CloseLog() End If sql="Select * from IDENTITY_MAP where type like 'SemClientGroup' and name like '" & RootGroup & "\" & TempGroupName & "' and domain_id = '" & DomainID & "';" 'LogEvent(" ::Temporary GroupID Query: " & sql & vbCRLF) Set resultSet = connect.Execute(sql) If err.number = 0 Then TempGroupID = resultSet("id") resultSet.Close LogEvent(" ::Success" & vbCRLF & " ::Temp GroupID: " & TempGroupID & vbCRLF) Else LogEvent(" ::Failed" & vbCRLF) ShowMessage("Failed to query the database for the Temp GroupID." & vbCRLF & "This script will now exit.") Err.Clear ' Clear the error. resultSet.Close call CloseLog() End If End Sub Sub StartRun(sFile, newSearchType) Dim oFile, sText On Error Resume Next Err.Clear ' Clear the error. 'Init vars SearchType = newSearchType FoundClients = False ListCount = 0 TotalCount = 1000 ReDim Hostname(TotalCount) ReDim HostGroup(TotalCount) ReDim SubnetLow(TotalCount) ReDim SubnetHigh(TotalCount) 'Open group file If oFSO.FileExists(sFile) Then Set oFile = oFSO.OpenTextFile(sFile, 1) If err.number = 0 Then LogEvent(" ::Processing " & sFile & vbCRLF) Else Err.Clear ' Clear the error. LogEvent("Unable to open file: " & sFile & vbCRLF) exit sub End If Dim bFileEmpty : bFileEmpty = TRUE Do While Not oFile.AtEndOfStream sText = oFile.ReadLine If Trim(sText) <> "" Then bFileEmpty = FALSE call AnalyzeString(sText) End If Loop oFile.Close If bFileEmpty = TRUE Then LogEvent("Empty settings file: " & sFile & vbCRLF) exit sub End If Else LogEvent("Missing settings file: " & sFile & vbCRLF) exit sub End If 'Update client groups If SearchType = 5 then call SwitchClientGroups Else call UpdateClientGroups End if End Sub Sub AnalyzeString (inputString) Dim Comma, HostText, GroupText Dim Dash, SubnetLowText, SubnetHighText, SubnetLowNum, SubnetHighNum, SubnetText Comma = instr(inputString, ",") If Comma > 0 then If SearchType = 1 then 'This is an IP search type SubnetText = replace(mid(inputString, 1, Comma - 1), " ", "") GroupText = trim(mid(inputString, Comma + 1, len(inputString))) Dash = instr(SubnetText, "-") If Dash > 0 then SubnetLowText = mid(SubnetText, 1, Dash - 1) SubnetHighText = mid(SubnetText, Dash + 1, len(SubnetText)) SubnetLowNum = GetSubnet(SubnetLowText) SubnetHighNum = GetSubnet(SubnetHighText) Else SubnetLowNum = GetSubnet(SubnetText) SubnetHighNum = SubnetLowNum End if If SubnetLowNum <> 0 and SubnetHighNum <> 0 and GroupText <> "" then ListCount = ListCount + 1 if ListCount > TotalCount then TotalCount = TotalCount + 1000 ReDim Preserve SubnetLow(TotalCount) ReDim Preserve SubnetHigh(TotalCount) ReDim Preserve HostGroup(TotalCount) End if SubnetLow(ListCount) = SubnetLowNum SubnetHigh(ListCount) = SubnetHighNum HostGroup(ListCount) = GroupText LogEvent(" ::" & SubnetLow(ListCount) & " - " & SubnetHigh(ListCount) & " -|- " & HostGroup(ListCount) & vbCRLF) End if Else HostText = trim(mid(inputString, 1, Comma - 1)) GroupText = trim(mid(inputString, Comma + 1, len(inputString))) If HostText <> "" and GroupText <> "" then ListCount = ListCount + 1 if ListCount > TotalCount then TotalCount = TotalCount + 1000 ReDim Preserve Hostname(TotalCount) ReDim Preserve HostGroup(TotalCount) End if Hostname(ListCount) = lcase(HostText) HostGroup(ListCount) = GroupText LogEvent(" ::" & Hostname(ListCount) & " -|- " & HostGroup(ListCount) & vbCRLF) End if End if End if End Sub Sub SwitchClientGroups Dim sql, resultSet, x, PolicyMode, CheckPolicyMode, Flag Dim GroupName, GetPolicyMode, UserName, UserDomain, UserDomainName, Hash On Error Resume Next Err.Clear ' Clear the error. x = 0 Do x = x + 1 GroupName = HostGroup(x) GetPolicyMode = lcase(Hostname(x)) 'Get policy mode PolicyMode = "" CheckPolicyMode = "" If (InStr(GetPolicyMode, "to") > 0) and (InStr(GetPolicyMode, "user") > 0) then PolicyMode = "0" CheckPolicyMode = "1" ElseIf (InStr(GetPolicyMode, "to") > 0) and (InStr(GetPolicyMode, "computer") > 0) then PolicyMode = "1" CheckPolicyMode = "0" End if 'Check policy mode and start query If PolicyMode <> "" and CheckPolicyMode <> "" then Err.Clear ' Clear the error. LogEvent(" ::Checking for clients to switch in group " & RootGroup & "\" & GroupName & vbCRLF) sql="Select SEM_CLIENT.COMPUTER_ID, SEM_COMPUTER.COMPUTER_NAME, SEM_COMPUTER.COMPUTER_DOMAIN_NAME, SEM_CLIENT.USER_NAME, SEM_CLIENT.USER_DOMAIN_NAME, SEM_CLIENT.HARDWARE_KEY, SEM_COMPUTER.CURRENT_LOGIN_USER, SEM_COMPUTER.CURRENT_LOGIN_DOMAIN " sql=sql & "from SEM_CLIENT, SEM_COMPUTER, IDENTITY_MAP where SEM_CLIENT.COMPUTER_ID = SEM_COMPUTER.COMPUTER_ID and SEM_CLIENT.DELETED = 0 and SEM_COMPUTER.DELETED = 0 " sql=sql & "and SEM_CLIENT.DOMAIN_ID = '" & DomainID & "' and SEM_CLIENT.POLICY_MODE = " & CheckPolicyMode & " and IDENTITY_MAP.ID = SEM_CLIENT.GROUP_ID and SEM_CLIENT.PIN_MARK = 0 " sql=sql & "and IDENTITY_MAP.TYPE like 'SemClientGroup' and IDENTITY_MAP.NAME like '" & RootGroup & "\" & GroupName & "' and IDENTITY_MAP.DOMAIN_ID = '" & DomainID & "';" 'LogEvent(" ::Temp Group client List SQL Query: " & sql & vbCRLF) Set resultSet = connect.Execute(sql) If err.number = 0 Then FoundClients = False Do While Not resultSet.EOF FoundClients = True Flag = True 'Get username and domain name and check if they are blank If (resultSet("USER_NAME") & "" <> "") and (resultSet("USER_DOMAIN_NAME") & "" <> "") then UserName = resultSet("USER_NAME") UserDomain = resultSet("USER_DOMAIN_NAME") ElseIf (resultSet("CURRENT_LOGIN_USER") & "" <> "") and (resultSet("CURRENT_LOGIN_DOMAIN") & "" <> "") then UserName = resultSet("CURRENT_LOGIN_USER") UserDomain = resultSet("CURRENT_LOGIN_DOMAIN") Else If PolicyMode = "0" then Flag = False LogEvent(" ::Cannot convert " & resultSet("COMPUTER_NAME") & " to user mode. Username or domain is blank." & vbCRLF) End if UserName = "" UserDomain = "" End if 'Check if user already exists If Flag and PolicyMode = "0" then If CheckForUser(UserName, UserDomain) = True then Flag = False LogEvent(" ::Cannot convert " & resultSet("COMPUTER_NAME") & " to user mode. User already exists." & vbCRLF) End if End if 'Check if computer name is blank If Flag and (PolicyMode = "1") and ((resultSet("COMPUTER_NAME") & "" = "") or (resultSet("COMPUTER_DOMAIN_NAME") & "" = "")) then Flag = False LogEvent(" ::Cannot convert " & UserName & " to computer mode. Computer name or domain is blank." & vbCRLF) End if 'Get hash Hash = "" If PolicyMode = "0" then UserDomainName = UserName & UserDomain Else UserDomainName = "" End if If Flag then Hash = UCase(CalculateMD5(DomainID & PolicyMode & UserDomainName & resultSet("COMPUTER_NAME") & resultSet("COMPUTER_DOMAIN_NAME") & resultSet("HARDWARE_KEY"))) If Flag and Hash = "" then Flag = False LogEvent(" ::Cannot compute hash for " & resultSet("COMPUTER_NAME") & ". Cannot convert to user mode." & vbCRLF) End if If Flag then call SwitchClient(resultSet("COMPUTER_ID"), PolicyMode, UserName, UserDomain, resultSet("COMPUTER_NAME") & "", resultSet("COMPUTER_DOMAIN_NAME") & "", resultSet("HARDWARE_KEY") & "", Hash) resultSet.MoveNext Loop If not FoundClients then LogEvent(" ::No clients found in " & GroupName & " that need to be switched." & vbCRLF) Else LogEvent(" ::Failed to retrieve client list from database." & vbCRLF) End If Else LogEvent(" ::Invalid policy mode for group: " & GroupName & vbCRLF) End If Loop until x >= ListCount resultSet.Close End Sub Function CheckForUser (UserName, UserDomain) Dim sql, resultSet On Error Resume Next Err.Clear ' Clear the error. sql="Select USER_NAME, USER_DOMAIN_NAME " sql=sql & "from SEM_CLIENT where USER_NAME like '" & UserName & "' and USER_DOMAIN_NAME like '" & UserDomain & "' " sql=sql & "and DOMAIN_ID = '" & DomainID & "' and POLICY_MODE = 0 and DELETED = 0 and PIN_MARK = 0;" 'LogEvent(" ::Check for client SQL Query: " & sql & vbCRLF) Set resultSet = connect.Execute(sql) If NOT resultSet.EOF then CheckForUser = True Else CheckForUser = False End if resultSet.Close End Function Sub SwitchClient (ComputerID, PolicyMode, UserName, UserDomain, ComputerName, ComputerDomain, HardwareKey, Hash) Dim sql, resultSet Dim NewTimeStamp, NewUSN On Error Resume Next Err.Clear ' Clear the error. FoundClients = True If PolicyMode = "0" then LogEvent(Date() & " " & Time() & " ::Switching Client " & ComputerName & " to user mode" & vbCRLF) Else LogEvent(Date() & " " & Time() & " ::Switching Client " & UserName & " to computer mode" & vbCRLF) End if NewTimeStamp = "(Select TOP 1 TIME_STAMP + DateDiff(second, DateAdd(second, TIME_STAMP/1000,'1970-01-01'), GetDate()) as NewTime from SEM_CLIENT where COMPUTER_ID='" & ComputerID & "' and domain_id='" & DomainID & "' and DELETED=0)" NewUSN = "(select SEQ_NUM + 1 as NewUSN from SE_Global)" sql="BEGIN" & vbCRLF sql=sql & "Update SEM_Computer set CURRENT_LOGIN_USER='" & UserName & "', CURRENT_LOGIN_DOMAIN='" & UserDomain & "', TIME_STAMP=" & NewTimeStamp & ", USN=" & NewUSN & " where COMPUTER_ID='" & ComputerID & "';" & vbCRLF sql=sql & "Update SEM_Client set USER_NAME='" & UserName & "', USER_DOMAIN_NAME='" & UserDomain & "', POLICY_MODE=" & PolicyMode & ", HASH='" & Hash & "', TIME_STAMP=" & NewTimeStamp & ", USN=" & NewUSN & " where COMPUTER_ID='" & ComputerID & "';" & vbCRLF sql=sql & "Update SE_Global set SEQ_NUM = " & NewUSN & ";" & vbCRLF sql=sql & "END" & vbCRLF Set resultSet = connect.Execute(sql) resultSet.Close End Sub Sub UpdateClientGroups Dim sql, resultSet On Error Resume Next Err.Clear ' Clear the error. LogEvent(" ::Checking for clients to move" & vbCRLF) sql="Select SEM_CLIENT.COMPUTER_ID, SEM_CLIENT.CLIENT_ID, SEM_COMPUTER.OPERATION_SYSTEM, SEM_COMPUTER.COMPUTER_NAME, SEM_CLIENT.USER_NAME, SEM_CLIENT.GROUP_ID, SEM_CLIENT.POLICY_MODE, " sql=sql & "CAST((case when SEM_COMPUTER.IP_ADDR1 < 0 then 0xFFFFFFFF + SEM_COMPUTER.IP_ADDR1 else SEM_COMPUTER.IP_ADDR1 end / 256 / 256 / 256) & 0xFF as VARCHAR) + '.' + " sql=sql & "CAST((case when SEM_COMPUTER.IP_ADDR1 < 0 then 0xFFFFFFFF + SEM_COMPUTER.IP_ADDR1 else SEM_COMPUTER.IP_ADDR1 end / 256 / 256) & 0xFF as VARCHAR) + '.' + " sql=sql & "CAST((case when SEM_COMPUTER.IP_ADDR1 < 0 then 0xFFFFFFFF + SEM_COMPUTER.IP_ADDR1 else SEM_COMPUTER.IP_ADDR1 end / 256) & 0xFF as VARCHAR) + '.' + " sql=sql & "CAST( case when SEM_COMPUTER.IP_ADDR1 < 0 then 0xFFFFFFFF + SEM_COMPUTER.IP_ADDR1 else SEM_COMPUTER.IP_ADDR1 end & 0xFF as VARCHAR) as LOCAL_IP_ADDRESS " sql=sql & "from SEM_CLIENT, SEM_COMPUTER where SEM_CLIENT.COMPUTER_ID = SEM_COMPUTER.COMPUTER_ID and SEM_CLIENT.DELETED = 0 and SEM_COMPUTER.DELETED = 0 and SEM_CLIENT.PIN_MARK = 0 " sql=sql & "and SEM_CLIENT.DOMAIN_ID = '" & DomainID & "'" If OnlyCheckClientsInTempGroup Then sql=sql & " and SEM_CLIENT.group_id like '" & TempGroupID & "';" Else sql=sql & ";" End if 'LogEvent(" ::Temp Group client List SQL Query: " & sql & vbCRLF) Set resultSet = connect.Execute(sql) If err.number = 0 Then Do While Not resultSet.EOF call MoveClient (resultSet("COMPUTER_NAME"), resultSet("USER_NAME"), resultSet("LOCAL_IP_ADDRESS"), resultSet("OPERATION_SYSTEM"), resultSet("COMPUTER_ID"), resultSet("CLIENT_ID"), resultSet("GROUP_ID"), resultSet("POLICY_MODE")) resultSet.MoveNext Loop If not FoundClients then LogEvent(" ::No clients found that need to be moved." & vbCRLF) Else LogEvent(" ::Failed to retrieve client list from database." & vbCRLF) End If resultSet.Close End Sub Sub MoveClient (ComputerName, UserName, IPAddress, OSType, ComputerID, ClientID, ComputerGroup, PolicyMode) Dim sql, resultSet Dim GroupID, NewTimeStamp, NewUSN Dim GroupName If SearchType = 1 then GroupName = GetGroupName(IPAddress) If SearchType = 2 then GroupName = GetGroupName(lcase(ComputerName)) If (SearchType = 3) and (PolicyMode = 0) then GroupName = GetGroupName(lcase(UserName)) If SearchType = 4 then GroupName = GetGroupName(lcase(OSType)) if GroupName = "" then Exit Sub FoundClients = True On Error Resume Next Err.Clear ' Clear the error. GroupID = "" 'SQL 2005/2008 select statement sql="Select IDENTITY_MAP.ID from IDENTITY_MAP, BASIC_METADATA where IDENTITY_MAP.Name like '" & RootGroup & "\" & GroupName & "' and IDENTITY_MAP.Type like 'SemClientGroup' and BASIC_METADATA.type like 'SemClientGroupTree' and CONVERT(varchar(max),CONVERT(varbinary(max), BASIC_METADATA.CONTENT)) like '%id=" & chr(34) & "' + IDENTITY_MAP.ID + '" & chr(34) & "%';" Set resultSet = connect.Execute(sql) 'Sybase select statement sql="Select IDENTITY_MAP.ID from IDENTITY_MAP, BASIC_METADATA where IDENTITY_MAP.Name like '" & RootGroup & "\" & GroupName & "' and IDENTITY_MAP.Type like 'SemClientGroup' and BASIC_METADATA.type like 'SemClientGroupTree' and CONVERT(text, BASIC_METADATA.CONTENT) like '%id=" & chr(34) & "' + IDENTITY_MAP.ID + '" & chr(34) & "%';" Set resultSet = connect.Execute(sql) If NOT resultSet.EOF then If NOT IsNull(resultSet("ID")) then GroupID = resultSet("ID") If (trim(GroupID) <> "") and (trim(GroupID) <> trim(ComputerGroup)) Then LogEvent(Date() & " " & Time() & " ::Moving Client '" & ComputerName & "' to group: '" & RootGroup & "\" & GroupName & "' ID: " & GroupID & vbCRLF) NewTimeStamp = "(Select TOP 1 TIME_STAMP + DateDiff(second, DateAdd(second, TIME_STAMP/1000,'1970-01-01'), GetDate()) as NewTime from SEM_CLIENT where COMPUTER_ID='" & ComputerID & "' and domain_id='" & DomainID & "' and DELETED=0)" NewUSN = "(select SEQ_NUM + 1 as NewUSN from SE_Global)" sql="BEGIN" & vbCRLF sql=sql & "Update SEM_Agent set GROUP_ID='" & GroupID & "', TIME_STAMP=" & NewTimeStamp & ", USN=" & NewUSN & " where COMPUTER_ID='" & ComputerID & "';" & vbCRLF sql=sql & "Update SEM_Client set GROUP_ID='" & GroupID & "', TIME_STAMP=" & NewTimeStamp & ", USN=" & NewUSN & " where CLIENT_ID='" & ClientID & "';" & vbCRLF sql=sql & "Update SE_Global set SEQ_NUM = " & NewUSN & ";" & vbCRLF sql=sql & "END" & vbCRLF Set resultSet = connect.Execute(sql) Else LogEvent(" ::Skipping client '" & ComputerName & "'. Already in the correct group or the new group is invalid." & vbCRLF) End if resultSet.Close End Sub Function GetSubnet (SubnetText) Dim Dot1, Dot2, Dot3 Dim Num1, Num2, Num3, Num4 GetSubnet = 0 Dot1 = instr(SubnetText, ".") if Dot1 <= 0 then Exit Function Dot2 = instr(Dot1 + 1, SubnetText, ".") if Dot2 <= 0 then Exit Function Dot3 = instr(Dot2 + 1, SubnetText, ".") if Dot3 <= 0 then Exit Function Num1 = mid(SubnetText, 1, Dot1 - 1) if Not IsNumeric(Num1) then Exit Function Num2 = mid(SubnetText, Dot1 + 1, Dot2 - Dot1 - 1) if Not IsNumeric(Num2) then Exit Function Num3 = mid(SubnetText, Dot2 + 1, Dot3 - Dot2 - 1) if Not IsNumeric(Num3) then Exit Function Num4 = mid(SubnetText, Dot3 + 1, len(SubnetText) - Dot3) if Not IsNumeric(Num4) then Exit Function GetSubnet = int(right("00" & Num1, 3) & right("00" & Num2, 3) & right("00" & Num3, 3) & right("00" & Num4, 3)) End Function Function GetGroupName (strText) Dim x, IPAddress GetGroupName = "" If SearchType = 1 then IPAddress = GetSubnet(strText) x = 0 Do x = x + 1 If SearchType = 1 then If IPAddress >= SubnetLow(x) and IPAddress <= SubnetHigh(x) then GetGroupName = HostGroup(x) Exit Function End if Else If (RegExpTest(Hostname(x), strText) > 0) then GetGroupName = HostGroup(x) Exit Function End if End if Loop until x >= ListCount End Function Function RegExpTest (patrn, strng) Dim regEx, Match, Matches Set regEx = New RegExp If (InStr(patrn, "?") > 0) or (InStr(patrn, "*") > 0) then Dim newPatrn newPatrn = patrn newPatrn = replace(newPatrn, "\", "\\") newPatrn = replace(newPatrn, "^", "\^") newPatrn = replace(newPatrn, "$", "\$") newPatrn = replace(newPatrn, "+", "\+") newPatrn = replace(newPatrn, "|", "\|") newPatrn = replace(newPatrn, "{", "\{") newPatrn = replace(newPatrn, "}", "\}") newPatrn = replace(newPatrn, "[", "\[") newPatrn = replace(newPatrn, "]", "\]") newPatrn = replace(newPatrn, "(", "\)") newPatrn = replace(newPatrn, ".", "\.") newPatrn = replace(newPatrn, "*", ".{0,}") newPatrn = replace(newPatrn, "?", ".{1}") regEx.Pattern = "^" & newPatrn & "$" regEx.IgnoreCase = True regEx.Global = True Set Matches = regEx.Execute(strng) RegExpTest = Matches.Count Else If patrn = strng then RegExpTest = 1 Else RegExpTest = 0 End if End if End Function Sub ShowMessage (strng) If not SilentMode then MsgBox(strng) End Sub Sub LogEvent (strng) If EnableLogging then OutputFile.Write(strng) End Sub Sub CloseLog () 'End output LogEvent("########################################################################" & vbCRLF & "# End MoveClient " & Date() & " " & Time() & vbCRLF & "########################################################################" & vbCRLF) If EnableLogging then OutputFile.Close connect.Close Set connect = Nothing 'notify user script completed ShowMessage("Script Completed" & vbCRLF & vbCRLF & "All activities of this script have been logged to " & sLogFile) WScript.Quit(0) End Sub ' ------ Call MD5 Hash Function -------- ' -------------------------------------- ' -------------------------------------- Public Function MD5FileHash(strFile) Dim strMD5 : strMD5 = "" Dim ofso : Set ofso = CreateObject("Scripting.FileSystemObject") If ofso.FileExists(strFile) then strMD5 = BinaryToString(ReadTextFile(strFile, "")) MD5FileHash = CalculateMD5(strMD5) Else MD5FileHash = strFile & VbCrLf & "Error: File not found" End if End Function ' -------------------------------------- Function ReadTextFile(FileName, CharSet) Const adTypeText = 2 Dim BinaryStream : Set BinaryStream = CreateObject("ADODB.Stream") BinaryStream.Type = adTypeText If Len(CharSet) > 0 Then BinaryStream.CharSet = CharSet End If BinaryStream.Open BinaryStream.LoadFromFile FileName ReadTextFile = BinaryStream.ReadText End Function ' -------------------------------------- Function BinaryToString(Binary) Dim cl1, cl2, cl3, pl1, pl2, pl3 Dim L cl1 = 1 cl2 = 1 cl3 = 1 L = LenB(Binary) Do While cl1<=L pl3 = pl3 & Chr(AscB(MidB(Binary,cl1,1))) cl1 = cl1 + 1 cl3 = cl3 + 1 If cl3>300 Then pl2 = pl2 & pl3 pl3 = "" cl3 = 1 cl2 = cl2 + 1 If cl2>200 Then pl1 = pl1 & pl2 pl2 = "" cl2 = 1 End If End If Loop BinaryToString = pl1 & pl2 & pl3 End Function ' -------------------------------------- Private Function MD5Round(strRound, a, b, C, d, X, S, ac) Select Case strRound Case "FF" a = MD5LongAdd4(a, (b And C) Or (Not (b) And d), X, ac) a = MD5Rotate(a, S) a = MD5LongAdd(a, b) Case "GG" a = MD5LongAdd4(a, (b And d) Or (C And Not (d)), X, ac) a = MD5Rotate(a, S) a = MD5LongAdd(a, b) Case "HH" a = MD5LongAdd4(a, b Xor C Xor d, X, ac) a = MD5Rotate(a, S) a = MD5LongAdd(a, b) Case "II" a = MD5LongAdd4(a, C Xor (b Or Not (d)), X, ac) a = MD5Rotate(a, S) a = MD5LongAdd(a, b) End Select End Function ' -------------------------------------- Private Function MD5Rotate(lngValue, lngBits) Dim lngSign Dim lngI lngBits = (lngBits Mod 32) If lngBits = 0 Then MD5Rotate = lngValue: Exit Function For lngI = 1 To lngBits lngSign = lngValue And &HC0000000 lngValue = (lngValue And &H3FFFFFFF) * 2 lngValue = lngValue Or ((lngSign < 0) And 1) Or (CBool(lngSign And &H40000000) And &H80000000) Next MD5Rotate = lngValue End Function ' -------------------------------------- Private Function TRID() Dim sngNum, lngnum Dim strResult sngNum = Rnd(2147483648) strResult = CStr(sngNum) strResult = Replace(strResult, "0.", "") strResult = Replace(strResult, ".", "") strResult = Replace(strResult, "E-", "") TRID = strResult End Function ' -------------------------------------- Private Function MD564Split(lngLength, bytBuffer()) Dim lngBytesTotal, lngBytesToAdd Dim intLoop, intLoop2, lngTrace Dim intInnerLoop, intLoop3 lngBytesTotal = lngTrack Mod 64 lngBytesToAdd = 64 - lngBytesTotal lngTrack = (lngTrack + lngLength) If lngLength >= lngBytesToAdd Then For intLoop = 0 To lngBytesToAdd - 1 arrSplit64(lngBytesTotal + intLoop) = bytBuffer(intLoop) Next MD5Conversion arrSplit64 lngTrace = (lngLength) Mod 64 For intLoop2 = lngBytesToAdd To lngLength - intLoop - lngTrace Step 64 For intInnerLoop = 0 To 63 arrSplit64(intInnerLoop) = bytBuffer(intLoop2 + intInnerLoop) Next MD5Conversion arrSplit64 Next lngBytesTotal = 0 Else intLoop2 = 0 End If For intLoop3 = 0 To lngLength - intLoop2 - 1 arrSplit64(lngBytesTotal + intLoop3) = bytBuffer(intLoop2 + intLoop3) Next End Function ' -------------------------------------- Private Function MD5StringArray(strInput) Dim intLoop Dim bytBuffer() ReDim bytBuffer(Len(strInput)) For intLoop = 0 To Len(strInput) - 1 bytBuffer(intLoop) = Asc(Mid(strInput, intLoop + 1, 1)) Next MD5StringArray = bytBuffer End Function ' -------------------------------------- Private Sub MD5Conversion(bytBuffer()) Dim X(16), a Dim b, C Dim d a = arrLongConversion(1) b = arrLongConversion(2) C = arrLongConversion(3) d = arrLongConversion(4) MD5Decode 64, X, bytBuffer MD5Round "FF", a, b, C, d, X(0), S11, -680876936 MD5Round "FF", d, a, b, C, X(1), S12, -389564586 MD5Round "FF", C, d, a, b, X(2), S13, 606105819 MD5Round "FF", b, C, d, a, X(3), S14, -1044525330 MD5Round "FF", a, b, C, d, X(4), S11, -176418897 MD5Round "FF", d, a, b, C, X(5), S12, 1200080426 MD5Round "FF", C, d, a, b, X(6), S13, -1473231341 MD5Round "FF", b, C, d, a, X(7), S14, -45705983 MD5Round "FF", a, b, C, d, X(8), S11, 1770035416 MD5Round "FF", d, a, b, C, X(9), S12, -1958414417 MD5Round "FF", C, d, a, b, X(10), S13, -42063 MD5Round "FF", b, C, d, a, X(11), S14, -1990404162 MD5Round "FF", a, b, C, d, X(12), S11, 1804603682 MD5Round "FF", d, a, b, C, X(13), S12, -40341101 MD5Round "FF", C, d, a, b, X(14), S13, -1502002290 MD5Round "FF", b, C, d, a, X(15), S14, 1236535329 MD5Round "GG", a, b, C, d, X(1), S21, -165796510 MD5Round "GG", d, a, b, C, X(6), S22, -1069501632 MD5Round "GG", C, d, a, b, X(11), S23, 643717713 MD5Round "GG", b, C, d, a, X(0), S24, -373897302 MD5Round "GG", a, b, C, d, X(5), S21, -701558691 MD5Round "GG", d, a, b, C, X(10), S22, 38016083 MD5Round "GG", C, d, a, b, X(15), S23, -660478335 MD5Round "GG", b, C, d, a, X(4), S24, -405537848 MD5Round "GG", a, b, C, d, X(9), S21, 568446438 MD5Round "GG", d, a, b, C, X(14), S22, -1019803690 MD5Round "GG", C, d, a, b, X(3), S23, -187363961 MD5Round "GG", b, C, d, a, X(8), S24, 1163531501 MD5Round "GG", a, b, C, d, X(13), S21, -1444681467 MD5Round "GG", d, a, b, C, X(2), S22, -51403784 MD5Round "GG", C, d, a, b, X(7), S23, 1735328473 MD5Round "GG", b, C, d, a, X(12), S24, -1926607734 MD5Round "HH", a, b, C, d, X(5), S31, -378558 MD5Round "HH", d, a, b, C, X(8), S32, -2022574463 MD5Round "HH", C, d, a, b, X(11), S33, 1839030562 MD5Round "HH", b, C, d, a, X(14), S34, -35309556 MD5Round "HH", a, b, C, d, X(1), S31, -1530992060 MD5Round "HH", d, a, b, C, X(4), S32, 1272893353 MD5Round "HH", C, d, a, b, X(7), S33, -155497632 MD5Round "HH", b, C, d, a, X(10), S34, -1094730640 MD5Round "HH", a, b, C, d, X(13), S31, 681279174 MD5Round "HH", d, a, b, C, X(0), S32, -358537222 MD5Round "HH", C, d, a, b, X(3), S33, -722521979 MD5Round "HH", b, C, d, a, X(6), S34, 76029189 MD5Round "HH", a, b, C, d, X(9), S31, -640364487 MD5Round "HH", d, a, b, C, X(12), S32, -421815835 MD5Round "HH", C, d, a, b, X(15), S33, 530742520 MD5Round "HH", b, C, d, a, X(2), S34, -995338651 MD5Round "II", a, b, C, d, X(0), S41, -198630844 MD5Round "II", d, a, b, C, X(7), S42, 1126891415 MD5Round "II", C, d, a, b, X(14), S43, -1416354905 MD5Round "II", b, C, d, a, X(5), S44, -57434055 MD5Round "II", a, b, C, d, X(12), S41, 1700485571 MD5Round "II", d, a, b, C, X(3), S42, -1894986606 MD5Round "II", C, d, a, b, X(10), S43, -1051523 MD5Round "II", b, C, d, a, X(1), S44, -2054922799 MD5Round "II", a, b, C, d, X(8), S41, 1873313359 MD5Round "II", d, a, b, C, X(15), S42, -30611744 MD5Round "II", C, d, a, b, X(6), S43, -1560198380 MD5Round "II", b, C, d, a, X(13), S44, 1309151649 MD5Round "II", a, b, C, d, X(4), S41, -145523070 MD5Round "II", d, a, b, C, X(11), S42, -1120210379 MD5Round "II", C, d, a, b, X(2), S43, 718787259 MD5Round "II", b, C, d, a, X(9), S44, -343485551 arrLongConversion(1) = MD5LongAdd(arrLongConversion(1), a) arrLongConversion(2) = MD5LongAdd(arrLongConversion(2), b) arrLongConversion(3) = MD5LongAdd(arrLongConversion(3), C) arrLongConversion(4) = MD5LongAdd(arrLongConversion(4), d) End Sub ' -------------------------------------- Private Function MD5LongAdd(lngVal1, lngVal2) Dim lngHighWord Dim lngLowWord Dim lngOverflow lngLowWord = (lngVal1 And &HFFFF&) + (lngVal2 And &HFFFF&) lngOverflow = lngLowWord \ 65536 lngHighWord = (((lngVal1 And &HFFFF0000) \ 65536) + ((lngVal2 And &HFFFF0000) \ 65536) + lngOverflow) And &HFFFF& MD5LongAdd = MD5LongConversion((lngHighWord * 65536) + (lngLowWord And &HFFFF&)) End Function ' -------------------------------------- Private Function MD5LongAdd4(lngVal1, lngVal2, lngVal3, lngVal4) Dim lngHighWord Dim lngLowWord Dim lngOverflow lngLowWord = (lngVal1 And &HFFFF&) + (lngVal2 And &HFFFF&) + (lngVal3 And &HFFFF&) + (lngVal4 And &HFFFF&) lngOverflow = lngLowWord \ 65536 lngHighWord = (((lngVal1 And &HFFFF0000) \ 65536) + ((lngVal2 And &HFFFF0000) \ 65536) + ((lngVal3 And &HFFFF0000) \ 65536) + ((lngVal4 And &HFFFF0000) \ 65536) + lngOverflow) And &HFFFF& MD5LongAdd4 = MD5LongConversion((lngHighWord * 65536) + (lngLowWord And &HFFFF&)) End Function ' -------------------------------------- Private Sub MD5Decode(intLength, lngOutBuffer(), bytInBuffer()) Dim intDblIndex Dim intByteIndex Dim dblSum intDblIndex = 0 For intByteIndex = 0 To intLength - 1 Step 4 dblSum = bytInBuffer(intByteIndex) + bytInBuffer(intByteIndex + 1) * 256 + bytInBuffer(intByteIndex + 2) * 65536 + bytInBuffer(intByteIndex + 3) * 16777216 lngOutBuffer(intDblIndex) = MD5LongConversion(dblSum) intDblIndex = (intDblIndex + 1) Next End Sub ' -------------------------------------- Private Function MD5LongConversion(dblValue) If dblValue < 0 Or dblValue >= OFFSET_4 Then Error 6 If dblValue <= MAXINT_4 Then MD5LongConversion = dblValue Else MD5LongConversion = dblValue - OFFSET_4 End If End Function ' -------------------------------------- Private Sub MD5Finish() Dim dblBits Dim arrPadding(72) Dim lngBytesBuffered arrPadding(0) = &H80 dblBits = lngTrack * 8 lngBytesBuffered = lngTrack Mod 64 If lngBytesBuffered <= 56 Then MD564Split (56 - lngBytesBuffered), arrPadding Else MD564Split (120 - lngTrack), arrPadding End If arrPadding(0) = MD5LongConversion(dblBits) And &HFF& arrPadding(1) = MD5LongConversion(dblBits) \ 256 And &HFF& arrPadding(2) = MD5LongConversion(dblBits) \ 65536 And &HFF& arrPadding(3) = MD5LongConversion(dblBits) \ 16777216 And &HFF& arrPadding(4) = 0 arrPadding(5) = 0 arrPadding(6) = 0 arrPadding(7) = 0 MD564Split 8, arrPadding End Sub ' -------------------------------------- Private Function MD5StringChange(lngnum) Dim bytA Dim bytB Dim bytC Dim bytD bytA = lngnum And &HFF& If bytA < 16 Then MD5StringChange = "0" & Hex(bytA) Else MD5StringChange = Hex(bytA) End If bytB = (lngnum And &HFF00&) \ 256 If bytB < 16 Then MD5StringChange = MD5StringChange & "0" & Hex(bytB) Else MD5StringChange = MD5StringChange & Hex(bytB) End If bytC = (lngnum And &HFF0000) \ 65536 If bytC < 16 Then MD5StringChange = MD5StringChange & "0" & Hex(bytC) Else MD5StringChange = MD5StringChange & Hex(bytC) End If If lngnum < 0 Then bytD = ((lngnum And &H7F000000) \ 16777216) Or &H80& Else bytD = (lngnum And &HFF000000) \ 16777216 End If If bytD < 16 Then MD5StringChange = MD5StringChange & "0" & Hex(bytD) Else MD5StringChange = MD5StringChange & Hex(bytD) End If End Function ' -------------------------------------- Private Function MD5Value() MD5Value = LCase(MD5StringChange(arrLongConversion(1)) & MD5StringChange(arrLongConversion(2)) & MD5StringChange(arrLongConversion(3)) & MD5StringChange(arrLongConversion(4))) End Function ' -------------------------------------- Public Function CalculateMD5(strMessage) Dim bytBuffer bytBuffer = MD5StringArray(strMessage) MD5Start MD564Split Len(strMessage), bytBuffer MD5Finish CalculateMD5 = MD5Value End Function ' -------------------------------------- Private Sub MD5Start() lngTrack = 0 arrLongConversion(1) = MD5LongConversion(1732584193) arrLongConversion(2) = MD5LongConversion(4023233417) arrLongConversion(3) = MD5LongConversion(2562383102) arrLongConversion(4) = MD5LongConversion(271733878) End Sub