+ Post New Thread
Results 1 to 14 of 14
Windows 7 Thread, Importing a registry key on startup? in Technical; Always used to use the following as a startup script on XP: Code: Dim wsh Set wsh = WScript.CreateObject("WScript.Shell") wsh.Run ...
  1. #1

    Join Date
    Jul 2006
    Location
    London
    Posts
    2,962
    Thank Post
    159
    Thanked 152 Times in 116 Posts
    Rep Power
    49

    Importing a registry key on startup?

    Always used to use the following as a startup script on XP:

    Code:
    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

  2. #2

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,154
    Thank Post
    114
    Thanked 527 Times in 450 Posts
    Blog Entries
    2
    Rep Power
    123
    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?

  3. #3

    Join Date
    Jul 2006
    Location
    London
    Posts
    2,962
    Thank Post
    159
    Thanked 152 Times in 116 Posts
    Rep Power
    49
    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.

  4. #4
    NorthernNoel's Avatar
    Join Date
    Sep 2010
    Posts
    16
    Thank Post
    0
    Thanked 2 Times in 2 Posts
    Rep Power
    8
    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.

  5. #5

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,154
    Thank Post
    114
    Thanked 527 Times in 450 Posts
    Blog Entries
    2
    Rep Power
    123
    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.
    Code:
    '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

  6. Thanks to srochford from:

    sidewinder (20th September 2010)

  7. #6

    Join Date
    Nov 2007
    Location
    Rotherham
    Posts
    1,675
    Thank Post
    122
    Thanked 126 Times in 102 Posts
    Rep Power
    45
    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

  8. #7
    NorthernNoel's Avatar
    Join Date
    Sep 2010
    Posts
    16
    Thank Post
    0
    Thanked 2 Times in 2 Posts
    Rep Power
    8
    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:

    Code:
    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
    Code:
    Set oAccount = oWMIService.Get _
    Any ideas?

    Edit: Error is: Not Found Source is: SWbemServicesEx
    Last edited by NorthernNoel; 17th September 2010 at 12:15 PM.

  9. #8

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,154
    Thank Post
    114
    Thanked 527 Times in 450 Posts
    Blog Entries
    2
    Rep Power
    123
    Try this:
    Code:
     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 ...

  10. #9

    Join Date
    Jul 2006
    Location
    London
    Posts
    2,962
    Thank Post
    159
    Thanked 152 Times in 116 Posts
    Rep Power
    49
    Thanks Steve, that script seems to work perfectly!

  11. #10
    NorthernNoel's Avatar
    Join Date
    Sep 2010
    Posts
    16
    Thank Post
    0
    Thanked 2 Times in 2 Posts
    Rep Power
    8
    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.

  12. #11

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,154
    Thank Post
    114
    Thanked 527 Times in 450 Posts
    Blog Entries
    2
    Rep Power
    123
    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"

  13. #12
    NorthernNoel's Avatar
    Join Date
    Sep 2010
    Posts
    16
    Thank Post
    0
    Thanked 2 Times in 2 Posts
    Rep Power
    8
    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:
    Code:
    For Each oFolder In flist

  14. #13

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,154
    Thank Post
    114
    Thanked 527 Times in 450 Posts
    Blog Entries
    2
    Rep Power
    123
    Are you running this as a machine startup script?

  15. #14
    NorthernNoel's Avatar
    Join Date
    Sep 2010
    Posts
    16
    Thank Post
    0
    Thanked 2 Times in 2 Posts
    Rep Power
    8
    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.

SHARE:
+ Post New Thread

Similar Threads

  1. iTalc and importing public key problem
    By beaagledave in forum Network and Classroom Management
    Replies: 4
    Last Post: 2nd October 2009, 07:51 PM
  2. Registry key deployment using GP
    By Mauri in forum Windows
    Replies: 11
    Last Post: 5th November 2008, 09:18 AM
  3. Changing permissions on a registry key
    By Wheelgunr in forum Windows
    Replies: 6
    Last Post: 18th September 2008, 02:14 PM
  4. Deleted a registry key - help!
    By mrcrazy04 in forum Windows
    Replies: 1
    Last Post: 19th July 2007, 07:26 AM
  5. Script To Change A Registry Key
    By DaveP in forum Windows
    Replies: 6
    Last Post: 7th March 2007, 12:57 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •