【问题标题】:Hiding Window with Powershell script running in the background使用后台运行的 Powershell 脚本隐藏窗口
【发布时间】:2016-07-12 13:12:34
【问题描述】:

我在后台运行的 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”。

【问题讨论】:

  • 您确定您的执行策略允许运行脚本吗?试试Powershell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -File C:\testFolder\script.ps1
  • 省略-WindowStyle Hidden参数是否有效?
  • 不幸的是它没有。我已经检查了环境变量中的 powershell.exe 路径。但似乎不起作用。也不给出任何错误。
  • @NeelDeveloper 将-WindowStyle hidden 替换为-NoExit 开关,这样Powershell 窗口就不会关闭。在脚本中添加一些write-host 语句,看看会发生什么。这听起来像您遇到了 UAC 问题。进一步的分析将受益于MVCE
  • 哦。抱歉,我需要添加while ($true) {}

标签: powershell background hidden


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2019-12-12
    • 1970-01-01
    • 1970-01-01
    • 2014-03-13
    • 2022-12-31
    • 2023-01-19
    • 1970-01-01
    • 2012-04-08
    • 2018-03-30
    相关资源
    最近更新 更多