Solved

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

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This article will show, step by step, how to integrate R code into a R Sweave document
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…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now