PDA

View Full Version : Solved: Newbie question



EricM
04-08-2005, 04:03 AM
How can I get a result from the below script to append to a spread sheet or txt file

' Check command line parameters
Select Case WScript.Arguments.Count
Case 0
' Default if none specified is local computer (".")
Set objWMIService = GetObject( "winmgmts://./root/cimv2" )
Set colItems = objWMIService.ExecQuery( "Select * from Win32_ComputerSystem", , 48 )
For Each objItem in colItems
strComputer = objItem.Name
Next
Case 1
' Command line parameter can either be a computer
' name or "/?" to request online help
strComputer = UCase( Wscript.Arguments(0) )
if InStr( strComputer, "?" ) > 0 Then Syntax
Case Else
' Maximum is 1 command line parameter
Syntax
End Select
' Define constants
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
' Header line for screen output
strMsg = vbCrLf & "CPU load percentage for " & strComputer & ":" & vbCrLf & vbCrLf
' Enable error handling
On Error Resume Next
' Connect to specified computer
Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/cimv2" )
' Display error number and description if applicable
If Err Then ShowError
' Query processor properties
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Processor", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)
' Display error number and description if applicable
If Err Then ShowError
' Prepare display of results
For Each objItem In colItems
strMsg = strMsg _
& "Device ID : " & objItem.DeviceID & vbCrLf _
& "Load Percentage : " & objItem.LoadPercentage & vbCrLf & vbCrLf
Next
' Display results
WScript.Echo strMsg
'Done
WScript.Quit(0)

Sub ShowError()
strMsg = vbCrLf & "Error # " & Err.Number & vbCrLf & _
Err.Description & vbCrLf & vbCrLf
Syntax
End Sub

Paleo
04-08-2005, 04:48 AM
Hi EricM,

what do you want to have stored at your spreadsheet? Is your script from a .vbs file or you have added it to a spredsheet yet?

If it enclosed in a spredsheet you may simply type:
Range("A" & Range("A65536").End(xlUp).Row + 1) = strMsg

EricM
04-08-2005, 05:53 AM
Paleo you are great at this. I appreciate all the help you give.
here is what I am doing.
I use host monitor to monitor my Network assets. Well lately it has been detecting a high CPU on some of my Citrix server. By the time I get there the CPU usage is down (Naturally) But my boss seems to think HM is giving false messages. So I am going to add this VB script to HM so when the CPU is above the 85% the action from host monitor will be to Run the VBS script. This way I can compare the time HM says it was high to the entry from the VBS script to see if it really was above 85%.


Hi EricM,

what do you want to have stored at your spreadsheet? Is your script from a .vbs file or you have added it to a spredsheet yet?

If it enclosed in a spredsheet you may simply type:
Range("A" & Range("A65536").End(xlUp).Row + 1) = strMsg

Paleo
04-08-2005, 07:47 AM
Hi Eric,

well sounds like my script wont help you too much as you dont want to enclose it in excel, so give me give me some more info and I will try to adapt the script for you. Which one is your operating system and which version is your HM?

Anyway, you may use this script to write to a text file:


Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
("c:\scripts\service_status.txt", ForAppending, True)
Set colServices = GetObject("winmgmts:").ExecQuery _
("Select * from Win32_Service")
For Each objService in colServices
objTextFile.WriteLine(objService.DisplayName & vbTab & _
objService.State)
Next
objTextFile.Close

This example script demonstrates how to retrieve the status for all the services installed on a computer, and then saves the service name and status to a text file.

EricM
04-14-2005, 06:53 PM
IT is on a W2K server and Host Monitor is the latest version. I am not at work to check right now but I know I just updated it. It can also goto a spread sheet. I just thought it be easier to goto a txt file thats all.

Paleo
04-17-2005, 01:08 PM
Hi Eric,

so did my script has done the trick for you?

EricM
04-17-2005, 07:37 PM
Yes
Sorry busy week

Paleo
04-18-2005, 04:28 AM
Great, I am glad I could help you out!:thumb