value of vbscript variables when run by hostmonitor

All questions related to installations, configurations and maintenance of Advanced Host Monitor (including additional tools such as RMA for Windows, RMA Manager, Web Servie, RCC).
Post Reply
rj
Posts: 7
Joined: Wed Dec 23, 2009 7:43 am

value of vbscript variables when run by hostmonitor

Post by rj »

In order to test some registry key on a windows 2008x64 machine I've tried to use a vbscript calling a WMI stdRegProv method GetDWORD.
The script works fine when run from the commandline but from within Hostmonitor it doesn't.
Aparently the value of the variable "mydwValue" then seems to get lost, therefore the last IF statement in the script indifferently returns "statusBad" even when mydwValue should have been 1:

Code: Select all

Option Explicit

const statusAlive       = "scriptRes:Host is alive:"
const statusDead        = "scriptRes:No answer:"
const statusUnknown     = "scriptRes:Unknown:"
const statusNotResolved = "scriptRes:Unknown host:"
const statusOk          = "scriptRes:Ok:"
const statusBad         = "scriptRes:Bad:"
const statusBadContents = "scriptRes:Bad contents:"

Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001  
Const HKEY_LOCAL_MACHINE = &H80000002  
Const HKEY_USERS = &H80000003  
Const HKEY_CURRENT_CONFIG = &H80000005

dim strComputer, oReg, strKeyPath, strValueName, mydwValue

