G Rojas
G Rojas

Reputation: 35

Try/Catch...but want to continue on error, not -ErrorAction Stop

I'm still a bit of a novice when it comes to scripting but have come a long way. I need to do a Try/Catch on the following, but am confused on it. All research I show, shows -ErrorAction Stop....but in this case, if there is an error...continue....I think. Here's the dealio. This section of the script checks to see if the site exists, if it doesn't .... GREAT...continue with the script. If it DOES exist, the stop and write out some stuff and END the script there. So the TRY/CATCH is confusing to me. Can you help?

$URLis = "https://ourdevsite.dev.com/sites/$myVar"

add-pssnapin microsoft.sharepoint.powershell -ea 0

If (-not(Get-SPWeb $URLis)){
    Write-Host "Site does not exist, so we can proceed with building it" -foregroundcolor green
    }
Else {
Write-Host "Site does exist, so we need to pick another URL" -foregroundcolor red
}

Upvotes: 0

Views: 912

Answers (1)

henrycarteruk
henrycarteruk

Reputation: 13227

You don't need to change ErrorAction, just use the exit keyword to stop the script:

add-pssnapin microsoft.sharepoint.powershell

$URL = "https://ourdevsite.dev.com/sites/$myVar"

If (Get-SPWeb $URL) {
    Write-Host "Site does exist, so we need to pick another URL" -foregroundcolor red
    exit
}
Else {
    Write-Host "Site does not exist, so we can proceed with building it" -foregroundcolor green
}

I don't have a Sharepoint environment, so I'm assuming your Get-SPWeb check already works and returns true/false.

Upvotes: 1

Related Questions