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 =>
0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
Nevermind I got it too work by changing everything else to write-output, thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.