?
Solved

Need help with VB Script coding

Posted on 2014-04-03
7
Medium Priority
?
480 Views
Last Modified: 2014-04-03
I'm a C# programmer and haven't done vbscript for years hence needing help here.  I have a script that reboots the pc .  It pops up a dialog that has a Yes and a No buttons that does the following.  
1.  I have a error in the syntax which I uploaded the error screen.  I can't see what the error is so appreciate help with this.  I'm using NotePad++ to edit this file.
2.  Would the code in Sub RebootPC work?  I got this code after googling online.
3.  This program will be run on Windows 7 PCs
Thank you.
 
If they click Yes then restart, if they click No, delay for 10 minutes, then prompt again.  
 
If they hit No a second time, delay 10 minutes, and then prompt again.
 
If they hit No a third time, delay 10 minutes and then prompt again.
 
On the 4th time, give them with a message that the computer will restart in 5 minutes with a count down, and then restart the box.
pcReboot.vbs
vb-err.jpg
0
Comment
Question by:lapucca
[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
  • 4
  • 3
7 Comments
 
LVL 56

Expert Comment

by:Bill Prew
ID: 39976190
The error message doesn't make sense with that code, since there is no line 49 in it, are you sure you pasted the entire script you ran to produce that error?

~bp
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 39976234
Found several syntax errors, here are some changes that should get past those.  Haven't checked the actual logic, didn't want to reboot at the moment...

'Give users 60 seconds to respond
const TIMEOUT = 60
dim numOfDelays
numOfDelays = 0
strComputer = "."
Set objShell = WScript.CreateObject("WScript.Shell")

SUB RebootPc()	
			Set objFSO = CreateObject("Scripting.FileSystemObject")
			Set objWMIService = GetObject("winmgmts:" _
				& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
			Set colProcessList = objWMIService.ExecQuery _
				("SELECT * FROM Win32_Process WHERE Name = 'explorer.exe'")
			For Each objProcess in colProcessList
				objProcess.Terminate()
			Next
			Set OpSysSet = GetObject("winmgmts:{(Shutdown)}//./root/cimv2").ExecQuery("select * from Win32_OperatingSystem where Primary=true")
			for Each OpSys in OpSysSet
				OpSys.Reboot( )
			NEXT
End SUB
	
SUB DelayOrReboot()
		if(numOfDelays < 3) then
			WScript.Sleep(1000*60*10)
			numOfDelays = numOfDelays + 1
		else 
			Call RebootPc()	
		end if
End SUB	

do while iRetVal < 3
	iRetVal = objShell.Popup("Your computer will restart momentarily.  To restart immediately, click Yes. You can delay the reboot by 10 minutes each time you click the No button with a maximum of 3 delays.", TIMEOUT, "IT Maintenance", vbExclamation+vbYesNo+vbDefaultButton3)

	Select Case iRetVal
	 Case vbYes 
		Call RebootPc()	
	 Case vbNo
		Call DelayOrReboot()
	 Case Else	
		Call DelayOrReboot()
	End Select
Loop

Open in new window

~bp
0
 

Author Comment

by:lapucca
ID: 39976239
Yes, you're absolutely correct!  I removed company heaader comments so that doesn't match up.  Please see the new screen shot.  Thank you.
ErrScreen.jpg
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 56

Expert Comment

by:Bill Prew
ID: 39976306
See my post right above yours...

http:#a39976234

~bp
0
 

Author Comment

by:lapucca
ID: 39976357
Great!  that does get rid of the error and the program is running.  I see the change in "call" sub.  Anything else you change to fix this?   Doesn't seem like change at line 33, iRetval = ....   Or is there changes needed?
Thank you.
0
 
LVL 56

Accepted Solution

by:
Bill Prew earned 2000 total points
ID: 39976424
There were several changes.  

Needed parens around the parms on the .Popup call.

Changed several "sub" to "call" where you were calling (rather than defining) subs.

Added "do" before the "while".

~bp
0
 

Author Closing Comment

by:lapucca
ID: 39976439
I google for VB script syntax to put this together and those were what I found.  Thank you so much for the help.  I have another problem with the same code and appreciate it if you can help out.  http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28404636.html
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

762 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