How to know my variable content in a PS Script Execute?

38 views Asked by At

I would like to close a chrome process opened in a kiosk mode according to my page content.

But I do not know how to test my variable content when I am executing my script.

Because nothing happened and i think there is a mistake in my code :(

This is it :

$chromePath = "C:\Program Files\Google\Chrome\Application\chrome.exe"
# My URL to open
$url = "http://www.google.fr"
# Chrome en mode kiosque
$chromeProcess = Start-Process -FilePath $chromePath -ArgumentList "--kiosk", $url -PassThru -WindowStyle Minimized
# interval de vérification (en secondes)
$intervalVerification = 5
# délai d'inactivité (en secondes)
$delaiInactivite = 5
# string to find (to End the process when it is found)
$ExtraitChaine = "Recherche Google"

# Fonction pour vérifier l'inactivité du site
function CheckPageContent {
    try{
    $webPage = Invoke-WebRequest -Uri $url 
    }
    catch{
        $webPage = $_.Exception.Response
    }
    $pageContent = $webPage.Content
    Write-Host $pageContent

    # Check the content page
    if ($pageContent -contains $ExtraitChaine) {
        return $true  # it is OK
    }
    return $false  # it is not OK
}

# close the process Chrome
function CloseChromeProcess {
    $chromeProcesses = Get-Process -Name chrome -ErrorAction SilentlyContinue
    foreach ($process in $chromeProcesses) {
        Stop-Process -Id $process.Id -Force
    }
    Write-Host "Chrome Process over"
}

# Loop to check
do{
    if (CheckPageContent) {
        # it is OK => close the process
        CloseChromeProcess
        break
    }

    # wait 1 second to re-start check
    Start-Sleep -Seconds 1
} until (CheckPageContent)

Hope to have some help from u

Nothing happened It is like never get in my check procedure

0

There are 0 answers