lapucca
asked on
Need help with VB Script coding
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
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
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
~bp
ASKER
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
ErrScreen.jpg
ASKER
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.
Thank you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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. https://www.experts-exchange.com/questions/28404636/Why-my-while-loop-not-working.html
~bp