I have to read registry value "GDIProcessHandleQuota" so that I have write below code
strKeyPath = "Schema\wcm://Microsoft-Windows-Win32k-Settings?version=6.1.7600.16385&language=neutral&processorArchitecture=x86&publicKeyToken=31bf3856ad364e35&versionScope=nonSxS&scope=allUsers\metadata\elements\GDIProcessHandleQuota"
Set WSHShell = CreateObject("WScript.Shell")
Set value = WSHShell.RegRead(strKeyPath)
When run this script its showing me error
WshShell.RegRead: Invalid root in registry key "Schema\wcm://Microsoft-Windows-Win32k-Settings?version=6.1.7600.16385&language=neutral&processorArchitecture=x86&publicKeyToken=31bf3856ad364e35&versionScope=nonSxS&scope=allUsers\metadata\elements\GDIProcessHandleQuota".
Used WMI:
'Constants (taken from WinReg.h)
Const HKEY_CLASSES_ROOT= &H80000000
Const HKEY_CURRENT_USER= &H80000001
Const HKEY_LOCAL_MACHINE= &H80000002
Const HKEY_USERS= &H80000003
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows"
strValueName = "GDIProcessHandleQuota"
oReg.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, strValue
WScript.Echo strValue
Above script showing error "type mismatch".
How should read in correct way or is there any alternative way to iterate through registry and find subkey because i have hardcoded the key that have to avoid. I just want write code like pass GDIProcessHandleQuota so that code will provide value.
I have no idea what the first snippet is trying to do. I have never seen a key path like that before.
But the second code snippet fails with a type mismatch error because GDIProcessHandleQuota is a REG_DWORD value but you are trying to read it as a REG_SZ value. You need to use the StdRegProv.GetDWORDValue() method instead of the StdRegProv.GetStringValue() method.
In any case, you don't need to enumerate the keys in this situation, since the location of GDIProcessHandleQuota is fixed and documented.
Related
Here is my code:
Const HKEY_CLASSES_ROOT= &H80000000
Const HKEY_CURRENT_USER= &H80000001
Const HKEY_LOCAL_MACHINE= &H80000002
Const HKEY_USERS= &H80000003
Set StdOut = WScript.StdOut
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
strKeyPath = "SYSTEM\CurrentControlSet\Hardware Profiles\UnitedVideo\CONTROL\VIDEO\{D218E173-A430-11E8-80D8-005056C00008}\0001"
strValueName = "venkat"
oReg.SetDWordValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, 800
oReg.GetDWordValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, dval
WScript.Echo "SYSTEM\CurrentControlSet\Control\" & " = " & dval
I'm running in Windows 10.
SetDWordValue of in the script is not working.
GetDwordValue is working fine and I'm getting the data.
Tried all the possibilities. Even the code from MSDN is not working. I want to change my registry key using vbs.
The most likely reason is that you simply don't have permission to write to that registry key. The WMI registry methods don't raise an error when something goes wrong, so you need to check the method's return code, e.g. by changing the line
oReg.SetDWordValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, 800
to
rc = oReg.SetDWordValue(HKEY_LOCAL_MACHINE, strKeyPath, strValueName, 800)
WScript.Echo rc
A return code of 5 means you have a permission error.
To fix that you probably just need to run the script "as Admin".
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.
I thought this would be easy, but apparently nobody does it...
I'm trying to see if a registry key exists. I don't care if there are any values inside of it such as (Default).
This is what I've been trying.
Set objRegistry = GetObject("winmgmts:\\.\root\default:StdRegProv")
objRegistry.GetStringValue &H80000003,".DEFAULT\Network","",regValue
If IsEmpty(regValue) Then
Wscript.Echo "The registry key does not exist."
Else
Wscript.Echo "The registry key exists."
End If
I only want to know if HKEY_USERES\.DEFAULT\.Network exists. Anything I find when searching mostly seems to discuss manipulating them and pretty much assumes the key does exists since it's magically created if it doesn't.
I found the solution.
dim bExists
ssig="Unable to open registry key"
set wshShell= Wscript.CreateObject("WScript.Shell")
strKey = "HKEY_USERS\.Default\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Digest\"
on error resume next
present = WshShell.RegRead(strKey)
if err.number<>0 then
if right(strKey,1)="\" then 'strKey is a registry key
if instr(1,err.description,ssig,1)<>0 then
bExists=true
else
bExists=false
end if
else 'strKey is a registry valuename
bExists=false
end if
err.clear
else
bExists=true
end if
on error goto 0
if bExists=vbFalse then
wscript.echo strKey & " does not exist."
else
wscript.echo strKey & " exists."
end if
The second of the two methods here does what you're wanting. I've just used it (after finding no success in this thread) and it's worked for me.
http://yorch.org/2011/10/two-ways-to-check-if-a-registry-key-exists-using-vbscript/
The code:
Const HKCR = &H80000000 'HKEY_CLASSES_ROOT
Const HKCU = &H80000001 'HKEY_CURRENT_USER
Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
Const HKUS = &H80000003 'HKEY_USERS
Const HKCC = &H80000005 'HKEY_CURRENT_CONFIG
Function KeyExists(Key, KeyPath)
Dim oReg: Set oReg = GetObject("winmgmts:!root/default:StdRegProv")
If oReg.EnumKey(Key, KeyPath, arrSubKeys) = 0 Then
KeyExists = True
Else
KeyExists = False
End If
End Function
Simplest way avoiding RegRead and error handling tricks. Optional friendly consts for the registry:
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const HKEY_CURRENT_CONFIG = &H80000005
Then check with:
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
If oReg.EnumKey(HKEY_LOCAL_MACHINE, "SYSTEM\Example\Key\", "") = 0 Then
MsgBox "Key Exists"
Else
MsgBox "Key Not Found"
End If
IMPORTANT NOTES FOR THE ABOVE:
Equals zero means the key EXISTS.
The slash after key name is optional and not required.
In case anyone else runs into this, I took WhoIsRich's example and modified it a bit. When calling ReadReg I needed to do the following: ReadReg("App", "HKEY_CURRENT_USER\App\Version") which would then be able to read the version number from the registry, if it existed. I also am using HKCU since it does not require admin privileges to write to.
Function ReadReg(RegKey, RegPath)
Const HKEY_CURRENT_USER = &H80000001
Dim objRegistry, oReg
Set objRegistry = CreateObject("Wscript.shell")
Set oReg = GetObject("winmgmts:!root\default:StdRegProv")
if oReg.EnumKey(HKEY_CURRENT_USER, RegKey) = 0 Then
ReadReg = objRegistry.RegRead(RegPath)
else
ReadReg = ""
end if
End Function
edit (sorry I thought you wanted VBA).
Anytime you try to read a non-existent value from the registry, you get back a Null. Thus all you have to do is check for a Null value.
Use IsNull not IsEmpty.
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set objRegistry = GetObject("winmgmts:\\" & _
strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion"
strValueName = "Test Value"
objRegistry.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
If IsNull(strValue) Then
Wscript.Echo "The registry key does not exist."
Else
Wscript.Echo "The registry key exists."
End If
See the Scripting Guy! Blog:
How Can I Tell Whether a Value Exists in the Registry?
They discuss doing the check on a remote computer and show that if you read a string value from the key, and if the value is Null (as opposed to Empty), the key does not exist.
With respect to using the RegRead method, if the term "key" refers to the path (or folder) where registry values are kept, and if the leaf items in that key are called "values", using WshShell.RegRead(strKey) to detect key existence (as opposed to value existance) consider the following (as observed on Windows XP):
If strKey name is not the name of an existing registry path, Err.Description reads "Invalid root in registry key"... with an Err.Number of 0x80070002.
If strKey names a registry path that exists but does not include a trailing "\" the RegRead method appears to interpret strKey as a path\value reference rather than as a simple path reference, and returns the same Err.Number but with an Err.Description of "Unable to open registry key". The term "key" in the error message appears to mean "value". This is the same result obtained when strKey references a path\value where the path exists, but the value does not exist.
The accepted answer is too long, other answers didn't work for me. I'm gonna leave this for future purpose.
Dim sKey, bFound
skey = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\SecurityHealth"
with CreateObject("WScript.Shell")
on error resume next ' turn off error trapping
sValue = .regread(sKey) ' read attempt
bFound = (err.number = 0) ' test for success
on error goto 0 ' restore error trapping
end with
If bFound Then
MsgBox = "Registry Key Exist."
Else
MsgBox = "Nope, it doesn't exist."
End If
Here's the list of the Registry Tree, choose your own base on your current task.
HKCR = HKEY_CLASSES_ROOT
HKCU = HKEY_CURRENT_USER
HKLM = HKEY_LOCAL_MACHINE
HKUS = HKEY_USERS
HKCC = HKEY_CURRENT_CONFIG
I have a code which checks for a registry key value that exists or not. It only works on none binary values, if the target path is a binary value then it can not checks for it and will tell that the key does not exists.
Here is the Code:
Const HKEY_CURRENT_USER = &H80000001
strComputer = "."
Set objRegistry = GetObject("winmgmts:\\" & _
strComputer & "\root\default:StdRegProv")
strKeyPath = "System\CurrentControlSet\Control\Stranger"
strValueName = "TargetBinaryKey"
objRegistry.GetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue
If IsNull(strValue) Then
WScript.Echo "The Key Does Not Exists."
Else
WScript.Echo "The Key Exists."
End If
What should I do?
You should use .enumValues instead of .GetStringValue. You can find a code snippet here
Addition: you could also use GetBinaryValue if you know at forehand that the value is stored as binary
I want to change the Source file Server path location in the reg path
HKEY_CLASSES_ROOT\Installer\Products\Product GUID\SourceList\Net\1 of every client machines,
as we have removed the existing Application server with a new one...We were able to change the old server path to new server path using the "replace" function in vbscript.
Set objWS = CreateObject("WScript.Shell")
strKeyValue = objWS.RegRead("HKEY_CLASSES_ROOT\Installer\Products\A7C4EB2D0BDDF2A43BDD35A498E12655\SourceList\Net\1")
newstrKeyValue = Replace(strKeyValue,"\\INADCSRV11" ,"\\INADCSRV12")
newstrKeyValue2 = Replace (newstrKeyValue ,"SMSPKGC$" ,"SMSPKGP$")
Const HKEY_CLASSES_ROOT = &H80000000
strComputer = "."
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")
strKeyPath = "Installer\Products\A7C4EB2D0BDDF2A43BDD35A498E12655\SourceList\Net\"
strValueName = "1"
strValue = newstrKeyValue2
oReg.SetExpandedStringValue HKEY_CLASSES_ROOT,strKeyPath,strValueName,strValue
but we are stuck in reading the reg values....
Product GUID is a variable.First we have to read till that path and then after reading one GUID, again we have to read the complete path
HKEY_CLASSES_ROOT\Installer\Products\Product GUID\SourceList\Net\1 and then change the server name
Please let me know anyone have encountered any situation like this.
Assuming I'm not mistaken and you want to enumerate all the subkeys within a certain key this this answer from another question shows code that does this.