mardi 15 janvier 2019

Powershell -notin array won't trigger if statement

I've recently begun to create a powershell script including a GUI.
To prevent the GUI from freezing I've created a background job in which my function "xyz" runs...
I want to capture a specific window title. If this window closes the if should be fired.

Now my problem with this script is the following:
If I run the script without putting it in the background job it does notice the if statement and returns the value I want to have.

If I run the script and put the task into the background job this job won't stop and won't recognize the if statement.
Does someone have a solution for this problem?

Function xyz {
    $global:TitleArray = @("Termius - Hosts","Some Window Title","...")
    $global:WindowClosed = $false
    $global:TitleArray | Out-Host
    $global:Job = start-job -Name FindGame {
        Do {
            (Get-Process | Where-Object {$_.MainWindowTitle -ne ""} | Select-Object MainWindowTitle) | % {
                if($_.MainWindowTitle -in $global:TitleArray){
                    $global:WindowFound = $true
                    $global:FoundWindowName = $_.MainWindowTitle
                    do {
                        $WindowArray = @()
                        (Get-Process | Where-Object {$_.MainWindowTitle -ne ""} | Select-Object MainWindowTitle) | % {
                           $WindowArray += $_.MainWindowTitle
                        }
                        if($global:FoundWindowName -notin $WindowArray){
                            $global:WindowClosed = $true
                        }
                         Sleep 1
                    } while ($global:WindowClosed -ne $true)
                }

                if($global:WindowClosed){
                    $Global:FoundWindowName | Out-Host
                    exit
                }
                Sleep 1
            }         
        } while ($true)
    }
}

xyz

Aucun commentaire:

Enregistrer un commentaire