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

Help with If statement

Status
Not open for further replies.
May 11, 2004
37
US
I am trying to use an If statement for the first time. I want to run a Sub based on if the user clicks OK or Cancel.
The Sub "rich" runs regardless of whether I click OK or Cancel. What am I doing wrong?? Also, is there a way to go back to and start the script over after the Sub "rich" is called? Many Thanks in advance!

Const intCancel = 2

Const intOK = 1

strComputer = InputBox("Enter computer name and click ok. If you don't know the name of your computer, click cancel")


If intCancel Then
call rich()

Else

If intOK Then
call rich2()
End IF
End IF

Sub rich()

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
For Each objItem in colItems
Wscript.Echo "Name: " & objItem.Name
Next

End Sub

Sub rich2()

Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "c:\Assettracker\clientcon.exe"


Set objEmail = CreateObject("CDO.Message")
objEmail.From = "richard.rekos@questionmark.com"
objEmail.To = "richard.rekos@questionmark.com"
objEmail.Subject = "AT Output"
objEmail.Textbody = "AT Output"
objEmail.AddAttachment "C:\assettracker\data\" & strComputer & ".xml"
objEmail.Send

End Sub
 
InputBox returns whatever the user enters. Thus if the user simply clicks Cancel, or clicks Ok with nothing entered in the box, then an empty string will be returned. I think thi will work for you:
Code:
strComputer = InputBox("Enter computer name and click ok.  If you don't know the name of your computer, click cancel")

If strComputer = "" Then
    call rich()
Else
    call rich2()
End IF

Sub rich()
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" _
        & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery( _
        "Select * from Win32_ComputerSystem",,48)
    For Each objItem in colItems
        Wscript.Echo "Name: " & objItem.Name
    Next
End Sub

Sub rich2()
    Set objShell = WScript.CreateObject("WScript.Shell")
    objShell.Run "c:\Assettracker\clientcon.exe"
    Set objEmail = CreateObject("CDO.Message")
    objEmail.From = "richard.rekos@questionmark.com"
    objEmail.To = "richard.rekos@questionmark.com"
    objEmail.Subject = "AT Output"
    objEmail.Textbody = "AT Output"
    objEmail.AddAttachment "C:\assettracker\data\" _
        & strComputer & ".xml"
    objEmail.Send
End Sub

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
start the script over after the Sub "rich" is called
Do
strComputer = InputBox("Enter computer name and click ok. If you don't know the name of your computer, click cancel")
If strComputer > "" Then Exit Do
Call rich()
Loop
Call rich2()

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Tom, I noticed the Syntax you used in your query to WMI.

"Select * from Win32_ComputerSystem",,48

I'm curious as to how ,,48 calls the machine name.

I am trying to figure out how to fully utilize the power of WMI, but find it a tall order to decipher. Do you know of any good refernces for using WMI, maybe including syntax usage, etc?

I appreciate you help.
 
I just copied the original code, so that is why the ,,48 is in there. That portion doesn't itself return the machine name, it simply controls how thje results are returned. For more information look here:


[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top