سؤال

I have a Script that will restart a Windows computer in Safe Mode.

This vbscript calls a BAT file that prompts a UAC elevation, and restarts the computer in safe mode.

Any suggestions on how to optimize this script? And can this work in Windows 8?

[code]Dim oShell, retCode
Set oShell = WScript.CreateObject("WScript.Shell")
Set objShell = CreateObject("Wscript.Shell")

retCode = oShell.Popup("Do you want to restart your computer in Safe Mode", 0, "Restart In SafeMode", 4 + 48)

Select Case retCode
case 6, -1
objShell.Run "elevaterestart.bat"
case 7
WScript.quit(1) 'No was chosen
End Select

Update: I updated the script, as suggested and added an Operation Canceled popup if a user selects no

Dim oShell, retCode, MsgBox
Set oShell = WScript.CreateObject("WScript.Shell")
Set objShell = CreateObject("Wscript.Shell")

retCode = oShell.popup("Do you want to restart your computer in Safe Mode", 0, "Restart                In Safe Mode", 4 + 48 + 256)


If retCode = 6 Or retCode = -1 Then
objShell.Run "elevaterestart.bat"
Else
oShell.popup "Operation Canceled", 0, "Restart In Safe Mode", 0 + 64 + 0
End If`
هل كانت مفيدة؟

المحلول

It seems that the Set objShell line is not required as your objShell variable is never used and is redundant with oShell.

VBScripts will quit without the wscript.quit(ExitCode) call when the end of the file is reached, but if you're calling this from something else, the 1 exit code will allow you to know that the user said they did not want to reboot (the default exit code when the end of the file is reached without errors is 0).

I wouldn't have used a case statement - that's generally only used style-wise if you're selecting from a series of options. More likely I'd have coded the last section like this, but this doesn't change the functionality at all.

If retCode = 6 Or retCode = -1 Then
    objShell.Run "elevaterestart.bat"
Else
    WScript.quit(1)
End If
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top