; ProfileMigrator
;
; Used during migration to re-profile machines after we've done a migration
;
; Version 1.00 - Nathan Reeves - 15 August 2002
;
; -- Start Variable Definition --
$NewDomainDC = "DCNAME"
$NewDomainName = "W2KDomain"

; -- End Variable Definition --

; -- Function Definition Start --
;FUNCTION StrSidToHexSid($StringSid)
;AUTHOR Howard A. Bullock (habullock@comcast.net)
;ACTION Converts a Sid in string (S-1-5-21-...) format to Hex format.
;SYNTAX StrSidToHexSid($StringSid)
;PARAMETERS $StringSid (Required) - String value
;REMARKS Do not prefix the computer name with "\\"'s.
;RETURNS String
;DEPENDENCIES KiXtart 4.02
;EXAMPLES StrSidToHexSid("S-1-5-21-24129212-1035812195-1543859470-1401")
; returns "010500000000000515000000BC2E7001633DBD3D0E6D055C79050000"
;
Function StrSidToHexSid($StringSid)
dim $i, $j, $Parts, $Sac, $Sid, $x

$Sid = ""
If Left($StringSid,4) = "S-1-"
$Parts = split($StringSid, "-")

For $i=1 to 2
$Sid = $Sid + PadStr(DecToHex($Parts[$i]),0,2)
Next

$Sac = Ubound($Parts) - 2
$Sid = $Sid + PadStr($Sac,0,12)

For $i=1 to $Sac
$x = PadStr(DecToHex($Parts[$i+2]),0,8)
For $j=7 to 1 Step -2
$Sid = $Sid + Substr($x,$j,2)
Next
Next
Else
? "StrSidToHexSid - Invalid input"
Exit 1
Endif
$StrSidToHexSid = $Sid
Exit 0
Endfunction

;FUNCTION PadStr($Input,$Pad,$Length)
;AUTHOR Howard A. Bullock (habullock@comcast.net)
;ACTION Left or Right pad a string.
;SYNTAX PadStr($Input,$Pad,$Length)
;PARAMETERS $Input (Required) - String value
; $Pad (Required) - String value
; $Length (Required) - Integer (Max length of padded string)
; $PadSide (Optional) - String [L|R] Default is "L"
;REMARKS The $Pad can be one or more characters. If the Padding exceeds
; the specified maximum length then the resulting string is trimmed
; preserving the original data.
;RETURNS String
;DEPENDENCIES KiXtart 4.02 or higher
;EXAMPLES PadStr("401","0",8) result "00000401"
; PadStr("401","0",8,"R") result "40100000"
;
Function PadStr($Input, $Pad, $Length, optional $PadSide)
Dim $i, $x
$PadStr = ""
$Input = "" + $Input
$Pad = "" + $Pad
$Length = 0 + $Length
If $PadSide="" or Len($PadSide)>1 or Instr("LR",$PadSide)= 0
$PadSide = "L"
Endif

$x = Len($Input)

For $i=$x to $Length - 1 Step Len("$Pad")
If $PadSide = "L"
$Input = $Pad + $Input
Else
$Input = $Input + $Pad
Endif
Next
If $PadSide = "L"
$Input = Right($Input, $Length)
Else
$Input = Left($Input, $Length)
Endif
$PadStr = $Input
Exit 0
Endfunction
; -- Function Definition End --

; -- Start Profile Migrator --
; List all keys in the Profilelist directory
$Index=0
:ProfileLoop
? "Index = $Index"
$ProfileName = EnumKey("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\ProfileList", $Index)
If @Error = 0
; Get the path of the profile
$ProfileDirString = ReadValue("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\$ProfileName", "ProfileImagePath")
; Now work out the actual directory name. Minus the length of the known path to do this
$ProfileDirLength = Len($ProfileDirString) - 22
;? $ProfileDirLength
;
$ProfileDirName = Substr($ProfileDirString, 23, 24 - ProfileDirLength)
? "'"$ProfileDirName"'"
; Work out if we have the administrator profile dir as we don't want to alter this
If $ProfileDirName <> "Administrator"
; See if the profile dir has a period. If so we don't want to touch it as it probably
; means that we have two profiles. The later one with a period should be left as is.
$AdditionalProfileP = Instr($ProfileDirName, ".")
If $AdditionalProfileP = 0
; Lets add the user to the local Admin group now
$AdminCmdLine = "net localgroup Administrators $NewDomainName\$ProfileDirName /ADD"
Shell "cmd /q /c $AdminCmdLine"
; Find the new Sid
? "Working on Profile: $ProfileDirName"
$CmdLine = "UserSid.bat \\$NewDomainDC $ProfileDirName"
SHELL "cmd /q /c $CmdLine"
If Open(3, "sid.txt") = 0
$NewSid = ReadLine(3)
If @Error = 0
Sleep 1
; Lets save the old key and restore it as the new key
$SaveResult = Savekey("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\$ProfileName", "%TEMP%\$ProfileName.reg")
If $SaveResult = 0
? "Loading Key [$ProfileName] to [$NewSid]"
$AddResult = AddKey("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\$NewSid")
$LoadPath = "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\$NewSid"
$LoadResult = LoadKey("$LoadPath", "%TEMP%\$ProfileName.reg")
; Now edit the Sid Key to enter the correct SID
$NewHexSid = StrSidToHexSid($NewSid)
$WriteResult = WriteValue("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\$NewSid", "SID", "$NewHexSid", "REG_BINARY")
EndIf
EndIf
$CloseCode = Close(3)
Sleep 1
EndIf
EndIf
EndIf
$Index = $Index + 1
Goto ProfileLoop
EndIf