タスクスケジュラーでプログラム起動して、1時間後にプログラムを終了させる
PowerShellスクリプトでDropboxを起動1時間後に終了させる
💡 ポイント
-
スクリプトのウィンドウは最小化されて 邪魔にならない
-
Dropboxは1時間後に安全に終了
-
終了確認のポップアップウィンドウが出るが強制的に閉じる
タスクスケジュラーに登録
Dropbox_Start_Kill.ps1
# --- PowerShellを最小化 ---
$hwnd = (Get-Process -Id $PID).MainWindowHandle
if ($hwnd -ne 0) {
$sig = @"
using System;
using System.Text;
using System.Runtime.InteropServices;
public static class WinAPI {
[DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")] public static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll", CharSet=CharSet.Auto)] public static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll", CharSet=CharSet.Auto)] public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")] public static extern bool PostMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
}
"@
Add-Type $sig
[WinAPI]::ShowWindow($hwnd, 2) # 2 = SW_MINIMIZE
}
# --- Dropboxを起動(/homeでExplorerは開かない) ---
$dropboxPath = "C:\Program Files (x86)\Dropbox\Client\Dropbox.exe"
$proc = Start-Process $dropboxPath -ArgumentList '/home' -PassThru
# --- 必要時間だけ待機(60分 = 3600秒。) ---
Start-Sleep -Seconds 3600
# --- Kill /T で指定されたプロセスおよびそれによって開始されたすべての子プロセスを終了、さらに /F オプションで強制終了して再起動ダイアログも閉じます ---
taskkill /IM Dropbox.exe /T /F
💡 タスクスケジュラー登録方法
-
プログラム/スクリプト に PowerShellの実行ファイル を指定
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
-
引数の追加 にスクリプトのパスと必要なオプションを指定
-ExecutionPolicy Bypass -File "C:\bat\Dropbox_Start_Kill.ps1"
これでタスクスケジューラーから .ps1 を実行可能になります✨
コメント
コメントを投稿