From: <D> on
I'm new to scripting, and trying to figure out a way to automate a task.
Background info - school district, 1500+ computers most of which have winxp.
Occasionally, I need to modify the user registry to accomodate for software
settings, etc. Typically, what I've had to do is load the default user
registry, import the reg file, then unload the reg hive. If there are user
accounts locally on the machine, I have to do this for those as well.
That's what I'd like to automate with a script. Ideally, this is what I'd
like it to do...

detect user folders in the documents and settings folder
cd into that folder
load the reg hive for that user
import the reg file
unload the reg hive
cd ..
cd into next folder and repeat

I know how to load the reg hive and import the file and then unload the
hive, but I'm having trouble with the folder stuff. I tried using a cmd
file like this...

for /f %%g in ('dir /b') do {
cd %%g
if not exist ntuser.dat goto nofile
reg load hklm\1 ntuser.dat
reg import myregfile.reg
reg unload hklm\1
:nofile
cd..
}
exit

however, I get errors that '{' is not a command and then everything fails
from there.

Any ideas you could push my way would be greatly appreciated.

Thanks,

Doug


From: Pegasus (MVP) on

<D> wrote in message news:u1JXqgWIIHA.4228(a)TK2MSFTNGP02.phx.gbl...
> I'm new to scripting, and trying to figure out a way to automate a task.
> Background info - school district, 1500+ computers most of which have
> winxp. Occasionally, I need to modify the user registry to accomodate for
> software settings, etc. Typically, what I've had to do is load the
> default user registry, import the reg file, then unload the reg hive. If
> there are user accounts locally on the machine, I have to do this for
> those as well. That's what I'd like to automate with a script. Ideally,
> this is what I'd like it to do...
>
> detect user folders in the documents and settings folder
> cd into that folder
> load the reg hive for that user
> import the reg file
> unload the reg hive
> cd ..
> cd into next folder and repeat
>
> I know how to load the reg hive and import the file and then unload the
> hive, but I'm having trouble with the folder stuff. I tried using a cmd
> file like this...
>
> for /f %%g in ('dir /b') do {
> cd %%g
> if not exist ntuser.dat goto nofile
> reg load hklm\1 ntuser.dat
> reg import myregfile.reg
> reg unload hklm\1
> :nofile
> cd..
> }
> exit
>
> however, I get errors that '{' is not a command and then everything fails
> from there.
>
> Any ideas you could push my way would be greatly appreciated.
>
> Thanks,
>
> Doug
>

Try this:
for /f %%g in ('dir /ad /b') do (
cd "%UserProfiles%\%%g"
if exist ntuser.dat (
reg load hklm\1 ntuser.dat
reg import myregfile.reg
reg unload hklm\1
)
)
Remarks:
- Using %UserProfiles% makes your batch file more robust.
- Enclosing %%g in double quotes allows for names
with embedded spaces (which might happen later on).
- You must use ellipses, not curly brackets.


From: T_Condit on
One of the problems that I have run into doing this is if someone is logged
in, plus you don't need to set info for certain users like: LocalService,
NetworkService, Public, All Users, Default User, and any other specific admin
accounts you may use in your environment . This script will also attempt to
find out if someone is logged in. It has some different samples and such
also. Obviously watch wrapping.

Option Explicit
const HKEY_CLASSES_ROOT = &H80000000
const HKEY_CURRENT_USER = &H80000001
const HKEY_LOCAL_MACHINE = &H80000002
const HKEY_USERS = &H80000003

Dim objWshShell, objFSO, objNetwork, objWMI, WshEnv
Dim SystemDrive, ProgramFiles, WinDir
Dim strAllUsersDesktopPath, strAllUsersProgramsPath,
strUserProfilesMainFolder, strCurrentUserDesktop, strCurrentUserStartMenu
Dim objExplorer
Dim strScriptFileDirectory, strConsoleUserName

Set objWshShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = Wscript.CreateObject("WScript.Network")
Set objWMI = GetObject("winmgmts:" &
"{impersonationLevel=impersonate}!\\.\root\cimv2")
Set WshEnv = objWshShell.Environment("PROCESS")


' Sets script variables and objects.
SetScriptItems

' Obtains the name of the logged in console user.
strConsoleUserName = GetConsoleUser

' Configures the registry settings.
SetRegistry(strConsoleUserName)

Wscript.Quit



' ~$~----------------------------------------~$~
' FUNCTIONS & SUBROUTINES
' ~$~----------------------------------------~$~
Sub SetScriptItems
' Attempts to set the variables and objects that might be needed by the
script (not all variables may be used by the script).
SystemDrive = objWshShell.ExpandEnvironmentStrings("%SystemDrive%")
ProgramFiles = objWshShell.ExpandEnvironmentStrings("%ProgramFiles%")
WinDir = objWshShell.ExpandEnvironmentStrings("%windir%")

