Solved

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

Posted on 2012-03-14
5
1,290 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

828 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