Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WMI Registry problem

Status
Not open for further replies.

josephk73

Technical User
Aug 1, 2002
115
GB
Hi

I have a script that I am writing that will connect to a server remotely and read a registry value (type is REG_SZ). The script appears to reach the end but stops with the runtime error: Invalid use of Null: 'Value'

To help here is a snippet of the code that generates the error:

Sub RegistrySize

Dim strKeyPath
Dim strValueName
Dim dwValue
Dim objReg
Dim objWMIConnect
Dim logfile
Const HKEY_LOCAL_MACHINE = &H80000002


wscript.echo "hello"

Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIConnect = SWBemlocator.ConnectServer(strComputer,"\root\default",Name,passWord)


wscript.echo "hi"

strKeyPath = "SOFTWARE\Infrastructure"
strValueName = "Build Number"
Set objReg=objWMIConnect.get("stdRegProv")

wscript.echo strValuename


objReg.getstringvalue HKEY_LOCAL_MACHINE, strKeyPath, strValueName,Value
msgbox value
End Sub

Note: All the required variables have been set-up above the function and this has been tested on several servers!

Any help would be appreciated!

Ps Does any one have another way to connect to a remote server that allows you to pass credentials?
 
[0] The problem is that if anything "erronous" happens to the getstringvalue method of the objReg, the out-param value would be null. And display a null via msgbox is an "Invalid use of null". This could simply be reproduce by this single line.
[tt] msgbox null 'runtime error [wrong][/tt]

[1] The wmi service stdregprov handles the kind of errors from its methods mostly non-intrusively by returning non zero coded error number. Hence, the simplest is to control the return value, like this.
[tt]
dim nret
nret=objReg.getstringvalue(HKEY_LOCAL_MACHINE, strKeyPath, strValueName,Value)
if nret <> 0 then
msgbox "Error occurs for multiple reason to be ascertained." & vbcrlf & "Returned error number: " & nret
else
msgbox value
end if
[/tt]
[2] Error can due to many reasons.
[ul]
[li]Insufficent access rights to the key[/li]
[li]Inexistence of the key[/li]
[li]Inexistence of the value so named[/li]
[li]data type is not reg_sz[/li]
[li]etc...[/li]
[/ul]
If you want to investigate what is the cause of it, you should first check the nret from the enumeration of error number (can be found in msdn documentation), use CheckAccess method to make sure you have sufficient access rights to the key, use enumerate methods to make sure the existence of the key and/or the value named and so on...
 
Thanks for the reply :) . I have used the information you provided and this returned error 2. I have also used the checkAccess method and this has confirmed that i do not have access to read the key!

Since I am testing this script with a domain admin account this is surprising. Is there any way around this, since I only need to read the key?

Again Thanks!
 
Why are you so sure the existence of the key to begin with? Do you go to the m/c, open and look at it? That's what you have to do.
 
Thanks for the reply!

Yes I am 100% sure that the key exists. I have seen the key myself and have this script in a modified version returning a DWord value. Its just the returning of the reg_sz that has caused me problems!

Hope that helps!
 
Hi tsjui,

yes the key is a string. I did a little experiment and ran Reg query and I got output that was correct. So the key does exist and should be returned. So I'm thinking, okay let me try and run the req query command using vbscript. But still no luck. here is the code.

strkey="\HKLM\SOFTWARE\VMWare, Inc.\VMware Tools /v Lastboot"


Set objShell = CreateObject("WScript.Shell")
Set objScriptExec = objShell.Exec("cmd /c reg query" & " " & "\\birm00001\" & strkey)

Do While Not objScriptExec.StdOut.AtEndOfStream
strText = objScriptExec.StdOut.ReadAll()
WScript.Echo strText
Loop
 
What about this ?
Code:
Set objScriptExec = objShell.Exec("cmd /c reg query ""\\birm00001\" & strkey & """")

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi PHV,

Although your code did not work, you put me on the right track and I'm grateful.

It is as you suggested all about spaces and quotes.

In the end this worked:

strkey="\HKLM\SOFTWARE\VMWare, Inc.\VMware Tools" & Chr(34) & " /v LastBoot"

Set objShell = CreateObject("WScript.Shell")
Set objScriptExec = objShell.Exec("reg query " & Chr(34) & "\\" & strComputer & strkey )

Do While Not objScriptExec.StdOut.AtEndOfStream
strText = objScriptExec.StdOut.ReadAll()
WScript.echo strText
Loop

BIG BIG thanks to everybody who contributed.
 
All that does not mean wmi stdregprov getstringvalue() method has any kind of deficiency. That's the only point I want to make.
 
This worked just fine for me

Code:
Option Explicit

Main()

Sub Main()
		Const HKLM = &H80000002
		Const HKCU = &H80000001
		Const HKUS = &H80000003
		
		Dim strComputer : strComputer = "xxxx"
		Dim objReg : Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
		Dim strKeyPath : strKeyPath = "SOFTWARE\VMWare, Inc.\VMware Tools"
		Dim strValName : strValName = "Lastboot"
		Dim strValue
		objReg.GetStringValue HKLM, strKeyPath, strValName, strValue
		If Not IsNull(strValue) Then
			WScript.Echo strValue
		End If
End Sub

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top