创建创建快捷方式然后重新启动计算机的脚本 [英] Creating a script that creates a shortcut and then restarts the computer

查看:118
本文介绍了创建创建快捷方式然后重新启动计算机的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码需要帮助.我正在尝试制作一个执行以下两项操作的脚本:首先,它在用户桌面上创建快捷方式图标.其次,当用户双击图标时,会出现一个框,询问他们是否要重新启动计算机,从而为他们提供了单击确定"以重新启动到取消"以取消命令的选项.将脚本输入命令提示符后,它仅执行重新启动计算机选项.任何帮助将不胜感激.这是我的脚本:

I need help with my code. I am trying to make a script that does 2 things: First, it creates shortcut icon on the users desktop. Second, when the user double clicks the icon a box appears asking if they want to restart their computer giving them the option to click OK to restart to CANCEL to cancel the command. When entering the script into the command prompt it just executes the restart computer option. Any help would be greatly appreciated. Here is my script:

    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

推荐答案

这是怎么回事:

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

这篇关于创建创建快捷方式然后重新启动计算机的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