0

I have create a little script to run and print out whenever I have a driver installed, The problem I am having is when I call Compare-Object the script seems to hang and only run this code on quit.

I have already tried | out-null and waitjob with no avail

$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
}

Here is my output, I expected the Object compare line to be executed before quit under [---] Driver Installed

[+] 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 =>
rooter
  • 99
  • 2
  • 8

1 Answers1

1

The 'Exiting now..' line is appearing above the rest of the output because it's using Write-Host which writes to the console instead of Write-Output which writes to the pipeline. But the script is executing in the expected order.

Rich Moss
  • 2,195
  • 1
  • 13
  • 18
  • How would I go about piping the command to Write-Host, I have tried ```Compare-Object -ReferenceObject $global:drivers -DifferenceObject $temp | Write-Host``` but it still seems to halt the program. – rooter Jun 11 '19 at 00:12
  • Nevermind I got it too work by changing everything else to write-output, thank you. – rooter Jun 11 '19 at 00:45