writing multiple values to Reg_SZ - windows

I have a application that needs multiple configuration files to be written to reg_sz as
"sEndorsement"="C:\\x\\file1.txt
C:\\x\\file2.txt"
Adding this via regedit doesn't works. i tried using a vbscript to do so as below,
Dim myval
myval = "C:\\x\\file1.txt" & VbCrLf _
& "C:\\x\\file2.txt" & VbCrLf _
& "C:\\y\\file3.dll" & VbCrLf
Dim WSHShell
set WSHShell = CreateObject("WScript.Shell")
WSHShell.RegWrite "HKEY_USERS\abc\def\TheSelectedFiles", myval, "REG_SZ"
But it still ended up in coming as single line.. i am not sure.. Is there someone can help me with it?? Thanks

For multiple lines, that's called REG_MULTI_SZ not REG_SZ.
You cannot create keys under the root of HKEY_USERS, you need to use the .Default subkey. The values will appear to be on the same line, but if you double click the value, you can see there are 3 lines.
Set objRegistry = GetObject("winmgmts:{impersonationLevel=impersonate,authenticationLevel=pktPrivacy}!\\.\root\default:StdRegProv")
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const HKEY_CURRENT_CONFIG = &H80000005
strKeyPath = ".DEFAULT\abc\def\TheSelectedFiles"
MultValueName = "sEndorsement"
iValues = Array("C:\x\file1.txt", "C:\x\file2.txt", "C:\y\file3.dll")
objRegistry.CreateKey HKEY_USERS,strKeyPath
objRegistry.SetMultiStringValue HKEY_USERS,strKeyPath,MultValueName,iValues

Related

Creating a Registry text file with vbs

I am trying to create a registry backup that I can read using VBS. I found a script online that seems to do the trick however, it only goes one file of the Hive deep.
This code:
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const HKEY_CURRENT_CONFIG = &H80000004
strKeyPath = ""
Sub EnumerateKeys(hive, key, f)
f.WriteLine( key)
reg.EnumKey hive, key, arrSubKeys
If Not IsNull(arrSubKeys) Then
For Each subkey In arrSubKeys
EnumerateKeys hive, key & "\" & subkey, f
Next
End If
End Sub
Set reg = GetObject("winmgmts://./root/default:StdRegProv")
Set fso = CreateObject("Scripting.FileSystemObject")
path = <Path to Text file>
If fso.FileExists(path) Then
Else
Set file = fso.CreateTextFile(path)
End If
EnumerateKeys HKEY_LOCAL_MACHINE, strKeyPath, file
executes to
\BCD00000000
\HARDWARE
\SAM
\SECURITY
\SOFTWARE
\SYSTEM
and only seems to go the first file path deep. Any help would be appreciated.
Thanks

Pulling Registry Values not working

I have just started learning automation....
Am looking to query below registry values to a test file with a date.
eRegistry values
I have written below script; however, I think this is missing some logic hence not able to get append value for each registry string,
Const HKEY_LOCAL_MACHINE = &H80000002
Const ForAppending = 2
Set objfso = CreateObject("scripting.filesystemobject")
Set objFile = objfso.OpenTextFile("C:\SWSetup\abc.txt", 8, true)
'Set filestreamOUT = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\Test.txt,8,true)
strComputer = "."
'strPath = "C:\SWSetup\abc.txt"
strKeyPath = "SOFTWARE\SAT"
strValueName1 = "Bcore"
strValueName = "GG"
strValueName = "Graph"
strValueName = "MEM"
strValueName = "PHD"
strValueName = "PHDk"
strValueName = "Pro"
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")
'oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue,strValueName1
objFile.WriteLine (Now)
objfile.WriteLine strValue
objfile.Close
You can do it in a much simpler way.
Use WScript.Shell Object to read values from registry.
Created a sample for you.
Modify it accordingly
I tested on my system.
I am seeing the expected results.
'DECLARE A FILE SYSTEM OBJECT
Set objfso = CreateObject("scripting.filesystemobject")
Set objFile = objfso.OpenTextFile("C:\\Users\\<<YOUR USER NAME>>\\desktop\\abc.txt", 8, true)
'CREATE A SHELL OBJECT TO READ REGISTRY
Dim WSHShell, value
Set WSHShell = CreateObject("WScript.Shell")
'READ REGISTRY VARIABLES
value = WSHShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\SAT\GG")
objFile.WriteLine (value & " " & Now)
value = WSHShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\SAT\Graph")
objFile.WriteLine (value & " " & Now)
value = WSHShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\SAT\MEM")
objFile.WriteLine (value & " " & Now)
value = WSHShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\SAT\PHD")
objFile.WriteLine (value & " " & Now)
'CLEAR THE OBJECTS
objfile.Close
set WSHShell = nothing
To get the key name perfectly:
Run the registry and search for "SAT" in your case.
Select it and right click your mouse.
You can see "Copy Key Name".
Select it.
Now paste it in some notepad.
Then append the "value" names to it. ( in your case GG MEM etc..)

VBScript: I need to be able to read all the subkey names in a registry key and check for a dword value in each

