Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vscript with prompt to restart after 5 minutes

Status
Not open for further replies.

Briandr

MIS
Jul 11, 2003
177
US
Hi,

I found this vbscript on Symantec Connect. It prompts the user to restart after a installation. Can this be tweaked so that it will continually prompt the user every 5 minutes to restart until they do so?

Dim WSHShell, fso, prodir, strKeyPath
Dim path, i, dpath
Dim strComputer
Dim strDirectory, sScriptDir
'CHANGE THE APPLICATION NAME HERE
Dim sTitle: sTitle = "My Application"
Dim sLogHeader : sLogHeader = sTitle & VbCrLf

strComputer = "."

Set objShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

'GET CURRENT/SOURCE FOLDER PATH
sScriptDir = fso.GetParentFolderName(WScript.ScriptFullName)
If Right(sScriptDir,1) <> "\" Then sScriptDir = sScriptDir & "\"

'CHNAGE THE NAME OF THE EXECUTABLE HERE Ex: 7-zip.msi
If fso.FileExists(sScriptDir & "Setup.msi") Then
'SPECIFY COMMANDLINE PARAMETERS HERE Ex: /quiet /norestart
path = Chr(34) & sScriptDir & "Setup.msi" & Chr(34) & " /QB!"

i = 0
i = objShell.Run(path, 1 ,True)

If (i = 0) Or (i = 3010) Then
'WRITE THE RESULT TO EVENT LOG
objShell.LogEvent vbLogSuccess, sLogHeader & "Installation completed successfully." & VbCrLf & _
"Exit code: " & i

'DISPLAY MESSAGE TO THE USER WITH TIMEOUT OF 60 MINUTES
'YOU CAN CHANGE THE TIMEOUT ACCORDING TO YOUR REQUIREMENT HERE [60 mins = 3600 secs]
objShell.Popup sTitle & " installation completed successfully!" & VbCrLf & _
"You must reboot your computer as soon as possible." & VbCrLf & _
3600, sTitle, vbExclamation+vbOKOnly+vbSystemModal

'SHUTDOWN MESSAGE BOX WITH 60 SECONDS TIMEOUT [IF THE USER IS NOT RESPONDING TO THE ABOVE MESSAGE]
'YOU CAN CHANGE THE TIMEOUT ACCORDING TO YOUR REQUIREMENT HERE [60]
strShutdown = "shutdown -s -t 60 -f -m \\" & strComputer
objShell.Run strShutdown

Set objShell = Nothing
Set fso = Nothing
WScript.Quit(i)
Else
objShell.LogEvent vbLogError, sLogHeader & _
"Installation returned failure code: " & i
WScript.Quit(i)
End If
Else
'UNABLE TO FIND THE SOURCE FILE
WScript.Quit (1)
End If

Thanks guys.
 
