Flow control issue resulting in WMI object error...

Author Message
Niceman

  • Total Posts : 2
  • Scores: 0
  • Reward points : 0
  • Joined: 1/31/2012
  • Status: offline
Flow control issue resulting in WMI object error... Wednesday, February 01, 2012 12:12 AM (permalink)
0
Hello!

Just started VBscripting and having a lot of fun, however, I've run into a snag. I'm not yet full bottle on flow control and basically what I am trying to achieve is move on to the main function of my script after a simple "Welcome" MsgBox. Where I'm running into trouble is having the script quit if the user clicks vbCancel.

After a couple of different ways to organise the script, I tried using an Else argument but this results in error 80041021 at line 17 char 21. I can get the Welcome box OK and the Select Computer box OK but it conks out after that.
Here is what I smashed together:


Call Welcome
Function Welcome
Dim intResponse, strComputer, strWMIOS

intResponse = MsgBox("Welcome to my Terrific and Exciting OS Information Tool!", vbInformation + vbOKCancel, "OS Information Tool")
If intResponse = vbOK Then
strComputer = Inputbox("Input the name of the remote computer or just click OK (or hit Enter) for this computer.", "OS Information Tool")
If strComputer = "" Then
strComputer = "."
Else If intResponse = vbCancel Then Wscript.Quit
End If
End If
End Function

Call ReportOS
Function ReportOS
Dim objWmiService : Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\"& strComputer & "\root\cimv2")

Set colOSes = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
Set colSMBIOS = objWMIService.ExecQuery("Select * from Win32_SystemEnclosure")

For Each objOS in colOSes
For Each objSMBIOS in colSMBIOS

MsgBox "Computer Name: " & objOS.CSName & vbcrlf _
& "Caption: " & objOS.Caption & vbcrlf _
& "Version: " & objOS.Version & vbcrlf _
& "Build Number: " & objOS.BuildNumber & vbcrlf _
& "Build Type: " & objOS.BuildType & vbcrlf _
& "OS Type: " & objOS.OSType & vbcrlf _
& "Service Pack: " & objOS.ServicePackMajorVersion & vbcrlf _
& "Serial Number: " & objSMBIOS.SerialNumber, vbInformation + vbOK, "OS Information Tool"

Next
Next
End Function

I assumed the best way to skip ahead to the meat and potatoes of the script was to sneak the Else If intResponse = vbCancel Then Wscript.Quit after we established it was OK to display the computer select box after clicking vbOK.

This is actually kind of exciting. :)
- D

<message edited by Niceman on Wednesday, February 01, 2012 12:14 AM>
 
#1
    TaterTot

    • Total Posts : 33
    • Scores: 0
    • Reward points : 0
    • Joined: 7/26/2010
    • Status: offline
    Re:Flow control issue resulting in WMI object error... Wednesday, February 01, 2012 4:26 AM (permalink)
    0
    For this particular error the flow is exactly your problem.
     
    Regardless of what you click and where, this is what your script does:
    Run Welcome function
    Run ReportOS
     
    Your ReportOS function runs every time whether strComputer gets set to anything or not.  To show you what I mean, try putting the following above Call ReportOS:
    strComputer = "."
     
    You'll notice the script runs fine.  So what does this indicate? strComputer = "." is never run in your Welcome function.
     
    Hope this helps.
     
    #2
      Niceman

      • Total Posts : 2
      • Scores: 0
      • Reward points : 0
      • Joined: 1/31/2012
      • Status: offline
      Re:Flow control issue resulting in WMI object error... Thursday, February 02, 2012 4:22 AM (permalink)
      0
      Hey there, thanks for your reply, looks like I managed to suss it out, I just put the Cancel argument in before the OK stuff happens. :)
       
      Now if only I could have that input box inline with the MsgBoxes, it always pops up too high and I added , x300, y300 but that didn't change a thing.
       
      Also, I need to research the whole Next loop thing, that one confuses me.
       
      Dim intResponse, strComputer

      intResponse = MsgBox("Welcome to My Terrific and Exciting OS Information Tool!", vbInformation + vbOKCancel, "OS Information Tool")

      If intResponse = vbCancel Then WScript.Quit
      If intResponse = vbOK Then
      strComputer = Inputbox("Input the name of the remote computer or just click OK (or hit Enter) for this computer.", "OS Information Tool")
      End If

      If strComputer = "" Then
      strComputer = "."
      End If

      Dim objWmiService : Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\"& strComputer & "\root\cimv2")

      Set colOSes = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
      Set colSMBIOS = objWMIService.ExecQuery("Select * from Win32_SystemEnclosure")

      For Each objOS in colOSes
      For Each objSMBIOS in colSMBIOS

      MsgBox "Computer Name: " & objOS.CSName & vbCrLf _
      & "Caption: " & objOS.Caption & vbCrLf _
      & "Version: " & objOS.Version & vbCrLf _
      & "Build Number: " & objOS.BuildNumber & vbCrLf _
      & "Build Type: " & objOS.BuildType & vbCrLf _
      & "OS Type: " & objOS.OSType & vbCrLf _
      & "Service Pack: " & objOS.ServicePackMajorVersion & vbCrLf _
      & "Serial Number: " & objSMBIOS.SerialNumber, vbInformation + vbOK, "OS Information Tool"

      Next
      Next
       
      #3

        Online Bookmarks Sharing: Share/Bookmark

        Jump to:

        Current active users

        There are 0 members and 1 guests.

        Icon Legend and Permission

        • New Messages
        • No New Messages
        • Hot Topic w/ New Messages
        • Hot Topic w/o New Messages
        • Locked w/ New Messages
        • Locked w/o New Messages
        • Read Message
        • Post New Thread
        • Reply to message
        • Post New Poll
        • Submit Vote
        • Post reward post
        • Delete my own posts
        • Delete my own threads
        • Rate post

        2000-2012 ASPPlayground.NET Forum Version 3.9