' Attempts to set the needed directory paths.
strScriptFileDirectory = objFSO.GetParentFolderName(wscript.ScriptFullName)

' Attempts to obtain the Desktop and Start Menu paths for all users.
strAllUsersProgramsPath =
objWshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Common Programs")
strAllUsersDesktopPath =
objWshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Common Desktop")

' Attempts to configure Windows XP paths.
strUserProfilesMainFolder =
Mid(strAllUsersDesktopPath,1,InStr(strAllUsersDesktopPath, "\All Users"))
strCurrentUserDesktop = strUserProfilesMainFolder & objNetwork.UserName &
"\Desktop"
strCurrentUserStartMenu = strUserProfilesMainFolder & objNetwork.UserName &
"\Start Menu"

If strUserProfilesMainFolder = "" Then
' Attempts to configure Windows Vista paths.
strUserProfilesMainFolder =
Mid(strAllUsersDesktopPath,1,InStr(strAllUsersDesktopPath, "\Public"))
strCurrentUserDesktop = strUserProfilesMainFolder & objNetwork.UserName &
"\Desktop"
strCurrentUserStartMenu = strUserProfilesMainFolder & objNetwork.UserName &
"\AppData\Roaming\Microsoft\Windows\Start Menu"
End If
End Sub

' ~$~----------------------------------------~$~
Function GetConsoleUser
' Attempts to obtain the name of the logged in Windows console user by using
QWINSTA.EXE.
Dim strTempFile, objTempFile

strTempFile = objFSO.GetSpecialFolder(2).ShortPath & "\" & objFSO.GetTempName
GetConsoleUser = ""

' Uses "for /f" to extract the user name, dumping the results into a
temporary text file.
objWshShell.Run "%ComSpec% /c for /f ""skip=1 Tokens=2"" %i in " &
"('%SystemRoot%\System32\QWINSTA.EXE console /SERVER:" &
objNetwork.ComputerName & "') do echo %i >" & strTempFile, 0, True

If objFSO.FileExists(strTempFile) Then
' Opens and parses the temporary text file.
Set objTempFile = objFSO.OpenTextFile(strTempFile)
Do While Not objTempFile.AtEndOfStream
GetConsoleUser = Trim(objTempFile.ReadLine)
Loop
objTempFile.Close
objFSO.DeleteFile strTempFile
End If
End Function

' ~$~----------------------------------------~$~
Sub SetRegistry(strConsoleUserName)
' Loads the individual user profile hives and configures needed USER
registry settings.
Dim strUserSID, Subfolder, UserPaths, strRegPath, strRegistryHive,
strRegistryKey
Dim colItems, objItem, objUserAccount
Dim strLocalSettingsFolder

On Error Resume Next

Set UserPaths = objFSO.GetFolder(strUserProfilesMainFolder).Subfolders
strUserSID = ""

' Attempts to obtain the SID of the logged in console user by checking
against the local user accounts.
Set colItems = objWMI.ExecQuery ("Select * from Win32_UserAccount Where
LocalAccount = True")
For Each objItem in colItems
If objItem.Name = strConsoleUserName Then
strUserSID = objItem.SID
End If
Next
If strUserSID = "" Then
' Attempts to obtain the SID of the logged in console user by checking
against the domain user accounts.
Set objUserAccount =
GetObject("winmgmts://./root/cimv2").Get("Win32_UserAccount.Domain='" &
objNetwork.UserDomain & "'" & ",Name='" & strConsoleUserName & "'")
strUserSID = objUserAccount.SID
End If

For Each Subfolder in UserPaths
strRegPath="USERS\CustomizeRegistry"
strRegistryHive = &H80000001
If Not Subfolder = (strUserProfilesMainFolder & "LocalService") Then
If Not Subfolder = (strUserProfilesMainFolder & "NetworkService") Then
If Not Subfolder = (strUserProfilesMainFolder & "Public") Then
If Not Subfolder = (strUserProfilesMainFolder & "All Users") Then

If objFSO.FolderExists (Subfolder & "\Local Settings") Then
strLocalSettingsFolder = Subfolder & "\Local Settings\Application Data"
Else
strLocalSettingsFolder = Subfolder & "\AppData\Local"
End If

' SAMPLE: Demonstrates how to create a new folder in every user's local
profile folder.
' If Not objFSO.FolderExists (strLocalSettingsFolder & "\NewFolderName")
Then
' objFSO.CreateFolder (strLocalSettingsFolder & "\NewFolderName")
' End If

' SAMPLE: Demonstrates how to copy a file into every user's local
profile folder.
' If objFSO.FileExists ("FileToBeCopied.txt") Then
' objFSO.CopyFile ("FileToBeCopied.txt"), (strLocalSettingsFolder &
"\Folder1\Folder2\NewNameOfTheFileToBeCopied.txt"), True
' End If


