LockWorkStation 無法透過任務規劃程式在 Powershell 中運作

LockWorkStation 無法透過任務規劃程式在 Powershell 中運作

我正在編寫一個腳本,該腳本接收來自透過 IFTTT 橋接的 Amazon Echo 的命令。

流程如下:

  • 我要求 Alexa 觸發“X”
  • IFTTT 將其發送到 Dropbox Applet
  • Dropbox 在目錄中寫入名為「X」的文字文件
  • 任務計劃 Powershell 腳本每 5 秒循環一次以偵測該目錄中的任何文件
  • 腳本根據檔案名稱執行命令

這是該腳本的一個簡短範例:

$SearchDirectory = "C:\Users\Username\Dropbox\IFTTT"
$SleepTime = 5
$Status = "none"

Remove-Item -Path "$SearchDirectory\shutdown.txt" -Force -ErrorAction SilentlyContinue
Remove-Item -Path "$SearchDirectory\restart.txt" -Force -ErrorAction SilentlyContinue
Remove-Item -Path "$SearchDirectory\sleep.txt" -Force -ErrorAction SilentlyContinue
Remove-Item -Path "$SearchDirectory\lock.txt" -Force -ErrorAction SilentlyContinue

While ($True) {
  Do {
    Start-Sleep -Seconds $SleepTime

    if (Test-Path -Path "$SearchDirectory\shutdown.txt") { $Status = "shutdown" }
    if (Test-Path -Path "$SearchDirectory\restart.txt") { $Status = "restart" }
    if (Test-Path -Path "$SearchDirectory\sleep.txt") { $Status = "sleep" }
    if (Test-Path -Path "$SearchDirectory\lock.txt") { $Status = "lock" }
  }
  Until ($Status -ne "none")

  switch ($Status) {
    "shutdown" { Remove-Item -Path "$SearchDirectory\shutdown.txt"; Stop-Computer -Force; break }
    "restart" { Remove-Item -Path "$SearchDirectory\restart.txt"; Restart-Computer -Force; break }
    "sleep" { Remove-Item -Path "$SearchDirectory\sleep.txt"; Suspend-Computer; break}
    "lock" { Remove-Item -Path "$SearchDirectory\lock.txt"; Invoke-Command {rundll32.exe user32.dll,LockWorkStation}; break}
    "none" { break }
  }

  $Status = "none"
}

上面的所有命令都工作正常,除了rundll32.exe user32.dll,LockWorkStation在控制台中似乎工作正常但不能通過任務計劃程序中的腳本工作的命令。

我看不到問題所在,為什麼它不起作用?

相關內容