Active Scripts and WMI

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).
avicooli
Posts: 6
Joined: Mon Jul 23, 2007 8:06 am

Active Scripts and WMI

Post by avicooli »

Hi,
I've recently started working with HostMonitor and wanted to make a test which will check the Defragmentation level of one of the local HDD's, and will alert me if I should defragment one of them. The thing is that I didn't find such an option within host monitor and made a small VBS file using WMI. I wanted to run the script as an Active Script test in Host Monitor, and made the .vbs file like those on the examples, the thing is, that Host Monitor, it seems - and correct me if I'm wrong, doesn't support Active Scripts with WMI.

When I clicked 'Test' in the place where I choose the file - when I create the Test. I got the strange error which got me to the conclusion above:
Microsoft VBScript Runtime Error,
Variable is undefined "WSCript"
The strange thing is that WSCript is a Global variable, witch allows you to use WMI within the VBS. and the strangest off all is that if I run the code not in the syntax of the HostMonitor, alone - on windows it works (I added just for the experiment a WSCript.Echo message)

And there is more, the truth is that I really need this option because I wanted to use a VBS that checks to fined any running process that contains a given string (unlike host monitor who checks to find the whole string) and turn some test to good condition if it's running and bad if not. I also tried doing so with the WMI test method, but I couldn't figure this out.

Thx in advance,
Avi
KS-Soft
Posts: 13012
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

Actually HostMonitor does not execute script by itself. HostMonitor calls MS Script Control (scripting host) that interacts with scripting engine (VBScript)
Could you please provide more information
- Windows version?
- Service Pack?
- What version of Windows Scripting Host is installed on your system?
- Version of HostMonitor?
- HostMonitor is started as service or application?
- May we see entire script?

Regards
Alex
avicooli
Posts: 6
Joined: Mon Jul 23, 2007 8:06 am

Post by avicooli »

you see, the code works when I click Start in the test list but it doesn't get values about the test's status, like it has some sort of a problem analyzing the script's returned value. I've done it just like it is in the examples, with the 'perform_test' function which calls the check defragmentation function - this who uses the WMI ,and returns statusok or statusbad. I'v tried not calling the check defragmentation function and just once I returned statusok and once statusbad, and it worked. But as I include any WMI code It's always on unknown status. I've tried it also with a script that kills all instances of the applications that contain a certain string and it should always return stausok no matter what - the script works but the status is always unknown.
I'm working on Windows XP SP2, With HM 6.80 - started as service.

I don't have the exact code in reach right now, so I will publish it later.

Thanks again,
Avi.


BTW: I just hate PHPBB it took me hours to write this simple message! :o
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

avicooli wrote:I'm working on Windows XP SP2, With HM 6.80 - started as service.
Please take a loo at the following script. It uses WMI call from VB script and works fine with "Active Script Test method":

Code: Select all

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


Dim objWMIService, objProcess, colProcess, HandleCount
Dim strComputer, strProcess
strComputer = "."
strProcess = "'hostmon.exe'" 

FUNCTION performtest()
  HandleCount = "0"
  
  Set objWMIService = GetObject("winmgmts:" _
  & "{impersonationLevel=impersonate}!\" _
  & strComputer & "\root\cimv2") 
  
  Set colProcess = objWMIService.ExecQuery _
  ("Select HandleCount from Win32_Process Where Name = " & strProcess )

  For Each objProcess in colProcess
    HandleCount = objProcess.HandleCount
  Next 
  IF (HandleCount > 0)  THEN
     performtest = statusOk & HandleCount
  ELSE
     performtest = statusBad & HandleCount 
  END IF

END FUNCTION
The script checks the handles usage by the 'hostmon.exe' process. This is a bit simplified script, however, I think, you may use it as an example.

Please note, you have to use function performtest(), not perform_test()
avicooli wrote:BTW: I just hate PHPBB it took me hours to write this simple message! :o
Hm. Strange problem. I think, there were some conection or site availability problems. Sorry, for that.

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

Post by KS-Soft »

I'm working on Windows XP SP2, With HM 6.80 - started as service
Have you specified account with necessary rights on Service page in the Options dialog?
If you provide the script, we will try to find reason of the problem.
BTW: I just hate PHPBB it took me hours to write this simple message!
H'm.. what is the difference? You are using keyboard to type the text anyway.
However if you do not like PHPBB, you may send letters to support@ks-soft.net instead.

Regards
Alex
avicooli
Posts: 6
Joined: Mon Jul 23, 2007 8:06 am

Post by avicooli »

Alex, Max, thank you for your help and quick reply.

I've relooked the kill script and it seems that I forgot do delete a "WSCript.close" line, from my messagebox version, which was the first one. After I deleted the line the code returned a good status, so it is now working.

The thing is that the check defragmentation script, it seems, returned a different error:
Incorrect result value
Here is the code:

Code: Select all

Option Explicit

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

'---- entry point ----
FUNCTION performtest()
    dim strComputer, objWMIServicem, colVolumes, objVolume, objWMIService, blnRecommended, objReport
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

    Set colVolumes = objWMIService.ExecQuery("select * from Win32_VolumeChangeEvent")
    
    For Each objVolume in colVolumes
        errResult = objVolume.DefragAnalysis(blnRecommended, objReport)
        If errResult = 0 Then
            If blnRecommended Then
               performtest=statusBad & "You need to Defrag "& objVolume.Name
            Else
               performtest=statusOk
            End If
        Else
            performtest=statusBad & " Analyze Error!"
        End If
   Next
