I know that after you look at what I am doing you might be asking yourself, why? Well at least it’s turning out to be a good exercise in trying to learn Kixtart.
I am in a Citrix environment and we went from mandatory profiles to roaming profiles. I’ve noticed that there were several SIDs in the registry that were referring to the mandatory profiles. I also noticed that there were several SIDs that didn’t point to the correct location of the roaming profile and others that referred to the location of the local profile as %SystemRoot%\Profiles\UserID (NT 4.0 Terminal Server) and others that referred to it as %UserProfile%\UserID. And, other SIDs that were for UserID.000 or UserID.001. I thought I would cleanup the ProfileList key in the registry by deleting reference to the old mandatory profiles and invalid UserIDs and standardize the entries for the roaming and local profile locations.
What was I thinking?
My problem is that the script deletes some of the mandatory profiles, but not all of them. If I run the script again it will delete some more entries, but again not all of them. I have to run the script 3-4 times before it will delete all the keys referring to the mandatory profiles. Why won’t it do it all in one shot? Also, for users that no longer exist the script deletes some of these keys and for others it enters the “tsprof” error message in the roaming profile location. Then for some if the registry key does not have the “CentralProfile” value the WriteValue does not create the value. Does WriteValue only work if the value already exists?
Well here’s the code:
Code:
If RedirectOutput("H:\CleanProfileList.log") = 0
EndIf
? "== @Time == @Date ====================================================="
$vIndex = 0
$sRegKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
$aLocalUsers = "All Users","Administrator","Default User"
$sTSProfError = "Failed getting User Configuration, Error = 2 (0x2)"
:Loop1
$sKeyValue = EnumKey("$sRegKey", $vIndex)
If @ERROR = 0
$vCentralProfile = ReadValue("$sRegKey\$sKeyValue", "CentralProfile")
? "*............................................................*"
If @ERROR = 0
If LEFT($vCentralProfile, 11) = "C:\PROFILES"
? " CentralProfile is equal to old mandatory profile"
? " Deleting SID: $sKeyValue"
$vRC = DelKey("$sRegKey\$sKeyValue")
If $vRC = 0
? " .. SID Key $sKeyValue deleted .... "
EndIf
Else
$vProfileImagePath = ReadValue("$sRegKey\$sKeyValue", "ProfileImagePath")
If @ERROR = 0
? " Current Local Profile Path: $vProfileImagePath"
$aUserID = Split("$vProfileImagePath","\")
$vX = UBOUND($aUserID)
$sUserID = $aUserID[$vX]
? " User ID is: $UserID"
$vX = ASCAN($aLocalUsers, $sUserID)
If Not (1+$vX) ; $vX <> 0
$aTSProfCmd = WshPipe("tsprof /q /domain:MyCompany $UserID",1)
If @ERROR = 0
$sTSProfResults = $aTSProfCmd[0]
$aStripFirst = Split($sTSProfResults,"{")
$vX = UBOUND($aStripFirst)
$sProfilePath = $aStripFirst[$vX]
$aStripLast = Split($sProfilePath,"}")
$sTSProfilePath = $aStripLast[0]
If $sTSProfilePath <> $sTSProfError
$vRC = WriteValue("$sRegKey\$sKeyValue", "CentralProfile", "$sTSProfilePath", "REG_SZ")
If @ERROR = 0
? " Set Terminal Server Profile Path to: $sTSProfilePath"
Endif
$vRC = WriteValue("$sRegKey\$sKeyValue", "ProfileImagePath", _
"%%SystemRoot%%\Profiles\$sUserID", "REG_EXPAND_SZ")
If @ERROR = 0
? " Set Local Profile Path to: %%SystemRoot%%\Profiles\$UserID"
Endif
Else
$vRC = DelKey("$sRegKey\$sKeyValue")
If $vRC = 0
? " .. Not in domain, deleting SID: $sKeyValue"
Endif
EndIf
EndIf
Else
? " ..User ID $sUserID, is a Local ID and is being by passed"
EndIf
EndIf
EndIf
EndIf
$vIndex = $vIndex + 1
goto Loop1
EndIf
Function WshPipe($ShellCMD, OPTIONAL $NoEcho)
Dim $oExec, $Output
$oExec = CreateObject("WScript.Shell").Exec($ShellCMD)
If Not VarType($oExec) = 9 $WshPipe="WScript.Shell Exec Unsupported" Exit 10 EndIf
$Output = $oExec.StdOut.ReadAll + $oExec.StdErr.ReadAll
If Not $NoEcho $Output EndIf
$WshPipe = Split(Join(Split($Output,CHR(13)),''),CHR(10))
Exit($oExec.ExitCode)
EndFunction