Solved

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

Posted on 2012-03-14
5
1,275 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
  • 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 333 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 333 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 167 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

777 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