隐藏窗口与Powershell脚本在后台运行

时间:2016-07-12 13:12:34

标签: powershell background hidden

我在后台运行的PowerShell脚本遇到了一些麻烦。以下是我的命令。 >Powershell.exe -File C:\testFolder\script.ps1 -WindowStyle Hidden。什么都没发生。该脚本不起作用。该脚本与Windows Powershell ISE一起运行良好。

以下是我的powershell脚本。

$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\ProgramData\"
$watcher.Filter = "output.txt";
$watcher.EnableRaisingEvents = $true 
$args = “$Server $FolderName”
$command = “cmd /C cscript .\VBScript.vbs $args”

$action = { $path = $Event.SourceEventArgs.FullPath
            $changeType = $Event.SourceEventArgs.ChangeType
            $logline = "$(Get-Date), $changeType, $path"
            Add-content "D:\log.txt" -value $logline
           invoke-expression $command
Write-Host "Test2"
          }    
Write-Host "Test"
$created = Register-ObjectEvent $watcher "Created" -Action $action
$changed = Register-ObjectEvent $watcher "Changed" -Action $action
$deleted = Register-ObjectEvent $watcher "Deleted" -Action $action
$renamed = Register-ObjectEvent $watcher "Renamed" -Action $action

在Windows Powershell ISE中执行powershell脚本输出(Test和Test2),而从命令提示符输出" Test"。

1 个答案:

答案 0 :(得分:0)

喔。我需要在最后添加while ($true) {}