PowerShell - Error Handling

Опубликовано: 21 Октябрь 2024
на канале: SQL Depository
170
5

PS 1
------------------------------------------------------------------------------

$error.Clear()
Try {

Get-ChildItem -Path "C:\data10" -ErrorAction Stop


}

Catch {

Write-Host "There is an error!!" -ForegroundColor Red
Write-Host "Message: [$($_.Exception.Message)"] -ForegroundColor Yellow -BackgroundColor black

}
IF(!$error){ Write-Host "NoError!" -ForegroundColor Green -BackgroundColor black}

PS2
----------------------------------------------------------------------------------------------------------------
$error.Clear()

try{

Invoke-SQLCMD -Query "Select * from sys.databas" |Format-Table

}
Catch
{

Write-Host "There is an error!!" -ForegroundColor Red
Write-Host "Message: $error" -ForegroundColor Yellow -BackgroundColor black

}
IF(!$error){ Write-Host "NoError!" -ForegroundColor Green -BackgroundColor Black}

PS3
---------------------------------------------------------------------------------------------------------------
$error.Clear()



Invoke-SQLCMD -Query "Select * from sys.databas" -ErrorAction SilentlyContinue |Format-Table



If($error.count -gt 0)
{

Write-Host "There is an error!!" -ForegroundColor Red
Write-Host "Message: $error" -ForegroundColor Yellow -BackgroundColor black

}
IF(!$error){ Write-Host "NoError!" -ForegroundColor Green -BackgroundColor black}