I am looking for a better way to handle exceptions in PowerShell. In the following example, I want to create a new SharePoint website and delete the old SharePoint website. When the New-SPWeb crashes, it is necessary for the script to complete. I think try / catch is the best way because the if statement only checks if $ a exists. Are there other options for handling exceptions?
Handling exceptions using the if statement:
$a = New-SPWeb http://newspweb if($a -eq $null) { Write-Error "Error!" Exit } Write-Host "No Error!" Remove-SPWeb http://oldspweb
With try / catch:
try { $a = New-SPWeb http://newspweb } catch { Write-Error "Error!" Exit } Write-Host "No Error!" Remove-SPWeb http://oldspweb
Laphi
source share