创建一个创建快捷方式的脚本,然后重新启动计算机

时间:2014-11-26 12:54:25

标签: vbscript shortcut desktop-shortcut

我的代码需要帮助。我正在尝试制作一个可以完成两件事的脚本:首先,它在用户桌面上创建快捷方式图标。其次,当用户双击该图标时,会出现一个框,询问他们是否要重新启动计算机,然后选择单击“确定”重新启动以取消取消该命令。将脚本输入命令提示符时,它只执行重新启动计算机选项。任何帮助将不胜感激。这是我的剧本:

    Dim answer


' ********* Main processing section **********

' Verify that the user wants to open the Turn Off Computer dialog
    Set wshObject = WScript.CreateObject("WScript.Shell")
    desktopFolder = wshObject.SpecialFolders("Desktop")
    Set myShortcut = wshObject.CreateShortcut(desktopFolder & "\\Shortcut.lnk")
    myShortcut.TargetPath = "%windir%\Shortcut.exe"
    myShortcut.Save()

    answer = MsgBox("The Turn Off Computer dialog will be opened.", 1, "Turn off Computer Script!")
    If answer = 1 then  ' User clicked on OK
    Initiate_Logoff()
    End if

' *********** Procedures go here *************

' Open the Windows Turn Off Computer dialog
    Function Initiate_Logoff()
    shellApp.ShutdownWindows
    End Function

1 个答案:

答案 0 :(得分:0)

这个怎么样:

Option Explicit

'create a desktop shortcut
Dim shl : Set shl = CreateObject("WScript.Shell")
Dim scut : Set scut = shl.CreateShortcut(shl.ExpandEnvironmentStrings("%USERPROFILE%") & "\Desktop\Shortcut.lnk")
scut.TargetPath = "%windir%\shortcut.exe"
scut.Save

Dim cmd : cmd = "shutdown.exe /r /t 1" 'this command restarts the machine

'if the script is being run by cscript (command line)
If InStr(WScript.FullName, "cscript") > 0 Then
  shl.Exec cmd
Else
  'else ask the user
  If MsgBox("Restart Now?", vbQuestion + vbOKCancel, "Title") = vbOK Then
    shl.Exec cmd
  End If
End If

WScript.Quit