locked
VBScript Update Multiple Reg Keys RRS feed

  • Question

  • Hi

    I am looking to develop a vbscript that first check if the registry keys exist, and second if the value exist ignore to do any updates. But, if the values do not exist to perform an update.

    Const HKEY_CURRENT_USER = &H80000001

    strComputer = "."
    Set objRegistry = GetObject("winmgmts:\\" & _
        strComputer & "\root\default:StdRegProv")
    Set Shell = CreateObject( "WScript.Shell" )

    strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Desktop"
    strValueName = "\\servername\sharename\" & UserName & "\Desktop"
    strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Favorites"
    strValueName = "\\servername\sharename\" & UserName & "\Favorites"
    strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\AppData"
    strValueName = "\\servername\sharename\" & UserName & "\AppData"
    strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Local AppData"
    strValueName = "\\servername\sharename\" & UserName & "\Local AppData"
    strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\My Music"
    strValueName = "\\servername\sharename\" & UserName & "\My Documents\My Music"
    strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\My Pictures"
    strValueName = "\\servername\sharename\" & UserName & "\My Documents\My Pictures"
    strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\My Videos"
    strValueName = "\\servername\sharename\" & UserName & "\My Documents\My Videos"
    strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\My Documents"
    strValueName = "\\servername\sharename\" & UserName & "\My Documents\My Documents"
    strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Personal"
    strValueName = "\\servername\sharename\" & UserName & "\My Documents\My Documents"
    objRegistry.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue

    If IsNull(strValue) Then
    'Shell Folders
        Shell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Desktop", "\\servername\sharename" & UserName & "\Desktop", "REG_SZ"
        Shell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Favorites", "\\servername\sharename" & UserName & "\Favorites", "REG_SZ"
        Shell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\AppData", "%USERPROFILE%\AppData\Roaming", "REG_SZ"
        Shell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Downloads", "%USERPROFILE%\Downloads", "REG_SZ"
        Shell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Local AppData", "%USERPROFILE%\AppData\Local", "REG_SZ"
        Shell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\My Music", "\\servername\sharename\My Documents" & UserName & "\My Music", "REG_SZ"
        Shell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\My Pictures", "\\servername\sharename\My Documents" & UserName & "\My Pictures", "REG_SZ"
        Shell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\My Video", "\\servername\sharename\My Documents" & UserName & "\My Videos", "REG_SZ"
        Shell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Personal", "\\servername\sharename\" & UserName & "\Documents", "REG_SZ"
        Shell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\My Documents", "\\servername\sharename" & UserName & "\My Documents", "REG_SZ"
    'User SHell Folders
        Shell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Desktop", "\\servername\sharename" & UserName & "\Desktop", "REG_EXPAND_SZ"
        Shell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Favorites", "\\servername\sharename" & UserName & "\Favorites", "REG_EXPAND_SZ"
        Shell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\AppData", "%USERPROFILE%\AppData\Roaming", "REG_EXPAND_SZ"
        Shell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Downloads", "%USERPROFILE%\Downloads", "REG_EXPAND_SZ"
        Shell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Local AppData", "%USERPROFILE%\AppData\Local", "REG_EXPAND_SZ"
        Shell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\My Music", "\\servername\sharename\My Documents" & UserName & "\My Music", "REG_EXPAND_SZ"
        Shell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\My Pictures", "\\servername\sharename\My Documents" & UserName & "\My Pictures", "REG_EXPAND_SZ"
        Shell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\My Video", "\\servername\sharename\My Documents" & UserName & "\My Videos", "REG_EXPAND_SZ"
        Shell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Personal", "\\servername\sharename" & UserName & "\My Documents", "REG_EXPAND_SZ"
        Shell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\My Documents", "\\servername\sharename" & UserName & "\My Documents", "REG_EXPAND_SZ"
    Else
        Wscript.Echo "The registry key exists."
    End If


    Saturday, August 2, 2014 1:26 PM

Answers

  • You are wrong about Group Policy.  Files can be left where they are when GP is removed,  Just check the option.

    It is just as big of a problem to edit the registry.  It will create other issues particularly across different OS platforms.

    You cannot remotely edit a users registry under most circumstanc3es and Woindows will likely reset the settings in a  domain

    Most of us use Group POlciy and have used it for years to manage user special folders.  We can do alomost anything we need but it does require correct training in WIndows Administration and Group Policy.

    You need to spend some time reviewing the way Windows works in a Domain and how to use Group Policy.  It is clear from your issues that you are not doing this correctly.

    The code I posted does what you ask.  You will still have to understand how the users registry hive works.  The code you posted shows you do not have much training or experience in the registry.  I recommend being carefuyl and doing some research to understand how this and VBScript work. You can also look at many registry examples that are in the Gallery.

    Here are some more resources: http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx

    I recommend not using VBScript since you are not experienced with it. PowerSHell can do more and is much easier to use and learn.


    ¯\_(ツ)_/¯

    • Marked as answer by PedroRobles Saturday, August 2, 2014 6:25 PM
    Saturday, August 2, 2014 6:09 PM

