IF/ELSE Get-SPWeb



我想快速检查一个网站是否存在。我似乎有一个错误在我的IF声明,但我不确定正确的语法。下面是我的代码:

$URLis = "https://ourdevsite.dev.com/sites/flibidyboots"
add-pssnapin microsoft.sharepoint.powershell -ea 0
IF ((Get-SPWeb $URLis) -ne 0){
    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
}

我做错了什么?

好的,首先它是$null,而不是0。其次,如果它不是$null,那么它就存在,所以您的情况混淆了。

下面是一些可以工作的代码。

Add-PSSnapin "Microsoft.SharePoint.Powershell" -ErrorAction SilentlyContinue
$url = "http://teams"
$w = Get-SPWeb -Identity $url -ErrorAction SilentlyContinue
if ($w) {
   Write-Host "Site Exists" -ForegroundColor Green
} else {
   Write-Host "No Site" -ForegroundColor Red
}

最新更新