PowerShell代码如果语句失败

时间:2017-08-17 20:25:02

标签: powershell if-statement install uninstall

我有一个有趣的情况发生在安装代理的地方并且它与服务器进行通信。此代理现在是EOL,并且没有续订供应商的支持。我们的许多代理商不再与服务器进行通信,因为它停止了回叫服务器退役的DMZ网络。代理具有密码防篡改功能,旧管理员无法识别密码。

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

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

这是我试图开发的脚本的背景。我的想法是从设备下载代理,检测体系结构并运行安装,然后运行卸载并为每个Program Files和/或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
}

2 个答案:

答案 0 :(得分:-1)

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
}

答案 1 :(得分:-1)

这个答案纯粹是针对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 }
}