Code:
...
[COLOR=#CC0000]do[/color]
   'DISPLAY MESSAGE TO THE USER WITH TIMEOUT OF 60 MINUTES	
   'YOU CAN CHANGE THE TIMEOUT ACCORDING TO YOUR REQUIREMENT HERE [60 mins = 3600 secs]
   [COLOR=#CC0000]varRestart =[/color] objShell.Popup [COLOR=#CC0000]([/color]sTitle & " installation completed successfully!" & VbCrLf & _
   "You must reboot your computer as soon as possible. Do it now?" & VbCrLf & _
   , 3600, sTitle, vbExclamation+[COLOR=#CC0000]vbYesNo[/color]+vbSystemModal[COLOR=#CC0000])[/color]

   [COLOR=#CC0000]if (varRestart = vbYes) then[/color]
      'SHUTDOWN MESSAGE BOX WITH 60 SECONDS TIMEOUT [IF THE USER IS NOT RESPONDING TO THE ABOVE MESSAGE]
      'YOU CAN CHANGE THE TIMEOUT ACCORDING TO YOUR REQUIREMENT HERE [60]
      strShutdown = "shutdown -s -t 60 -f -m \\" & strComputer 
      objShell.Run strShutdown 
   [COLOR=#CC0000]end if[/color]
   [COLOR=#CC0000]wscript.sleep 300000 '300000ms = 5min[/color]
[COLOR=#CC0000]loop[/color] 
...

-Geates

 
Hi Geates,

I will test out and see how it works. Altiris is our delivery vehicle for the app and the script. Thank you again for offering the tweaking.
 
I feel as thought I should offer a brief explanation.

Wrap the code you want to execute repetitively in a do..loop. Modify the [tt]objShell.Popup[/tt] to return the value of the button clicked from the prompt. Use the returned value to drive the if..then statement that resets the computer. If the if..then statement is true, execute the restart command. If it's false, use a delay to control the speed of the loop; in this case, 5 minutes.

-Geates

 
Hi Geates,

There is some error logging I just assume not use, maybe later but for now I just want to get rid of it. Are the lines I marked in red the ones I want to remove?

Dim WSHShell, fso, prodir, strKeyPath
Dim path, i, dpath
Dim strComputer
Dim strDirectory, sScriptDir
'CHANGE THE APPLICATION NAME HERE
Dim sTitle: sTitle = "My Application"
Dim sLogHeader : sLogHeader = sTitle & VbCrLf

strComputer = "."

Set objShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

'GET CURRENT/SOURCE FOLDER PATH
sScriptDir = fso.GetParentFolderName(WScript.ScriptFullName)
If Right(sScriptDir,1) <> "\" Then sScriptDir = sScriptDir & "\"

'CHNAGE THE NAME OF THE EXECUTABLE HERE Ex: 7-zip.msi
If fso.FileExists(sScriptDir & "Setup.msi") Then
'SPECIFY COMMANDLINE PARAMETERS HERE Ex: /quiet /norestart
path = Chr(34) & sScriptDir & "Setup.msi" & Chr(34) & " /QB!"

i = 0
i = objShell.Run(path, 1 ,True)

If (i = 0) Or (i = 3010) Then
'WRITE THE RESULT TO EVENT LOG
objShell.LogEvent vbLogSuccess, sLogHeader & "Installation completed successfully." & VbCrLf & _
"Exit code: " & i


'DISPLAY MESSAGE TO THE USER WITH TIMEOUT OF 60 MINUTES
'YOU CAN CHANGE THE TIMEOUT ACCORDING TO YOUR REQUIREMENT HERE [60 mins = 3600 secs]
objShell.Popup sTitle & " installation completed successfully!" & VbCrLf & _
"You must reboot your computer as soon as possible." & VbCrLf & _
3600, sTitle, vbExclamation+vbOKOnly+vbSystemModal

'SHUTDOWN MESSAGE BOX WITH 60 SECONDS TIMEOUT [IF THE USER IS NOT RESPONDING TO THE ABOVE MESSAGE]
'YOU CAN CHANGE THE TIMEOUT ACCORDING TO YOUR REQUIREMENT HERE [60]
strShutdown = "shutdown -s -t 60 -f -m \\" & strComputer
objShell.Run strShutdown

Set objShell = Nothing
Set fso = Nothing
WScript.Quit(i)
Else
objShell.LogEvent vbLogError, sLogHeader & _
"Installation returned failure code: " & i
WScript.Quit(i)
End If
Else
'UNABLE TO FIND THE SOURCE FILE
WScript.Quit (1)
End If


Thanks.
 
Mostly, but keep lowest level if..then condition. It should resemble ...

Code:
If (i = 0) Or (i = 3010) Then
   do
      'DISPLAY MESSAGE TO THE USER WITH TIMEOUT OF 60 MINUTES	
      'YOU CAN CHANGE THE TIMEOUT ACCORDING TO YOUR REQUIREMENT HERE [60 mins = 3600 secs]
      varRestart = objShell.Popup (sTitle & " installation completed successfully!" & VbCrLf & "You must reboot your computer as soon as possible. Do it now?", 3600, sTitle, vbExclamation+vbYesNo+vbSystemModal)
      if (varRestart = vbYes) then
         'SHUTDOWN MESSAGE BOX WITH 60 SECONDS TIMEOUT [IF THE USER IS NOT RESPONDING TO THE ABOVE MESSAGE]
         'YOU CAN CHANGE THE TIMEOUT ACCORDING TO YOUR REQUIREMENT HERE [60]
         strShutdown = "shutdown -s -t 60 -f -m \\" & strComputer 
         objShell.Run strShutdown 
      end if
      wscript.sleep 300000 '300000ms = 5min
   loop 
End If

-Geates

 
I think I got this right. If not can you let me know if I truncated one too many lines. I did get rid of the comments lines because I could follow the logic without them.

Dim WSHShell, fso, prodir, strKeyPath
Dim path, i, dpath
Dim strComputer
Dim strDirectory, sScriptDir

Dim sTitle: sTitle = "My Application"
Dim sLogHeader : sLogHeader = sTitle & VbCrLf

strComputer = "."

Set objShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

sScriptDir = fso.GetParentFolderName(WScript.ScriptFullName)
If Right(sScriptDir,1) <> "\" Then sScriptDir = sScriptDir & "\"


If fso.FileExists(sScriptDir & "Setup.msi") Then

path = Chr(34) & sScriptDir & "Setup.msi" & Chr(34) & " /QB!"

i = 0
i = objShell.Run(path, 1 ,True)

If (i = 0) Or (i = 3010) Then
do

varRestart = objShell.Popup (sTitle & " installation completed successfully!" & VbCrLf & "You must reboot your computer as soon as possible. Do it now?", 3600, sTitle, vbExclamation+vbYesNo+vbSystemModal)
if (varRestart = vbYes) then

strShutdown = "shutdown -s -t 60 -f -m \\" & strComputer
objShell.Run strShutdown
end if
wscript.sleep 300000 '300000ms = 5min
loop
End If

Thanks for the help. I might have more questions. This was kinda of a rush job to pull this together.[thumbsup2]

 
Hi,

I saw after posting I needed another End If and now the code tests fine.

Another question. How do I set the directory? in the script? Is my example correct below?

sScriptDir = fso.GetParentFolderName(WScript.ScriptFullName)
If Right(sScriptDir,1) <> "\" Then sScriptDir = sScriptDir & "C:\Temp\"

Thanks.
 
Hi Geates

So if one were to believe the coder of the script all the first line is doing is getting the current folder path?

i.e., 'GET CURRENT/SOURCE FOLDER PATH

The second line appears to be setting the source directory to C:\Temp\ which is all I should really need prior to installing the MSI.

If Right(sScriptDir,1) <> "\" Then sScriptDir = sScriptDir & "C:\Temp\"

Just trying to understand the coder's thought a little better. I do realize sometimes good code does need cleaning.

Thanks.



 
I will figure out how to edit my posts. It may be staring right at me. Something I remember and not fondly was the darn

open file - security warning

I am on a W7 computer and I am logged with admin rights. I did try to explicitly login as admin. So how do I suppress this? Is this article from 2008 still valid. It does indicate it addresses this issue on XP SP2 computers.


Any other ideas?
 
The second line appears to be setting the source directory to C:\Temp\ which is all I should really need prior to installing the MSI.
If Right(sScriptDir,1) <> "\" Then sScriptDir = sScriptDir & "C:\Temp\"

Wrong. This line of code checks to see if the right-most single character of sScriptDir is not "\". If this is true, then append "C:\Temp\" to the end.

Let's say that sScriptDir = "C:\program files". After the above line of code runs, sScriptDir = "C:\program filesC:\Temp\". That's not a directory - no wonder you are getting that error.

-Geates

The link in my signature is a quick reference to VBScript which will help interpret the author's code

 
Actually I was not getting a error with and without it, but thanks for the explanation. As far as the open file - security warning the instructions in the link I posted are still valid.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top