Powershell循环不等待命令完成

时间:2019-06-10 20:44:33

标签: windows powershell winapi cmd

我已经创建了一个小脚本,每当安装了驱动程序时就可以运行并打印出来。我遇到的问题是,当我调用Compare-Object时,脚本似乎挂起了,只在退出时运行此代码。

我已经尝试了| out-nullwaitjob,但无济于事

$global:drivers = $null

function checkDrivers{
    $temp = Driverquery.exe /V

    # testing
    $temp += "newDriver     SMS Process Event Driv SMS Process Event Driv Kernel        Manual     Running    OK         TRUE        FALSE        8,192             8,192       0          03/08/2011 8:56:26 AM  C:\WINDOWS\system32\DRIVERS\prepdrv.sys          4,096"

    if($temp.Length -eq $global:drivers.Length){
        return
    }

    Write-Output "[---] Driver Installed"
    Compare-Object -ReferenceObject $global:drivers -DifferenceObject $temp
    $global:drivers = $temp
}

Write-Output "[+] Parsing initial drivers..."
$global:drivers = Driverquery.exe /V
Write-Output "[+] Parsing complete`n"
Write-Output "[+] Press 'q' to quit"
Write-Output "[+] Scanning for Driver Installs..."
while ($true){
    if ($Host.UI.RawUI.KeyAvailable -and ("q" -eq $Host.UI.RawUI.ReadKey("IncludeKeyUp,NoEcho").Character)) {
        Write-Host "Exiting now..." -Background DarkRed
        break;
    }

    checkDrivers
    start-sleep -seconds 5
}

这是我的输出,我希望在[---]已安装驱动程序下退出之前执行对象比较行

[+] Parsing initial drivers...
[+] Parsing complete

[+] Press 'q' to quit
[+] Scanning for Driver Installs...
[---] Driver Installed

Exiting now...
InputObject                                                                                                                                                                                                                                                SideIndicator
-----------                                                                                                                                                                                                                                                -------------
newDriver     SMS Process Event Driv SMS Process Event Driv Kernel        Manual     Running    OK         TRUE        FALSE        8,192             8,192       0          03/08/2011 8:56:26 AM  C:\WINDOWS\system32\DRIVERS\prepdrv.sys          4,096 =>

1 个答案:

答案 0 :(得分:0)

“ Exiting now ..”行出现在输出的其余部分上方,因为它使用的是writes to the consoleWrite-Host,而不是写入管道的Write-Output。但是脚本正在按预期顺序执行。