END FUNCTION
and it took me hours because first I started writing the message and went for half an hour, then I returned and finished writing and when I clicked 'Preview' it asked me to log in and I needed to rewrite the message. Then – after log in I wrote the message again and discovered that it redirected me to 'add a topic' instead of 'add a reply', so I copied the text and deleted the message.

And again – but now I can't blame PHPBB, I wrote this message clicked 'preview' and the Firefox stuck and all of what I wrote was gone again. anyway,

Thanks and Best Regards,
Avi
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

I think, you have to adjust your script into the following way:
1. Define errResult variable
2. Use Win32_Volume class instead of Win32_VolumeChangeEvent
3. Specify particular drive to check (e.g. C:\, D:\, etc)

Script below works fine on our system. It analyses defrag info on drive C:\ as well:

Code: Select all

Option Explicit 

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

'---- entry point ---- 
FUNCTION performtest() 
    dim strComputer, objWMIServicem, colVolumes, objVolume, objWMIService, blnRecommended, objReport, errResult
    strComputer = "." 
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 

    Set colVolumes = objWMIService.ExecQuery("select * from Win32_Volume where Name = 'C:\\'") 
    For Each objVolume in colVolumes 
        errResult = objVolume.DefragAnalysis(blnRecommended, objReport) 
        If errResult = 0 Then 
            If blnRecommended Then 
               performtest=statusBad & "You need to Defrag "& objVolume.Name 
            Else 
              performtest=statusOk 
            End If 
         Else 
            performtest=statusBad & " Analyze Error!" 
         End If 
   Next 
END FUNCTION
Please note: probably you have to increase timeout, that is specified in "Test Properties" window.

Regards,
Max
avicooli
Posts: 6
Joined: Mon Jul 23, 2007 8:06 am

Post by avicooli »

still, its doesn't work :( ,

when i click 'Test' it gives even a stranger error:
Error in script:
Error number :-2147217392
Source :
Description :
Text :
Line :18
Column :4
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

Sorry, I can reproduce such error only when I remove second slash \ from the drive letter. Have you removed the second trailing slash?
Please note: Visual Basic requires two slash after drive letter in this case (e.g. C:\\, D:\\). So, the string on line 17 should be like the following:
Set colVolumes = objWMIService.ExecQuery("select * from Win32_Volume where Name = 'C:\\'")
or
Set colVolumes = objWMIService.ExecQuery("select * from Win32_Volume where Name = 'D:\\'")

Could you post the entire script you are using?

Regards,
Max
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

Hm. Sorry for the previous post. Looks like Win32_Volume class is not available on windows XP. :roll: Quote from the Microsoft:
http://msdn2.microsoft.com/en-us/library/aa394515.aspx
==============
Windows XP and earlier: This class is not available.
==============

As I can see, the Win32_VolumeChangeEvent class also does not represent necessary information.

Probably, you have to use defrag.exe command line utility.
http://support.microsoft.com/kb/283080
You may use "Shell script" or "Execute External program" test method for that purpose.

Regards,
Max
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

As a workaround, you may use the following script, that uses defrag.exe utiluty, to analyze defrag data on your XP machine. Script is designed for the "Shell script" test method: http://www.ks-soft.net/hostmon.eng/tests.htm#chkShell

So, you have to open the "Script Manager", create the new script using "New" button on the top of the window and provide the following information:
Start cmd: cmd /c %Script% %Params%
Script:

Code: Select all

@echo off
%1 - volume to check

c:\windows\system32\defrag.exe %1 /a /v | find "You do not need to defragment this volume" > NUL
IF ERRORLEVEL 1 (
   echo ScriptRes:Bad:You should defragment this volume
) ELSE (
   echo ScriptRes:Ok:You do not need to defragment this volume
)
Script requires the volume label as a parameter. You may specify certain volume label using "Params" box of the "Test Properties" window, e.g. C: or D: , etc.

Regards,
Max
avicooli
Posts: 6
Joined: Mon Jul 23, 2007 8:06 am

Post by avicooli »

KS-Soft Europe wrote:Looks like Win32_Volume class is not available on windows XP. :roll: Quote from the Microsoft:
http://msdn2.microsoft.com/en-us/library/aa394515.aspx
==============
Windows XP and earlier: This class is not available.
==============
So might it work on Windows Server 2003?
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

avicooli wrote:
KS-Soft Europe wrote:Looks like Win32_Volume class is not available on windows XP. :roll: Quote from the Microsoft:
http://msdn2.microsoft.com/en-us/library/aa394515.aspx
==============
Windows XP and earlier: This class is not available.
==============
So might it work on Windows Server 2003?
Sure, script that uses Win32_Volume class works at our Server 2003 as well.

Regards,
Max
avicooli
Posts: 6
Joined: Mon Jul 23, 2007 8:06 am

Post by avicooli »

KS-Soft Europe wrote:
avicooli wrote:
KS-Soft Europe wrote:Looks like Win32_Volume class is not available on windows XP. :roll: Quote from the Microsoft:
http://msdn2.microsoft.com/en-us/library/aa394515.aspx
==============
Windows XP and earlier: This class is not available.
==============
So might it work on Windows Server 2003?
Sure, script that uses Win32_Volume class works at our Server 2003 as well.

Regards,
Max
Ohhh..Ok thanks, the truth is that originally I want to run it on Windows Server 2003 and I was only checking that it works on XP :oops:
I've just now tried it and it works just fine, thank you, and Alex for the support and help.

Best Regards,
Avi
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

avicooli wrote:I've just now tried it and it works just fine
Glad it works! :-)
avicooli wrote:thank you, and Alex for the support and help.
You are welcome.

Regards,
Max
Post Reply