部分と全体

知識の巣窟

VBScript |指定された時間後にWindowsをシャットダウンまたはスリープモードにするスクリプト

指定時間後にWindowsをシャットダウンかスリープにするVBscriptをつくってみました。

このスクリプトを使用するには、テキストエディタにコードをコピーし、.vbs拡張子で好きな名前で保存します。

スクリプトが実行されると、カウントダウン時間(秒)をユーザーに問い合わせます。好きな秒数を入力してください。

次に、カウントダウンを開始するかどうか、ユーザーに確認を問い合わせます。

ユーザーが確認すると、カウントダウンが開始され、残り時間が5秒ごとに表示されます。

カウントダウンが完了すると、Windowsはシャットダウンまたはスリープモードに移行します。

シャットダウンかスリープモードかを切り替えるには、以下のコードをコメントアウトするか、適切な行を有効化します。

    ' Shutdown Windows
     'objShell.Run "shutdown.exe -s", 0  'comment out to activate

     ' Sleep Windows
     objShell.Run "rundll32.exe PowrProf.dll,SetSuspendState Sleep", 0  'comment out to activate

5秒毎にPopupでカウントダウンが表示されて時間が来たら自動でオフになります。

注意

このコードを実行すると、Windowsは指定時間後に強制的に実行されます。すべてのプログラムは強制的に終了され、未保存のデータが失われる可能性があります。実行する前に、必要なデータを保存し、他のタスクを完了させてください。

コード全文
'-----------------------------------------------------------------------
'Count Down Timer to Shut Down v.1
'Created by Chonko 2023-01-09
'This code prompts the user for the countdown time, and then 
'displays a confirmation message asking if the user wants to start 
'the countdown. If the user selects Yes, the countdown starts and 
'the remaining time is displayed every 5 seconds. 
'If the user selects No, the countdown is not started and the script ends.
'Note: This code will forcibly shutdown Windows after the entered time. 
'All programs will be forcibly closed and there is a possibility of unsaved 
'data being lost. Before running this code, save any necessary data 
'and complete any other tasks.
'-----------------------------------------------------------------------

Dim objShell, intTime, strPrompt, intResult
Set objShell = CreateObject("WScript.Shell")

' Prompt user for countdown time

strPrompt = "Enter the countdown time (seconds): "
intTime = InputBox(strPrompt)
intTime = CInt(intTime)

' Confirm with the user whether to start the countdown

intResult = objShell.Popup("Start the countdown?", 0, "Confirm", 4 + 32)


' If Yes is selected, start the countdown

If intResult = 6 Then
   While intTime > 0
      objShell.Popup intTime & " seconds until Windows sleeps.", 5, "Countdown", 0
      intTime = intTime - 5
      WScript.Sleep 5000
   Wend

     ' Adjust code as you like below:

     ' Shutdown Windows
     'objShell.Run "shutdown.exe -s", 0  'comment out to activate

     ' Sleep Windows
     objShell.Run "rundll32.exe PowrProf.dll,SetSuspendState Sleep", 0  'comment out to activate
End If

Set objShell = Nothing