3

I am currently working on a script that will create a shortcut to the desktop and when clicked on, will prompt a user if they want to shut down. When I test my script, it just decides to run automatically. Is there a way I can maybe seperate the two functions (creating shortcut and prompting user for shutdown?).

Here is my code:

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
Chris G
  • 31
  • 2
  • Why not have 2 scripts -- one to create the shortcut and one to be the target of the shortcut? The alternative is to have your script check to see if the shortcut is already there. – John Coleman Nov 29 '15 at 22:56
  • What I need to do is to write a script that will create a shortcut on the user's desktop that can be used to shutdown the computer and when the user clicks on the icon created , they should be asked if they really want to shut down the computer and given the option to cancel the shutdown or proceed with the shutdown. – Chris G Nov 29 '15 at 23:15
  • If you can't do a two script solution (e.g. because it is homework and writing a single script is a requirement) the natural thing to do is to have the script create a shortcut to itself the first time it is run and then to ask about shut down on subsequent runs. The way to do this is to test if the shortcut has already been created. If not -- create it. If so -- ask about shut down. Rather than having two functions you need an if-then-else structure to your code. – John Coleman Nov 29 '15 at 23:33
  • Ok, thank you for your help. I'll see what I can do. – Chris G Nov 29 '15 at 23:48

1 Answers1

2

This vbscript can create a shortcut on your desktop asking you if you want to shutdown the computer or not.

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
'**********************************************************************************************
Hackoo
  • 18,337
  • 3
  • 40
  • 70