PowerShell 代码 if 语句失败

PowerShell Code if statement failure

我遇到了一个有趣的情况,其中安装了代理并与服务器通信。此代理现在已停产,供应商的支持未续订。我们的许多代理不再与服务器通信,因为它一直在回拨到服务器停用的 DMZ 网络。代理有密码篡改保护,旧管理员不知道密码。

我发现我们可以做的是在这些服务器上,再次安装代理,然后我们内部网络上的设备将密码更改为任何密码,对于我的情况,我没有填写。

更糟糕的是,代理安装在 Program FilesProgram Files (x86) 中,在某些情况下,它会在同一台服务器上安装两次。

这就是我正在尝试开发的脚本的背景。我的想法是从设备下载代理,检测架构和 运行 安装,然后 运行 卸载并为每个 Program Files and/or Program Files(x86) 循环它.我让它工作正常,但它没有从 x86 目录中删除应用程序。另外,我希望能得到一些格式提示。

if ((Get-WmiObject Win32_OperatingSystem).OSArchitecture -eq "64-bit") {
    # Download Application
    $source = "http://heartrate0001/x64/HeartRate.exe"
    $filename = [System.IO.Path]::GetFileName($source)
    $destination = "$ENV:USERPROFILE\Desktop$filename"
    $webclient = New-Object System.Net.WebClient
    $webclient.DownloadFile($source,$destination)
    $patha = "C:\Program Files\ICU\HeartRate.exe"
    $pathb ="C:\Program Files (x86)\ICU\HeartRate.exe"
    $Folder1Path = 'C:\Program Files\ICU\HeartRate.exe'
    $Folder2Path = 'C:\Program Files (x86)\ICU\HeartRate.exe'
}

# Install Application
$process = Start-Process $destination -PassThru -Wait
$process.ExitCode

Write-Host $process.Exitcode

# Uninstall
if ((Test-Path -Path $Folder1Path) -eq "true") {
    {
        $Folder1Path = "C:\Program Files\ICU\HeartRate.exe"
        $arg1 = "-uninstall"
        & $Folder1Path $Arg1
    }
}

if ((Test-Path -Path $Folder2Path) -eq "true") {
    {
        $Folder2Path = "C:\Program Files\ICU\HeartRate.exe"
        $arg1 = "-uninstall"
        & $Folder2Path $Arg1
    }
} elseif ((Get-WmiObject Win32_OperatingSystem).OSArchitecture -eq "32-bit") {
    # Download Application
    $source = "http://heartrate0001/HeartRate.exe"
    $destination = "$ENV:USERPROFILE\Desktop$filename"
    $webclient = New-Object System.Net.WebClient
    $webclient.DownloadFile($source,$destination)

    # Install Application
    $process = Start-Process $destination -PassThru -Wait
    $process.ExitCode

    Write-Host $process.Exitcode

    # Uninstall
    $app = "C:\Program Files\ICU\HeartRate.exe"
    $arg1 = "-uninstall"
    & $app $Arg1
}

您的 if 语句中有一个脚本块。但是,您永远不会调用该脚本块。

变化:

if ((Test-Path -Path $Folder2Path) -eq "true") {
    {
        $Folder2Path = "C:\Program Files\ICU\HeartRate.exe"
        $arg1 = "-uninstall"
        & $Folder2Path $Arg1
    }
}

至:

if ((Test-Path -Path $Folder2Path) -eq "true") {
    $Folder2Path = "C:\Program Files\ICU\HeartRate.exe"
    $arg1 = "-uninstall"
    & $Folder2Path $Arg1
}

此答案纯粹是针对 OP 要求的格式示例。你的大部分工作都是多余的:

If ((Get-WmiObject Win32_OperatingSystem).OSArchitecture -eq '64-bit')
{
    $Source = 'http://heartrate0001/x64/HeartRate.exe'
    $Destination = "$env:UserProfile\Desktop$([IO.Path]::GetFileName($Source))"
    # PSv3+
    Invoke-WebRequest -Uri $Source -OutFile $Destination
    $x64 = "$env:ProgramFiles\ICU\HeartRate.exe"
    $x86 = "${env:ProgramFiles(x86)}\ICU\HeartRate.exe"

    (Start-Process $Destination -PassThru -Wait).ExitCode

    If (Test-Path $x64) { & $x64 -uninstall }
    If (Test-Path $x86) { & $x86 -uninstall }
}
Else
{
    $Source = 'http://heartrate0001/HeartRate.exe'
    $Destination = "$env:UserProfile\Desktop$([IO.Path]::GetFileName($Source))"
    Invoke-WebRequest -Uri $Source -OutFile $Destination
    $x86 = "$env:ProgramFiles\ICU\HeartRate.exe"

    (Start-Process $Destination -PassThru -Wait).ExitCode

    If (Test-Path $x86) { & $x86 -uninstall }
}