尝试/捕获...但想继续出错,而不是-ErrorAction停止

时间:2016-11-10 15:59:52

标签: powershell sharepoint sharepoint-2010 powershell-v2.0

我在编写脚本方面仍然有点新手但是已经走了很长的路。我需要在下面做一个Try / Catch,但我很困惑。我展示的所有研究显示-ErrorAction Stop ....但在这种情况下,如果有错误...继续......我想。这是一个有关的事情。 脚本的这一部分检查该站点是否存在,如果它不存在......那么......继续使用该脚本。如果它存在,停止并写出一些东西并在那里结束脚本。 所以TRY / CATCH让我很困惑。你能帮忙吗?

$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
}

1 个答案:

答案 0 :(得分:0)

您无需更改ErrorAction,只需使用exit关键字停止脚本:

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
}

我没有Sharepoint环境,因此我假设您的Get-SPWeb检查已经有效并返回true / false。