All replies

  • What is your actual question?
    Saturday, August 2, 2014 1:32 PM
  • Hi

    I am looking to develop a vbscript that first check if the registry keys exist, and second if the value exist ignore to do any updates. But, if the values do not exist to perform an update.


    You have to do a RegRead on each key in a loop.

    Set Shell = CreateObject( "WScript.Shell" )
    
    strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Desktop"
    strValueName = "\\servername\sharename\" & UserName & "\Desktop"
    
    TextUpdateRegValue strKeyPath, strValueName
    
    Function TextUpdateRegValue( key, value )
    
        If Not Shell.RegRead( key & value) Then
            Shell.RegWrite key, value, "REG_SZ"
        End If
    
    End Function

    Some issue you may  have:

    1.  You cannot use the Shell obj3ect remotely.
    2.  The values you are trying to set are set by the operating system and should not be changed.
    3.  The values cannot be set remotely by most normal means.
    4.  Group Policy can be used to move the users folders to the correct locations.
    5.  The location of these folders cannot be the desktop as it will create storage and logon issues or may just create conflict.

    TO correctly migrate folders to the network yu should use Group Policy.  That is what it is designed for among other things


    ¯\_(ツ)_/¯


    • Edited by jrv Saturday, August 2, 2014 2:48 PM
    Saturday, August 2, 2014 2:47 PM
  • Hi

    Thank you for the example. As far as using group policy, we have used them in the past; but, it also created issues when using group policies. For example, in the group policy says that if at some point the policy needs to be removed, the files and folders redirected will return to the original location. In this case, instead of \\servername\sharename back to c:\users\%username%. Well, it does not work that easy and we have learned it the hard way.

    We have close to 200 computers that after turning off the folder redirection, in the GPO, inside the registry key, the user folders are still pointing to an old server.

    This is the reason that we are going with vbscripting and other scripts to move files to any location at anytime whenever we want it to happen.

    In my example shown above, I am trying to ensure the vbscript checks for the registry keys and if not to make the adjustments as necesary.

    Saturday, August 2, 2014 5:06 PM
  • You are wrong about Group Policy.  Files can be left where they are when GP is removed,  Just check the option.

    It is just as big of a problem to edit the registry.  It will create other issues particularly across different OS platforms.

    You cannot remotely edit a users registry under most circumstanc3es and Woindows will likely reset the settings in a  domain

    Most of us use Group POlciy and have used it for years to manage user special folders.  We can do alomost anything we need but it does require correct training in WIndows Administration and Group Policy.

    You need to spend some time reviewing the way Windows works in a Domain and how to use Group Policy.  It is clear from your issues that you are not doing this correctly.

    The code I posted does what you ask.  You will still have to understand how the users registry hive works.  The code you posted shows you do not have much training or experience in the registry.  I recommend being carefuyl and doing some research to understand how this and VBScript work. You can also look at many registry examples that are in the Gallery.

    Here are some more resources: http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx

    I recommend not using VBScript since you are not experienced with it. PowerSHell can do more and is much easier to use and learn.


    ¯\_(ツ)_/¯

    • Marked as answer by PedroRobles Saturday, August 2, 2014 6:25 PM
    Saturday, August 2, 2014 6:09 PM
  • Thank you for your concern, and for the assistance provided. Yet, it is true, the GPO "should work". But, it does not work all the times.

    Again, thanks all for the kind assistances.

    Saturday, August 2, 2014 6:25 PM
  • Thank you for your concern, and for the assistance provided. Yet, it is true, the GPO "should work". But, it does not work all the times.

    Again, thanks all for the kind assistances.

    It works al of the time if it is set up correctly.  Consider how many enterprises rely on this and have since Windows 2000.  In every situation where an Admin claimed it didn';t work we were able to show that the GP for redirection was not being deployed correctly.

    I have heard this same claim for almost 10 years and have never found it to be true.  


    ¯\_(ツ)_/¯

    Saturday, August 2, 2014 7:54 PM