powershell 值为False,条件为if语句中的-eq $true,并且通过

wnavrhmk  于 2023-02-16  发布在  Shell
关注(0)|答案(1)|浏览(205)

我是powershell的新手。我一直在学习,我上周五开始的。所以原谅我,如果这是显而易见的!我不知道为什么第一个if语句通过,而条件没有得到尊重。
我试着检查一个程序是否在我的电脑上运行。如果是,它告诉我“程序正在运行”!当它不再运行时,它写的是没有运行。这是我的代码:

if (isPrgmRunning -eq $true) {
    isPrgmRunning #Return false, why the last if statement pass ???
    write-output "Programme is running"
    $isRunning = $true 
    while($isRunning) {
        write-output "While the programme keep running"
        if(CheckInternetConnection) {
            if(isPrgmRunning -eq $false) {
                isPrgmRunning #Check again value, and it's false
                Write-Output "If it's not running anymore"
                $isRunning = $false
            }
        }
    }
}

我不明白为什么它不运行,第一个如果通过...
这就是所有的问题:

$global:previousStatus=$null

# Function to check if the computer is connected to the internet
Function CheckInternetConnection {
    $currentStatus = Test-Connection -ComputerName "www.google.com" -Count 1 -Quiet
    If ($currentStatus -ne $global:previousStatus) {
        If ($currentStatus) {
            ShowMessageBox "Connected"
        }
        Else {
            ShowMessageBox "Disconnected"
        }
        $global:previousStatus = $currentStatus
    }
    return $currentStatus
}

# Function to display a message box
Function ShowMessageBox {
    [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
    [System.Windows.Forms.MessageBox]::Show($args[0], "Internet Connection Status")
}

# Function to check if the "Minecraft" program is running can be replace by any other
Function isPrgmRunning {
    try {
        $process = Get-Process -Name "Minecraft" -ErrorAction Stop #This is an exemple of programme :)
        if ($process -ne $null) {
            write-output "Prgm run"
            return $true
        }
    } catch [Microsoft.PowerShell.Commands.ProcessCommandException] {
        write-output "Prgm not run"
        return $false
    }
}

# Main function
function Main {
    while($true){
        if (CheckInternetConnection) {
        write-output "Connected"
            if (isPrgmRunning -eq $true) {
                isPrgmRunning #Return false, why the last if statement pass ???
                write-output "Programme is running"
                $isRunning = $true 
                while($isRunning) {
                    write-output "While the programme keep running"
                    if(CheckInternetConnection) {
                        if(isPrgmRunning -eq $false) {
                            isPrgmRunning #Check again value, and it's false
                            Write-Output "If it's not running anymore"
                            $isRunning = $false
                        }
                    }
                }
            }
        }
        Start-Sleep -Seconds 1
    }
}

Main

谢谢你对我的剧本的任何帮助!我会听到所有的建议和解决方案,你可以给我!

[编辑]:问题解决了。它在isPrgmRunning上。我不知道为什么,但不是这样:

$process = Get-Process -Name "Minecraft" -ErrorAction Stop

我在www.example.com上看到过这个educba.com/powershell-boolean/:

Get-Process | where{$_.Name -eq "Wrong process"} -ErrorAction Ignore

所以我们得到这个:

Function isPrgmRunning {
    $process = Get-Process | where{$_.Name -eq "Minecraft"} -ErrorAction Ignore #Still with Minecraft but it can be "Teams" or something else
    if($process){
        return $true
    }
    else{
        return $false
    }
}
xuo3flqw

xuo3flqw1#

评估函数,此刻你只是检查它是否存在,尝试这样做:

$checkinternetconnection = $true

Function isPrgmRunning {
    try {
        $process = Get-Process -Name "Teams" -ErrorAction Stop #This is an exemple of programme :)
        if ($process -ne $null) {
            write-output "Prgm run"
            return $true
        }
    } catch [Microsoft.PowerShell.Commands.ProcessCommandException] {
        write-output "Prgm not run"
        return $false
    }
}

function Main {
    while($true){
        if ($CheckInternetConnection) {
        write-output "Connected"
            if (isPrgmRunning) {
                isPrgmRunning #Return false, why the last if statement pass ???
                write-output "Programme is running"
                $isRunning = $true 
                while($isRunning) {
                    write-output "While the programme keep running"
                    if($CheckInternetConnection) {
                        if(!(isPrgmRunning)) {
                            isPrgmRunning #Check again value, and it's false
                            Write-Output "If it's not running anymore"
                            $isRunning = $false
                        }
                    }
                }
            }
        }
        Start-Sleep -Seconds 1
    }
}
main

相关问题