创建桌面快捷方式的vbscript,当用户单击该快捷方式时,提示用户关闭

创建桌面快捷方式的vbscript,当用户单击该快捷方式时,提示用户关闭,vbscript,Vbscript,我目前正在编写一个脚本,该脚本将创建桌面快捷方式,当单击时,将提示用户是否要关闭。当我测试脚本时,它决定自动运行。有没有办法将这两个功能分开(创建快捷方式和提示用户关机?) 这是我的密码: dim shellApp , answer Set shellApp = CreateObject("Shell.Application") Set wshObject = WScript.CreateObject("WScript.Shell") desktopFolder = wshObject.Spec

我目前正在编写一个脚本,该脚本将创建桌面快捷方式,当单击时,将提示用户是否要关闭。当我测试脚本时,它决定自动运行。有没有办法将这两个功能分开(创建快捷方式和提示用户关机?)

这是我的密码:

dim shellApp , answer
Set shellApp = CreateObject("Shell.Application")
Set wshObject = WScript.CreateObject("WScript.Shell")
desktopFolder = wshObject.SpecialFolders("Desktop")
Set myShortcut = wshObject.CreateShortcut(desktopFolder & "\\Shut Down.lnk" )
myShortcut.TargetPath ="C:\Users\Grant\Scripts\ChristianGrantProblem1.vbs"
myShortcut.Save()
answer = MsgBox("The Turn off Computer dialog will be opened, if you wish to shut down, continue.", 1 )
If answer = 1 then ' User clicked on OK
Initiate_Logoff()
End If

此vbscript可以在桌面上创建一个快捷方式,询问您是否要关闭计算机

Option Explicit
Dim MyScriptPath 
MyScriptPath = WScript.ScriptFullName
Call Shortcut(MyScriptPath,"Shutdown the computer")
Call AskQuestion()
'**********************************************************************************************
Sub Shortcut(PathApplication,Name)
    Dim objShell,DesktopPath,objShortCut,MyTab
    Set objShell = CreateObject("WScript.Shell")
    MyTab = Split(PathApplication,"\")
    If Name = "" Then
        Name = MyTab(UBound(MyTab))
    End if
    DesktopPath = objShell.SpecialFolders("Desktop")
    Set objShortCut = objShell.CreateShortcut(DesktopPath & "\" & Name & ".lnk")
    objShortCut.TargetPath = Dblquote(PathApplication)
    ObjShortCut.IconLocation = "%SystemRoot%\system32\SHELL32.dll,-28"
    objShortCut.Save
End Sub
'**********************************************************************************************
Sub AskQuestion()
    Dim Question,Msg,Title
    Title = "Shutdown the computer"
    Msg = "Are you sure to shutdown the computer now ?"& Vbcr &_
    "If yes, then click [YES] button "& Vbcr &_
    "If not, then click [NO] button"
    Question = MsgBox (Msg,VbYesNo+VbQuestion,Title)
    If Question = VbYes then
        Call Run_Shutdown(30)
    else
        WScript.Quit()
    End if
End Sub
'**********************************************************************************************
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'**********************************************************************************************
Sub Run_Shutdown(N)
    Dim ws,Command,Execution
    Set ws = CreateObject("wscript.Shell")
    Command = "Cmd /c Shutdown -s -t "& N &" -c "& DblQuote("Save your work because your PC will shut down in "& N &" seconds")
    Execution = ws.run(Command,0,True)
End sub
'**********************************************************************************************

为什么不使用两个脚本——一个创建快捷方式,另一个作为快捷方式的目标?另一种方法是让脚本检查快捷方式是否已经存在。我需要做的是编写一个脚本,在用户桌面上创建一个可用于关闭计算机的快捷方式,当用户单击创建的图标时,应询问他们是否真的要关闭计算机,并给予他们取消关闭或继续关闭的选项。如果您不能执行双脚本解决方案(例如,因为这是家庭作业,需要编写单个脚本)自然的做法是让脚本在第一次运行时创建自身的快捷方式,然后在后续运行时询问关机。方法是测试是否已经创建了快捷方式。如果没有——创建它。如果是这样的话——询问关于关闭的问题。您的代码不需要两个函数,而是需要一个if-then-else结构。好的,谢谢您的帮助。我看看我能做什么。