' If Subfolder = (strUserProfilesMainFolder & strConsoleUserName) Then
' If strUserSID = "" Then
' strRegPath="CURRENT_USER"
' Else
' strRegPath="USERS\" & strUserSID
' End If
' End If

If strRegPath = "USERS\CustomizeRegistry" Then
strRegistryHive = &H80000003
' Loads the user profile's registry hive.
objWshShell.Run ("reg.exe load HKEY_USERS\CustomizeRegistry """ &
Subfolder & """\NTuser.dat"), 0, True
End If

'SAMPLE: Demonstrates how to make a registry change in every user's
registry hive.
' objWshShell.RegWrite "HKEY_" & strRegPath & "\Software\Adobe\Acrobat
Reader\8.0\Updater\iUpdateFrequency", "0","REG_DWORD"
' objWshShell.RegWrite "HKEY_" & strRegPath & "\Software\Adobe\Acrobat
Reader\8.0\AdobeViewer\EULA", "1","REG_DWORD"

'SAMPLE: Demonstrates how to make a registry change in every user's
registry hive using a .REG file.
If objFSO.FileExists ("CustomizeRegistry.reg") Then
objWshShell.Run ("reg.exe HKLM_Med.reg"), 0, True
objWshShell.Run ("reg.exe HKCU_Med.reg"), 0, True
End If

' SAMPLE: Demonstrates how to arrange the Start Menu in alphabetical
order (and shows how to delete a registry key in every user's registry hive).
' strRegistryKey="Software\Microsoft\Windows\CurrentVersion\Explorer\MenuOrder\Start Menu2"
' If strRegPath = "USERS\CustomizeRegistry" Then
' strRegistryKey="CustomizeRegistry\" & strRegistryKey
' End If
DeleteRegistryKey strRegistryHive, strRegistryKey


If strRegPath = "USERS\CustomizeRegistry" Then
' Unloads the user profile registry hive.
objWshShell.Run ("reg.exe unload HKEY_USERS\CustomizeRegistry"), 0, True
End If
End If
End If
End If
End If
Next
End Sub

' ~$~----------------------------------------~$~
Sub DeleteRegistryKey(RegRoot, strPath)
' Attempts to delete all subkeys and values before deleting the parent
registry key.
Dim strRegistryKeys, SubKeyCount, objRegistry, lRC, lRC2, strKey

On Error Resume Next

Set objRegistry = GetObject("winmgmts:root\default:StdRegProv")
lRC = objRegistry.EnumKey(RegRoot, strPath, strRegistryKeys)

If IsArray(strRegistryKeys) Then
For each strKey in strRegistryKeys
DeleteRegistryKey RegRoot, strPath & "\" & strKey
Next
End If

lRC2 = objRegistry.DeleteKey(RegRoot, strPath)
End Sub



"D" wrote:

> I'm new to scripting, and trying to figure out a way to automate a task.
> Background info - school district, 1500+ computers most of which have winxp.
> Occasionally, I need to modify the user registry to accomodate for software
> settings, etc. Typically, what I've had to do is load the default user
> registry, import the reg file, then unload the reg hive. If there are user
> accounts locally on the machine, I have to do this for those as well.
> That's what I'd like to automate with a script. Ideally, this is what I'd
> like it to do...
>
> detect user folders in the documents and settings folder
> cd into that folder
> load the reg hive for that user
> import the reg file
> unload the reg hive
> cd ..
> cd into next folder and repeat
>
> I know how to load the reg hive and import the file and then unload the
> hive, but I'm having trouble with the folder stuff. I tried using a cmd
> file like this...
>
> for /f %%g in ('dir /b') do {
> cd %%g
> if not exist ntuser.dat goto nofile
> reg load hklm\1 ntuser.dat
> reg import myregfile.reg
> reg unload hklm\1
> :nofile
> cd..
> }
> exit
>
> however, I get errors that '{' is not a command and then everything fails
> from there.
>
> Any ideas you could push my way would be greatly appreciated.
>
> Thanks,
>
> Doug
>
>
>
 | 
Pages: 1
Prev: event supervise
Next: Conditional NumberFormat