我正在编写一个脚本,通过 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
那些在控制台中运行正常但在任务计划程序中的脚本中无法正常运行的命令。
我看不出问题所在,那么为什么它不起作用呢?