sidewinder Posted September 16, 2010 Posted September 16, 2010 Always used to use the following as a startup script on XP: Dim wsh Set wsh = WScript.CreateObject("WScript.Shell") wsh.Run "%SystemRoot%\regedit.exe /S regkeytoimport.reg", , True But now it does nothing. I cant even run it when logged on as myself, I get an error saying that the system cannot find the file specified on line 3. But it does start regedit first, so its something do with the actual import I guess? I've tried with different scripts, and the .reg file is in the same folder. What am I doing wrong or does 7 just not support this method? Ive tried using the 'new' bit of group policy to do this, but although it lets you import/replace keys, you have to type the value in, and what we are trying to import has quite a few subkeys
srochford Posted September 16, 2010 Posted September 16, 2010 Where is the reg file to import? regedit needs to run in elevated mode and may not be able to access a network share. Can you do it with one or more reg add commands?
sidewinder Posted September 17, 2010 Author Posted September 17, 2010 Its in the same folder - I've got both the reg file and the script in the same GPO folder. Even when both are on my C drive it doesnt work. Not sure reg add will work - it looks like you have to specify the whole key contents with the command, but what I'm trying to add has a few subkeys so I would assume needs to stay as a reg file. The reason I'm trying to do it is we're getting a problem where students are often given the message "The user profile failed the logon: user profile cannot be loaded" If I manually delete all the previous network logons in HKLM/Software/Microsoft/Windows NT/CurrentVersion/Profilelist students can log on again, but as a manual process it takes forever as we're experiencing the problem all over the school. So I was hoping to find a way to replace that key with the default values automatically.
NorthernNoel Posted September 17, 2010 Posted September 17, 2010 I know it may be a bit of a pain but could you run it them on a machine, then in GPO click on '...' at the end of 'Key Path'? You'd still have to do each one but at least you won't have to type them in.
srochford Posted September 17, 2010 Posted September 17, 2010 OK; this might be a bit of over kill but this is how I clean up "dead" profiles. First line has a list of users where you don't want the profile to be cleared, all others are removed from disc and from the list in the registry. It also cleans out the profile list in the registry (so you don't get the problem you're seeing) If you set bdebug=true at the start then it writes a log to the "slog" folder. 'don't delete profiles for these local users sUsersToKeep=array("exam09","administrator","LocalService","NetworkService","All Users","Default User") bDebug=false 'set true to write to log file Const HKEY_LOCAL_MACHINE = &H80000002 dim sLog sLog="c:\windows\profileclean.log" set oUserNameDic=createobject("scripting.dictionary") set oUserSIDDic=createobject("scripting.dictionary") set oShell=createobject("wscript.shell") set oFSO=createobject("scripting.filesystemobject") Set oRegistry=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv") set oNetwork=createobject("wscript.network") sComputer=oNetwork.computername debugout "Starting" BuildDictionaries ClearProfiles debugout "Ending" sub BuildDictionaries on error resume next for each sUser in sUsersToKeep sSid=GetSid(sComputer,sUser) if not oUserSIDDic.exists(sSID) then oUserSIDDic.add sSid, sUser end if oUserNameDic.add lcase(sUser),sSid debugout "Adding " & sUser next 'now add details for user running this script; don't want to delete profile for logged on user! sUser=lcase(oNetwork.username) sDomain=oShell.ExpandEnvironmentStrings("%userdomain%") sSid=GetSid(sDomain,sUser) debugout "Process running as " & sDomain & "\" & sUser & "(SID: " & sSID & ")" if not oUserSIDDic.exists(sSid) then oUserSIDDic.add sSid, sUser end if if not oUserNameDic.exists(sUser) then oUserNameDic.add lcase(sUser),sSid debugout "Adding " & sUser end if on error goto 0 end sub Function GetSid(sDomain,sUser) 'sDomain could be domain or computer name but will only query local computer '. below means query local computer for info Set oWMIService = GetObject("winmgmts:\\.\root\cimv2") on error resume next Set oAccount = oWMIService.Get _ ("Win32_UserAccount.Name='" & sUser & "',Domain='" & sDomain & "'") if err.number=0 then sSid=oAccount.SID else sSid=0 end if GetSid=sSid on error goto 0 end function sub ClearProfiles on error resume next sPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileGuid" lRC = oRegistry.EnumKey(HKEY_LOCAL_MACHINE, sPath, sKeys) If (lRC = 0) And (Err.Number = 0) and not(isnull(sKeys)) Then ' no errors and there is something to look at For i=lbound(sKeys) to ubound(sKeys) lRc=oRegistry.GetStringValue(HKEY_LOCAL_MACHINE,sPath &"\"& sKeys(i),"SidString",sValue) if CheckSid(sValue) or len(sValue)<40 then 'profile is in list to keep or is well known profile so do nothing else debugout "deleting reg key " & sPath & "\" & sKeys(i) deletesubkeys HKEY_LOCAL_MACHINE,sPath & "\" & sKeys(i) end if next end if sPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" lRC = oRegistry.EnumKey(HKEY_LOCAL_MACHINE, sPath, sKeys) If (lRC = 0) And (Err.Number = 0) and not(isnull(sKeys)) Then For i=lbound(sKeys) to ubound(sKeys) if CheckSid(sKeys(i)) then 'profile is in list to keep or is well known profile so do nothing else lRc=oRegistry.GetStringValue(HKEY_LOCAL_MACHINE,sPath &"\"& sKeys(i),"ProfileImagePath",sValue) on error resume next if ofso.folderexists(sValue) then if lcase(sValue)<> lcase(oShell.ExpandEnvironmentStrings("%USERPROFILE%")) then 'it's not "my" profile folder debugout "deleting folder " & sValue err.clear ofso.deletefolder sValue,true if err.number<>0 then debugout "Error " & err.number & " on delete" end if end if end if on error goto 0 deletesubkeys HKEY_LOCAL_MACHINE,sPath & "\" & sKeys(i) end if next end if root="c:\documents and settings" Set folder=oFSO.GetFolder(root) Set flist=folder.subfolders For Each oFolder In flist sFolderName=lcase(oFolder.name) iDotPos=instr(sFolderName,".") 'check to see if name is username.ic etc if iDotPos<>0 then sName=left(sFolderName,iDotPos-1) else sName=sFolderName end if If not CheckName(sName) then if lcase(root & "\" & sFoldername) <> lcase(oShell.ExpandEnvironmentStrings("%USERPROFILE%")) then 'it's not "my" profile folder debugout "Deleting folder " & sFolderName deletefolder root & "\" & sFoldername end if End If Next on error goto 0 end sub Sub DeleteSubkeys(iRoot, sKeyPath) on error resume next oRegistry.EnumKey iRoot, sKeyPath, arrSubkeys If IsArray(arrSubkeys) Then For Each sSubkey In arrSubkeys DeleteSubkeys iRoot, sKeyPath & "\" & sSubkey Next End If oRegistry.DeleteKey iRoot, sKeyPath on error goto 0 End Sub function CheckSid(sSID) on error resume next 'is this SID in the dictionary to be preserved or is it a "well known" SID? if oUserSIDDic.exists(sSID) or len(sSID)<40 then CheckSID=true else CheckSID=false end if on error goto 0 end function function CheckName(sName) on error resume next 'is this username in the dictionary to be preserved if oUserNameDic.exists(lcase(sName)) then CheckName=true else CheckName=false end if end function sub debugout(sItem) on error resume next if bdebug then if not ofso.fileexists(sLog) then set oFile=ofso.createtextfile(sLog) ofile.writeline now & ": Starting" ofile.close end if set oFile=ofso.opentextfile(sLog,8) ofile.writeline now & ": " & sItem oFile.close end if on error goto 0 end sub Sub DeleteFolder(sRoot) on error resume next Set oFolder=oFso.getfolder(sRoot) For Each folder In oFolder.subfolders ClearContents(folder) Next ClearContents(oFolder) ofso.deletefolder sRoot on error goto 0 end sub Sub ClearContents(sFolder) on error resume next for each f1 in sFolder.files ofso.deletefile f1,true next For Each f2 In sFolder.subfolders ClearContents(f2) ofso.deletefolder f2,true Next on error goto 0 End Sub 1
Stuart_C Posted September 17, 2010 Posted September 17, 2010 There is a GP setting which you can use to delete local profiles older than x days. Needs vista or above and is in ComputerCOnfig/System/User Profiles
NorthernNoel Posted September 17, 2010 Posted September 17, 2010 (edited) Stuart; Trouble with that GP setting is that it needs at least a day before it deletes. By then it could have copied the corrupt profile back. Steve; With that script I get an error on line 56 char 3: Looking at the code it's this section: Function GetSid(sDomain,sUser) 'sDomain could be domain or computer name but will only query local computer '. below means query local computer for info Set oWMIService = GetObject("winmgmts:\\.\root\cimv2") 'on error resume next Set oAccount = oWMIService.Get _ ("Win32_UserAccount.Name='" & sUser & "',Domain='" & sDomain & "'") if err.number=0 then sSid=oAccount.SID else sSid=0 end if GetSid=sSid on error goto 0 end function The error is on Set oAccount = oWMIService.Get _ Any ideas? Edit: Error is: Not Found Source is: SWbemServicesEx Edited September 17, 2010 by NorthernNoel
srochford Posted September 17, 2010 Posted September 17, 2010 Try this: Set oAccount = oWMIService.Get("Win32_UserAccount.Name='" & sUser & "',Domain='" & sDomain & "'") The _ should work as a line continuation character (and I copied and pasted from a working script, honest!) but is obviously not working ...
sidewinder Posted September 20, 2010 Author Posted September 20, 2010 Thanks Steve, that script seems to work perfectly!
NorthernNoel Posted September 20, 2010 Posted September 20, 2010 Hi Steve, afraid it's still not working for me. I'd already tried what you suggested above. Feel as though i'm missing something obvious but here's a screenshot of what I get when I run it. http://i806.photobucket.com/albums/yy343/NorthernNoel/scripterror.jpg
srochford Posted September 20, 2010 Posted September 20, 2010 I'm guessing line 53 is the "set oAccount" line If so, do you have on error resume next as the previous line (and not commented out - ie not starting with a ' mark)? Some accounts won't be found in the domain (they're local) so it's not possible to get a SID for them - this is a kludgy way of skipping over them (ie the error will happen but vbscript carries on running because you've said "carry on" if there's an error) and the error handler then says "if error code is not zero then I know the SID wasn't found so just use 0 as a dummy"
NorthernNoel Posted September 20, 2010 Posted September 20, 2010 Thanks Steve, looking back I had commented them out to see if I was missing something with an original error. With them back in I get an error for Permission denied when it tries to run this line: For Each oFolder In flist
srochford Posted September 20, 2010 Posted September 20, 2010 Are you running this as a machine startup script?
NorthernNoel Posted September 20, 2010 Posted September 20, 2010 OK i'm getting closer. It's deleting the reg keys fine. When trying to delete the users folder in 'C:\Users' the log reports 'Error 70 on delete'. Also I've added users not to delete to sUsersToKeep=array but it is still deleting them. Thanks for all your help on this.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now