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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Undefined variable error 1

Status
Not open for further replies.

AshGoon

Technical User
Aug 9, 2007
5
GB
All

i am trying to write a script for mom 2007, which will check for wmi and do an nslookup. i'm getting stuck with some of the syntax as i'm new to all this, was wondering if someone could help? Script has been split into 2 for now, 1st part is this:

Option Explicit
Dim oArgs, oShell, fso, oNetwork
Dim wmistatus
Dim objWMIservice
Dim objWMI

Set oArgs = WScript.Arguments
set oShell = CreateObject("Wscript.Shell")
Set fso = CreateObject("scripting.filesystemobject")
set oNetwork = CreateObject("Wscript.Network")


'Start of main
'Check WMI is running

wmistatus = IsWMIRunning(objWMI)

If wmistatus <> False Then
wmiMessage = "WMI is running"
Else
wmiMessage = "Cannot query WMI"
End If

'Query disk space (no point if WMI is down)
If wmistatus <> False Then
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & oNetwork.ComputerName & "\root\cimv2")
Set colItems = objWMIService.ExecQuery ("Select DeviceID, FreeSpace from Win32_LogicalDisk where DriveType = 3")

For Each objItem in colItems
'Write to return event
DiskName = objItem.DeviceID
MBFree = formatnumber((objItem.FreeSpace/1048576),2)
diskmessage = diskmessage & "Name: " & DiskName & " Free megabytes: " & MBFree & VbCrLf
Next

End If

'Check WMI
Function IsWMIRunning()
On Error Resume Next
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & oNetwork.ComputerName & "\root\cimv2")
If Err Then
IsWMIRunning = False
Else
IsWMIRunning = True
End If
End Function

i get variable undefined error - states that wmistatus is the problem. but i don't know what i'm doing wrong here - any ideas?

Thanks
 
Some discrepancy here:
wmistatus = IsWMIRunning[!](objWMI)[/!]
vs
Function IsWMIRunning[!]()[/!]

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
thanks, good to know where the problem is - only i don't know why?

as there is no argument, the function pair is empty. i don't know what i shouls be putting in the 1st set though. initially it was blank and i was still getting errors, so tried it with the object, but it was guesswork. what should the correct syntax be here?
 
If think the undefined variable is wmiMessage ...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
ok, i'm really confused. corrected that, but now i'm getting - variable is undefined: 'objitem'.

why does that need to be declared - i'm sure itr doesn't does it? is there a more funadamental problem with the script?
 
why does that need to be declared
Because the following instruction:
Option Explicit

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
thx for the replies phv.

ok so a little clearer - understand about option explicit.

regards the function wmistatus, still not clear on what/how i should code this? do i need to put a value when the function is called, and how do i define this? the book i'm using isn't clear on how i define the argument in the paretheses. should it be declared in the function? and then whats declared in the function call?

sorry, but i'm a newbie to all this - 1st script of many that i have to write!
 
'personally i would do the following
'actually i prob wouldnt go so far as the function but there you go
Dim objWMI
wmistatus = IsWMIRunning(objWMI)

If wmistatus <> False Then
'NO NEED TO CREATE ANOTHER WMI INSTANCE
Set colItems = objWMI.ExecQuery ("Select DeviceID, FreeSpace from Win32_LogicalDisk where DriveType = 3")

....

End If

Set objWMI = Nothing


' i have used objPassed for berevaty
'Check WMI
Function IsWMIRunning(objPassed)
On Error Resume Next
Set objPassed = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
If Err Then
IsWMIRunning = False
Else
IsWMIRunning = True
End If
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top