strComputer = "."
Set oReg=GetObject( _
   "winmgmts:{impersonationLevel=impersonate}!\\" &_
    strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update"
strValueName = "AUOptions"
oReg.GetDWORDValue _
   HKEY_LOCAL_MACHINE,strKeyPath,strValueName,mydwValue

If mydwValue = 1 Then
   WScript.StdOut.Write statusOk 
Else 
   WScript.StdOut.Write statusBad
End if
KS-Soft
Posts: 13012
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

H'm, regardles how we run this script (using HostMonitor or command line), it cannot access this specific registry key on our Windows 2008 system. It works fine with other registry keys and it works just fine on Windows XP, 2003 systems.
We checked user permissions, tried to use regedit - everything is fine.
Looks like some Windows "features" :roll:

BTW:
What exactly HostMonitor option do you use to run this script? Shell Script test method?

Note:
If you are running HostMonitor as service, please do not forget to provide admin account using options located on Service page in HostMonitor Options dialog

Regards
Alex
rj
Posts: 7
Joined: Wed Dec 23, 2009 7:43 am

Post by rj »

Thanks a lot for diving in to this subject and responding quickly.
Indeed, I've been using the Shell Script test method. I also tested the script with the script manager and the "Lets try" option. To OS is: Microsof Windows Server 2008 R2 Enterprise Version 6.1.7600 and Hostmonitor is being run not as a seervice but started as a program when logged in as Administrator. I'll experiment reading some other keys and post the results here later.

Regards,
Robert
rj
Posts: 7
Joined: Wed Dec 23, 2009 7:43 am

Post by rj »

Strangely the abovementionned script doesn't resolve the following two values when executed by Hostmonitor:
GetDWORDValue HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update.AUOptions
GetStringValue HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon.AutoAdminLogon

yet this one does give a correct reply:
GetDWORDValue HKLM:\SYSTEM\CurrentControlSet\Control\CrashControl.AutoReboot

Maybe its because the first two keys contain a space character.

Just Mentioning: I thought to try a workaround and read the same registry value using a Powershell script. But the following command doesn't seem to work as expected either when run by 32bits powershell.

Code: Select all

get-itemproperty -path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update" -name "AUOptions"
KS-Soft
Posts: 13012
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

No, space is not a problem.
I think there is some permission related issue. Could you try to disable UAC or start HostMonitor using "Run as adminitrator" option?
Actually HostMonitor uses manifest to get admin rights but may be this does not work in your case for some reason :roll:
May be you have installed some antivirus monitors or other security related 3rd party software?

Also, you may modify script a little bit (using On Error Resume Next command) and get error description.
E.g.

Code: Select all

Option Explicit 

const statusAlive       = "scriptRes:Host is alive:" 
const statusDead        = "scriptRes:No answer:" 
const statusUnknown     = "scriptRes:Unknown:" 
const statusNotResolved = "scriptRes:Unknown host:" 
const statusOk          = "scriptRes:Ok:" 
const statusBad         = "scriptRes:Bad:" 
const statusBadContents = "scriptRes:Bad contents:" 

Const HKEY_CLASSES_ROOT = &H80000000 
Const HKEY_CURRENT_USER = &H80000001  
Const HKEY_LOCAL_MACHINE = &H80000002  
Const HKEY_USERS = &H80000003  
Const HKEY_CURRENT_CONFIG = &H80000005 

dim strComputer, oReg, strKeyPath, strValueName, mydwValue 
On Error Resume Next

strComputer = "." 
Set oReg=GetObject( _ 
   "winmgmts:{impersonationLevel=impersonate}!\\" &_ 
    strComputer & "\root\default:StdRegProv") 
If Err.Number=0 then
   strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update" 
   strValueName = "AUOptions" 
   oReg.GetDWORDValue _ 
     HKEY_LOCAL_MACHINE,strKeyPath,strValueName,mydwValue 
   If Err.Number=0 then
     If mydwValue = 1 Then 
        WScript.StdOut.Write statusOk 
     Else 
        WScript.StdOut.Write statusBad 
     End if 
  else
    WScript.StdOut.Write statusUnknown & Err.Description
  end if
else
 WScript.StdOut.Write statusUnknown & Err.Description
end if
Regards
Alex
rj
Posts: 7
Joined: Wed Dec 23, 2009 7:43 am

Post by rj »

Hi Alex!

Thanks for the script improvements and debugging tips.
According to the taskmanager: the hostmon.exe*32 process was already running as Administrator. To be sure I've disabled UAC restarted the server and started HostMonitor using "Run as adminitrator". The machine runs without any 3rd party virusscanners / security tools, but maybe I should mention that its installed under a VMWare host.

I've copied your script including the error description. In it, i've took the liberty to add "& mydwValue" behind the status in the following lines:

Code: Select all

     If mydwValue = 1 Then 
        WScript.StdOut.Write statusOk & mydwValue 
     Else 
        WScript.StdOut.Write statusBad & mydwValue 
     End if

Thereafter, running the script from Hostmonitor's Script Manager I get:
[0:40:36] HostMonitor is going to execute "vbscript CheckAutoUpdateOff" script ...
[0:40:36] Script executed, correct result received:
----------
- Status: Bad
- Reply:
----------
Yet the same script from the commandline produces:
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Users\Administrator>cscript /E:vbscript c:\scripts\CheckAutoUpdateOff.vbs
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

scriptRes:Ok:1
When I change the line

Code: Select all

If mydwValue = 1 Then
in

Code: Select all

If mydwValue = nil Then
then the result in Hostmonitor becomes:
[0:43:23] HostMonitor is going to execute "vbscript CheckAutoUpdateOff" script ...
[0:43:23] Script executed, correct result received:
----------
- Status: Ok
- Reply:
----------
So, it still appears that when run from Hostmonitor mydwValue does not get properly assigned its value or looses its value outside some kind of scope. I've continued by trying to run the Hostmonitor application with compatability modes Windows 2008 and Windows XP, but this made no difference either.

Regards,
Robert
Paul_NHS
Posts: 59
Joined: Wed Feb 25, 2009 6:17 am

Post by Paul_NHS »

"If mydwValue = nil Then" should not work unless you have set mydwValue to "nil". If you are testing for NULL you need to use "If mydwValue is nothing Then" or if it's an empty string it's "If mydwValue = "" Then".

cheers, Paul
rj
Posts: 7
Joined: Wed Dec 23, 2009 7:43 am

Post by rj »

Ok. I've been able to replicate the faulty behavior on the commandline using the 32bit version of cscript.exe
Using the following script:

Code: Select all

Option Explicit 

const statusUnknown      = "scriptRes:Unknown:" 
const statusOk           = "scriptRes:Ok:" 
const statusBad          = "scriptRes:Bad:" 
Const HKEY_LOCAL_MACHINE = &H80000002  


dim strComputer, oReg, strKeyPath, strValueName, mydwValue 
On Error Resume Next 

strComputer = "." 
Set oReg=GetObject( _ 
   "winmgmts:{impersonationLevel=impersonate}!\\" &_ 
    strComputer & "\root\default:StdRegProv") 
If Err.Number=0 then 
   strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update" 
   strValueName = "AUOptions" 
   oReg.GetDWORDValue _ 
     HKEY_LOCAL_MACHINE,strKeyPath,strValueName,mydwValue 
   If Err.Number=0 then 
     If mydwValue = 1 Then 
        WScript.StdOut.Write statusOk & mydwValue 
     Else 
        WScript.StdOut.Write statusBad & mydwValue 
     End if 
  else 
    WScript.StdOut.Write statusUnknown & Err.Description 
  end if 
else 
 WScript.StdOut.Write statusUnknown & Err.Description 
end if
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Users\Administrator>%windir%\system32\cscript.exe /E:vbscript /NoLogo c:\scripts\CheckAutoUpdateOff.vbs
scriptRes:Ok:1
C:\Users\Administrator>%windir%\sysWOW64\cscript.exe /E:vbscript /NoLogo c:\scripts\CheckAutoUpdateOff.vbs
scriptRes:Bad:
C:\Users\Administrator>
Thus, it is most probably related to the fact that hostmonitor uses the 32bit version of cscript. Is there some workaround for this or should I convince our sysadmin to provide a 32bit version of Windows for me?

Regards,
Robert
KS-Soft
Posts: 13012
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

Theoretically its easy to configure HostMontior's Shell Manager to start 64bit version of cscript - just type
c:\windows\system32\csript.exe /E:VBScript %Script% %Params%
instead of
csript.exe /E:VBScript %Script% %Params%

But here is surprise - Windows starts c:\windows\sysWOW64\csript.exe even if 32bit application (e.g. HostMonitor) specifically calls another exe module (c:\windows\system32\csript.exe)!!
That's why we could not execute this script from command line - we used another 32bit application as shell.

We tried to modify script but still - 32bit cscript.exe on 64bit system cannot access this registry key :(
May be there is some simple Windows tweak that can fix the problem but we cannot find such information. Compatibility mode does not help...

Regards
Alex
KS-Soft
Posts: 13012
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

That's why script and 32bit applications cannot access this registry key - Windows redirects requests to HKEY_LOCAL_MACHINE\Software tree
(and Windows "reflector" does not reflects everything... )
http://support.microsoft.com/kb/305097

BTW: if you copy cscript.exe from system32 folder into sysWOW64 then HostMonitor will be able to start 64bit script processor.
But I am not sure this is 100% safe for other 32bit applications that are using scripts on this system :roll:

Regards
Alex
rj
Posts: 7
Joined: Wed Dec 23, 2009 7:43 am

Post by rj »

Thanks a lot for your inputs Paul and Alex.
The solution to have Hostmonitor call the 64 bit version of cscript is just fine with me. I've copied and renamed cscript from %windir%\system32\cscript.exe to %windir%\SysWOW64\cscript64.exe leaving the origional 32bit version in place. Using the following line in Hostmonitor to start the script then solves the problem.
cscript64.exe /B /E:VBScript %Script%
KS-Soft
Posts: 13012
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

Nice solution!
And we found another one. There is script that can check 64bit registry tree from 32bit application.

Code: Select all

Option Explicit

const statusAlive       = "scriptRes:Host is alive:"
const statusDead        = "scriptRes:No answer:"
const statusUnknown     = "scriptRes:Unknown:"
const statusNotResolved = "scriptRes:Unknown host:"
const statusOk          = "scriptRes:Ok:"
const statusBad         = "scriptRes:Bad:"
const statusBadContents = "scriptRes:Bad contents:"
const HKEY_LOCAL_MACHINE = &H80000002

dim sData, OS, objArgs, keyPath, keyName
Set objArgs = WScript.Arguments

if objArgs.count <> 4 then
  WScript.StdOut.Write statusUnknown & "expected parameters: <Path to registry key> <Key name> <registry type: 32 | 64> <value to compare>"
  WScript.Quit
end if

keyPath = objArgs(0)
keyName = objArgs(1)

if objArgs(2) = "64" then
  OS = 64
else
  OS = 32
End if

sData = ReadRegStr (HKEY_LOCAL_MACHINE, keyPath, keyName, OS)

if CStr(sData) = objArgs(3) then
  WScript.StdOut.Write statusOk & sData
else
  WScript.StdOut.Write statusBad & sData
end if

Function ReadRegStr (RootKey, Key, Value, RegType)
    Dim oCtx, oLocator, oReg, oInParams, oOutParams

    Set oCtx = CreateObject("WbemScripting.SWbemNamedValueSet")
    oCtx.Add "__ProviderArchitecture", RegType

    Set oLocator = CreateObject("Wbemscripting.SWbemLocator")
    Set oReg = oLocator.ConnectServer("", "root\default", "", "", , , , oCtx).Get("StdRegProv")

    Set oInParams = oReg.Methods_("GetDwordValue").InParameters
    oInParams.hDefKey = RootKey
    oInParams.sSubKeyName = Key
    oInParams.sValueName = Value

    Set oOutParams = oReg.ExecMethod_("GetDwordValue", oInParams, , oCtx)

    ReadRegStr = oOutParams.uValue
End Function
Parameters: <Path to registry key> <Key name> <registry type: 32 | 64> <value to compare>"

Regards
Alex
Post Reply