samedi 20 juin 2020

Powershell if else condition ignore warning keep to continue

I would like to design a condition which is if the tcp connection result is return "true", will proceed install action and go next function. Otherwise, it will return a warning message, stop to proceed and pass to next function. I don't know how to implement keep to proceed next function if occurs any warning on current function. Is there anybody can help to fix the issue? Many Thanks.

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;

Aucun commentaire:

Enregistrer un commentaire