我有这个powershell脚本,当在某个目录中创建文件时会触发一些指令。 一切运作良好,但在动作完成后我无法关闭powershell。 我怎么能这样做?
### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\Users\Autostrade\CartellaSFTP"
$watcher.Filter = "*.*"
$watcher.IncludeSubdirectories = $false
$watcher.EnableRaisingEvents = $true
$uscita = 0
### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
$action = { $path = $Event.SourceEventArgs.FullPath
$changeType = $Event.SourceEventArgs.ChangeType
$logline = "$(Get-Date), $changeType, $path"
Add-content "C:\Users\Autostrade\PHP Script Dati Telepass\log.txt" -value $logline
# Set up references to executable and script
$PhpExe = "C:\PHP\php.exe"
$PhpFile = "C:\Users\Autostrade\PHP Script Dati Telepass\script.php"
# Create arguments from Script location
# usually php.exe is invoked from console like:
# php.exe -f "C:\path\myscript.php"
#$PhpArgs = '-f "{0}"' -f $PhpFile
# Invoke, using the call operator
$PhpOutput = & $PhpExe $PhpFile
PhpOutput
$uscita = 1
}
### DECIDE WHICH EVENTS SHOULD BE WATCHED
Register-ObjectEvent $watcher "Created" -Action $action
#Register-ObjectEvent $watcher "Changed" -Action $action
#Register-ObjectEvent $watcher "Deleted" -Action $action
#Register-ObjectEvent $watcher "Renamed" -Action $action
while (!$uscita) {sleep 5}
exit
我尝试使用“$ uscita”变量来停止while并退出,但它不起作用。我还尝试将退出命令放在操作的最后,但这也不起作用
答案 0 :(得分:0)
看起来你的第2行到最后一行可能会返回true,所以它只是在sleep命令中。
尝试此测试,如果最后一个命令成功完成,它将返回true。
buildForm
答案 1 :(得分:0)
我以这种方式编辑了代码的最后一部分,现在它可以工作了。所以在动作之后,脚本结束了。 这是代码:
while (!$uscita) {
if($? -eq $false) {
exit
}
sleep 5}