I am stumped here. I can't seem to wrap my head around getting the subkeys read into an array, and then walk through each subkey to search for a certain Dword value. I could assign variables to every subkey (which would take forever.)
My script works fine when I have it set up to look at one subkey and one value:
Const HKLM = &H80000002
Const REG_SZ = 1
Const REG_EXPAND_SZ = 2
Const REG_BINARY = 3
Const REG_DWORD = 4
Const REG_MULTI_SZ = 7
strKeyPath = "HKEY_LOCAL_MACHINE\SOFTWARE\Program\Policies\policy1"
strValueName = "lastStatus"
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
objRegistry.GetDWORDValue HKLM,strKeyPath,strValueName,dwValue
If dwValue = 0 Then
Wscript.Echo strcomputer & " not done."
Elseif dwvalue = 3 Then
Wscript.Echo strcomputer & " is done!"
But, I need the script to walk through HKLM\Software\program\policies\policy1, then HKLM\Software\program\policies\policy2, then HKLM\Software\program\policies\policy3... until it reads every policy, of which there are 32 to 34 depending on the computer.
Then I need to check within each policy subkey if a dword value = 0 or 3.
I am not sure what you mean by "can't wrap my head around arrays". Do you mean you don't like the idea? In that case if you can be sure about the names of the subkeys you can do it like
For i = 1 To 34
strKeyPath = "SOFTWARE\Program\Policies\policy" & i
objRegistry.GetDWORDValue HKLM,strKeyPath,strValueName,dwValue
Next
If you just mean you don't know the proper way to do it, the normal approach would be
Const HKLM = &H80000002
strComputer = "."
strKeyPath = "SOFTWARE\Policies\"
strValueName = "lastStatus"
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
objRegistry.EnumKey HKLM, strKeyPath, subKeys
For Each subKey In subKeys
objRegistry.GetDWORDValue HKLM,strKeyPath & subKey ,strValueName,dwValue
Next
Please note that either way, if you use the WMI registry provider the keyPaths are always without "HKEY_LOCAL_MACHINE\" because you have (correctly) specified that as constant already.

How can I pull the correct data from the HK Current User registry key instead of temp profile information

I am working on a script to pull the value in the key
HKCurrentUser\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Desktop
Currently all it is returning is:
C:\WINDOWS\system32\config\systemprofile\Desktop
When I want/think it should return:
%USERPROFILE%\Desktop
Below is the script that is pulling the infomration from the key and as far as I can tell it should be pulling the correct information. Just wondering if someone can enlighten me as to what I am missing. It also returns the computer name and the logged in username which both return correctly. This is going to be run on quite a few machines remotely.
'These are the constants for the following KEYS'
Const HKClassesRoot = &H80000000 'HKEY_CLASSES_ROOT
Const HKCurrentUser = &H80000001 'HKEY_CURRENT_USER
Const HKLocalMachine = &H80000002 'HKEY_LOCAL_MACHINE
Const HKUsers = &H80000003 'HKEY_USERS
Const HKCurrentConfig = &H80000005 'HKEY_CURRENT_CONFIG
'Setup objects to interact with here'
Set wshShell = WScript.CreateObject("Wscript.Shell")
strComputer = wshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
Set objNetwork = CreateObject("Wscript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Define variable to store the current user and then pull the current user
Dim currentUser
strCurrentUser = objNetwork.UserName
'find the data in the string we want to get the value from'
strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\"
strValueName = "Desktop"
'pull the info and store it in strValue'
objRegistry.GetStringValue HKCurrentUser,strKeyPath,strValueName,strValue
'setup for output of data to the file'
Dim strSpacer
Dim strData
strSpace = "+-------------------------------------------------------------------------------------------------------------------------+"
strData = "| " & strComputer & " == " & strCurrentUser & " == " & strValue & " |"
Dim strFileName
strFileName = "\\server\share\" & strCurrentUser & ".txt"
Set objFile = objFSO.OpenTextFile(strFileName,8,true)
objFile.write vbCrLf & strSpace & vbCrLf
objFile.write strData & vbCrLf
objFile.write strSpace & vbCrLf
'Close file'
objFile.Close
After review I found the answer to my own question. I was reading the registry incorrectly for what I was doing.
strRegkey = "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Desktop"
strDataValue = wshShell.RegRead(strRegKey)
this returns the value stored currently in the key.
I suspect that the environment variable %USERPROFILE% from the registry value gets expanded to the profile of the user running the WMI service (LOCAL SYSTEM). GetStringValue seems to behave the same as GetExpandedStringValue when reading REG_EXPAND_SZ values.

Why wont this WMI script read my registry string value?

I'm using the examples from technet to try to read a dword / string from HKLM\Microsoft\Windows\CurrentVersion\Run called MyStartupExe.. It is returning empty.. This regular example works:
Const HKEY_CURRENT_USER = &H80000001
strComputer = "."
Set oReg=GetObject( _
"winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")
strKeyPath = "Console"
strValueName = "HistoryBufferSize"
oReg.GetDWORDValue _
HKEY_CURRENT_USER,strKeyPath,strValueName,dwValue
WScript.Echo "Current History Buffer Size: " & dwValue
My adaptation of it does not work. The string and dword value exists in the registry at the key path I'm looking for.
const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set oReg=GetObject( _
"winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
strValueName = "MyStartUpExe"
oReg.GetDWORDValue _
HKEY_LOCAL_MACHINE,strKeyPath,strValueName,dwValue
WScript.Echo "MyStartupExe" & dwValue
"MyStartUpExe" is most likely a REG_SZ value, not a REG_DWORD value, so you'll have to use GetStringValue() instead of GetDWORDValue().
oReg.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, value
WScript.Echo "MyStartUpExe" & value
Refer to the WMI StdRegProv class.
Or you could just use Shell.RegRead to read registry values where you don't know the values' datatype. If the return code of RegRead is 0 (success) a reg value exists, else if the return code will be some general error code, e.g. &h800xxxxx etc. then no reg value exists. To check your OS architecture type, query the Win32_Processor.Architecture value (where '0' = 'x86' or '9' = 'x64').

Resources