Тест Powershell 2, если истинные функции - PullRequest
0 голосов
/ 18 февраля 2019
Clear-Host
Function First {
    $File1 = "C:\Program Files\WinRAR\WinRAR.exe"
    $TestFile1 = Test-Path $File1
    If ($TestFile1 -eq $True) {Write-Host "Winrar is installed." -F Green -B Black}
    Else {Write-Host "Winrar is not installed." -F Red -B Black}
}
First

Function Second {
    $winrar = Get-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*
    $winrarCheck = "Winrar"
    $winrarFinal = $winrar | Where {$_.DisplayName -match $winrarCheck} | Format-Table -Property DisplayName,DisplayVersion

    $winrarFinal
    If ($winrarFinal) {Write-Host "Winrar registry check installed." -F Green -B Black}
    Else {Write-Host "Winrar registry check failed." -F Red -B Black}
    }
Second

Есть ли способ проверить обе функции с помощью оператора If?Я хочу проверить, как если обе функции верны, Write-Host "Program installed." Else Write-Host "Failed to install".

1 Ответ

0 голосов
/ 18 февраля 2019

Вот и мы:

Function CheckFileInstalled {

    param (
        [string]$pathProg     = "C:\Program Files\WinRAR\WinRAR.exe",
        [string]$nameProg     = "Winrar"
    )

    $testFileProg = Test-Path $pathProg

    $x86 = ((Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall") |
        Where-Object { $_.GetValue( "DisplayName" ) -like "*$nameProg*" } ).Length -gt 0;

    $x64 = ((Get-ChildItem "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall") |
        Where-Object { $_.GetValue( "DisplayName" ) -like "*$nameProg*" } ).Length -gt 0;


    return ( $testFileProg -and ($x86 -or $x64) )

}


if( CheckFileInstalled ) {

    Write-Host "Program installed." 
}
else {
    Write-Host "Failed to install."
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...