Powershell if else条件忽略警告继续

Powershell if else条件忽略警告继续,powershell,if-statement,Powershell,If Statement,我想设计一个条件,如果tcp连接结果为return true,将继续安装操作并执行下一个函数。否则,它将返回警告消息,停止继续并传递到下一个函数。如果当前函数出现任何警告,我不知道如何实现keep to Procedue next函数。 有没有人可以帮助解决这个问题? 非常感谢 function A(){ $printerIPAddress = "192.168.1.100" $sharedPrinter = "\\SERVERNAME\A" $chec

我想设计一个条件,如果tcp连接结果为return true,将继续安装操作并执行下一个函数。否则,它将返回警告消息,停止继续并传递到下一个函数。如果当前函数出现任何警告,我不知道如何实现keep to Procedue next函数。 有没有人可以帮助解决这个问题? 非常感谢

function A(){

$printerIPAddress = "192.168.1.100"
$sharedPrinter = "\\SERVERNAME\A"
$checkPrinterExists = Get-Printer -Name $sharedPrinter -ErrorAction SilentlyContinue

    if ((Test-NetConnection -ComputerName $printerIPAddress -WarningAction SilentlyContinue).PingSucceeded){
        Write-Host "Succeed to connect - $printerIPAddress" -ForegroundColor Black -BackgroundColor Gray
        Start-Sleep(2)
    }
    else {
        Write-Warning "Failure to connect - $printerIPAddress"
    }
    
    if (-not $checkPrinterExists){
        Write-Host "Installing a printer..."
        Add-Printer -ConnectionName $sharedPrinter
        Write-Host "Succeed to install - A Printer" -ForegroundColor Black -BackgroundColor Gray
    }
    else{
        Write-Warning "Failure to install - A Printer already exist"
    }
}

function B(){

$printerIPAddress = "192.168.1.101"
$sharedPrinter = "\\SERVERNAME\B"
$checkPrinterExists = Get-Printer -Name $sharedPrinter -ErrorAction SilentlyContinue

    if ((Test-NetConnection -ComputerName $printerIPAddress -WarningAction SilentlyContinue).PingSucceeded){
        Write-Host "Succeed to connect - $printerIPAddress" -ForegroundColor Black -BackgroundColor Gray
        Start-Sleep(2)
    }
    else {
        Write-Warning "Failure to connect - $printerIPAddress"
    }
    
    if (-not $checkPrinterExists){
        Write-Host "Installing a printer..."
        Add-Printer -ConnectionName $sharedPrinter
        Write-Host "Succeed to install - B Printer" -ForegroundColor Black -BackgroundColor Gray
    }
    else{
        Write-Warning "Failure to install - B Printer already exist"
    }
}

Write-Host "Running...Please wait..."
A;
B;


我试图简化您的代码,并在测试NetConnection失败的情况下固有地处理停止继续

由于您的主要目标是安装打印机,如果参数$sharedPrinter未达到定义的ValidateScript标准,则该函数将出错

Function Install-Printer {

    [CmdletBinding()]
    Param(
    [parameter(Mandatory=$true)]
    [ValidateScript({$_ -match [IPAddress]$_ })]  
    [string]
    $printerIPAddress,

    [parameter(Mandatory=$true)]
    [ValidateScript({(Test-Path $_) -and ($Printer = Get-Printer -Name $_)})]  
    [string]
    $sharedPrinter
    )

    if ((Test-NetConnection -ComputerName $printerIPAddress -WarningAction SilentlyContinue).PingSucceeded) {

        Write-Host "Succeed to connect - $printerIPAddress" -ForegroundColor Black -BackgroundColor Gray
        Write-Host "Installing a printer..."
        Add-Printer -ConnectionName $sharedPrinter
        if($?){ # if previous command succeeded
            Write-Host "Succeed to install - $($Printer.Name) Printer" -ForegroundColor Black -BackgroundColor Gray
        }
        else {
            Write-Warning "Failure to install - $($Printer.Name) Printer already exist"
        }

    }
    else {

        Write-Error "Failure to connect - $printerIPAddress"

    }

}

Write-Host "Running...Please wait..."
# Install Printer A
Install-Printer -printerIPAddress "192.168.1.100" -sharedPrinter "\\SERVERNAME\A"
# Install Printer B
Install-Printer -printerIPAddress "192.168.1.101" -sharedPrinter "\\SERVERNAME\B"