Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

HTA VBScript - Could not complete the operation due to error 80041001

Posted on 2012-03-14
5
Medium Priority
?
1,343 Views
Last Modified: 2012-03-16
Hi,

I have created a basic HTA VBScript which you type in a Computer Name and it's Pinged using WMI

I have an issue where depending on what name is entered it crashes

Examples of my issue

Type In following computer names:

PC12345 - Everything is ok
123445 - Script crashes with error 80041001

Anytime the PC name starts with a number is crashes.

Although I don't have any computer where the name starts with a number, I want it to handle this better incase of user error typing in a computer name

Here is some code.

<HTML>

<!-- ---------------------------------------------------------------------------

Script Name:    Ping Computer
                   
---------------------------------------------------------------------------- -->

  <HEAD>

    <TITLE>
    
    Ping Computer
    
    </TITLE>

    <!-- Helpful HTA Information at Microsoft
    http://msdn.microsoft.com/en-us/library/ms536495(v=vs.85).aspx -->
    
    <META http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <META name="description" content="Ping Computer">
    
    <HTA:APPLICATION 
      ID="objPing Computer" 
      APPLICATIONNAME="Ping Computer"
      SCROLL="No"
      SINGLEINSTANCE="yes"
      WINDOWSTATE="normal"
      BORDER="dialog"
      BORDERSTYLE="normal"
      CAPTION="Ping Computer
      CONTEXTMENU="no"
      MAXIMIZEBUTTON="no"
      MINIMIZEBUTTON="yes"
      NAVIGABLE="yes"
      SYSMENU="yes"
      VERSION="0.1"  
     >
     
     <SCRIPT language="VBScript">
    
        intLeft = window.screenLeft
        intTop = window.screenTop
        
        window.ResizeTo 175,100
    
      </SCRIPT>

  </HEAD>

  <SCRIPT type="text/vbscript">
  
' ------------------------------------------------------------------------------
  
    ' Function: Machine Reachable
    
    Function Reachable()
    
      strComputer=InputBox("Enter PC Name to Ping","Userinput")
     
      wmiQuery = "Select * From Win32_PingStatus Where Address = '" & strComputer & "'"
    
      Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
      Set objPing = objWMIService.ExecQuery(wmiQuery)
      
      MsgBox "I am about to ping: " & strComputer
        
      For Each objStatus In objPing
        
        If IsNull(objStatus.StatusCode) Or objStatus.Statuscode<>0 Then
        
          ' If computer is unreachable, return false
          
          Reachable = False
          MsgBox "Machine Reachable = FALSE"
        
        Else
        
          ' If computer is reachable, return true
          
          Reachable = True
          MsgBox "Machine Reachable = TRUE"
        
        End If
  			
      Next
  
    End Function
    
' ------------------------------------------------------------------------------

  </SCRIPT>

  <BODY>

    <button onclick="Reachable()">Ping Computer</button>

    <span id="mySpan"></span>

  </BODY>

</HTML>

Open in new window


I realise that PC Names cannot only contain numbers

How can I validate if the name is a number only get the user to re enter?

Thank you
0
Comment
Question by:bl460c
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 

Author Comment

by:bl460c
ID: 37723327
According to here

http://support.microsoft.com/kb/909264

These characters are not allowed

backslash (\)
slash mark (/)
colon (:)
asterisk (*)
question mark (?)
quotation mark (")
less than sign (<)
greater than sign (>)
vertical bar (|)


Minimum name length
1 character.
Maximum name length
15 characters.

Cannot contain all numbers

I suppose I could either do an if statement pseudo

IF ComputerName contains any of those characters then ask for a new computer name

or seeing as I have an active directory environment, I am thinking I should search ad for that computer and if it exists continues, else ask for another name.
0
 
LVL 20

Assisted Solution

by:ltlbearand3
ltlbearand3 earned 1332 total points
ID: 37723331
You could approach this in three (or maybe more) different ways.

(1)Force the user to enter a valid name and keep prompting until they do:
Change this line:
strComputer=InputBox("Enter PC Name to Ping","Userinput")

Open in new window

to This:
strComputer=InputBox("Enter PC Name to Ping","Userinput")
Do While len(strComputer)=0 or isnumeric(strComputer)
    strComputer=InputBox("Enter PC Name to Ping1","Userinput")	
Loop

Open in new window


(2)Exit script if a bad name is entered:
Change this line:
strComputer=InputBox("Enter PC Name to Ping","Userinput")

Open in new window

to This:
strComputer=InputBox("Enter PC Name to Ping","Userinput")
If isnumeric(strComptuer) Then msgbox ("Invalid machine name entered.  Exiting script")
Wscript.Quit

Open in new window


(3)Keep prompting until a valid name has been entered, but warn them why you are prompting again
Change this line:
strComputer=InputBox("Enter PC Name to Ping","Userinput")

Open in new window

to This:
strComputer=InputBox("Enter PC Name to Ping","Userinput")
Do While len(strComputer)=0 
    strComputer=InputBox("Enter PC Name to Ping2","Userinput2")	
	If isnumeric(strComputer) Then 
		msgbox ("Invalid machine name entered.  Please re-enter")
		strComputer = ""
	End if
Loop

Open in new window


You may want to add a way for them to exit and not get stuck in the loops on 1 & 3

-Bear
0
 
LVL 20

Assisted Solution

by:ltlbearand3
ltlbearand3 earned 1332 total points
ID: 37723335
If you want to make this script more robust to handle all exceptions mentioned by bl460c, we could add a RegEx call to check for those.

-Bear
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 668 total points
ID: 37723525
Hi, I assume it crashes on this line:
Set objPing = objWMIService.ExecQuery(wmiQuery)

so you can also ignore errors, and detect if one occurred, and return false for your function.

Regards,

Rob.

	Function Reachable()
		strComputer=InputBox("Enter PC Name to Ping","Userinput")
		wmiQuery = "Select * From Win32_PingStatus Where Address = '" & strComputer & "'"
		Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
		MsgBox "I am about to ping: " & strComputer
		On Error Resume Next
		Set objPing = objWMIService.ExecQuery(wmiQuery)
		If Err.Number = 0 Then
			For Each objStatus In objPing
				If IsNull(objStatus.StatusCode) Or objStatus.Statuscode<>0 Then
					' If computer is unreachable, return false
					Reachable = False
					MsgBox "Machine Reachable = FALSE"
				Else
					' If computer is reachable, return True
					Reachable = True
					MsgBox "Machine Reachable = TRUE"
				End If
			Next
		Else
			' If computer is unreachable, return false
			Reachable = False
			MsgBox "Machine Reachable = FALSE"
		End If
		Err.Clear
		On Error GoTo 0
	End Function

Open in new window

0
 

Author Closing Comment

by:bl460c
ID: 37731939
Thank you all.

I will go with the easy way out (continue on error)

however to make it more robust In the future I will look into RegEx

Cheers.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

647 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question