Get-SPOSite 查找现有站点
Get-SPOSite find existing sites
此代码将检查所列网站是否已存在。
$sites = get-content -Path C:\code\CheckSPSites.txt
foreach($site in $sites){
$url = (Get-SPOSite -Filter{url -like $site} -ErrorAction SilentlyContinue) -ne $null
if ($url -eq $true){
Write-Host "$site already created" -BackgroundColor Red
}else{
Write-Host "$site not created" -BackgroundColor Green
}
}
当我使用变量 $site 过滤搜索时,它没有找到网站。
我试过将变量放在引号中(Get-SPOSite -Filter{url -like "$site")但它也不起作用.
如有任何帮助,我们将不胜感激。
非常感谢
谢谢Theo and UBK。将您的建议作为答案发布以帮助其他社区成员。
您可以使用 -Identity
参数以及网站集 URL 来查找是否存在。
例如:Get-SPOSite -Identity https://contoso.sharepoint.com
# Verify if site of same name already exists in SharePoint Online
$siteAlreadyExists = Get-SPOSite -Identity $url
# If it does, stop the script
if ($null -ne $siteAlreadyExists) {
Write-Host "Site already exists" -ForegroundColor Red
...
}
如果您的代码在站点不存在时失败,那么您可以尝试以下 try-catch 块:
try {
$siteAlreadyExists = Get-SPOSite -Identity $url
if ($null -ne $siteAlreadyExists) {
Write-Host "Site already exists" -ForegroundColor Red
}
}
catch {
Write-Host "Site already exists" -ForegroundColor Red
}
此代码将检查所列网站是否已存在。
$sites = get-content -Path C:\code\CheckSPSites.txt
foreach($site in $sites){
$url = (Get-SPOSite -Filter{url -like $site} -ErrorAction SilentlyContinue) -ne $null
if ($url -eq $true){
Write-Host "$site already created" -BackgroundColor Red
}else{
Write-Host "$site not created" -BackgroundColor Green
}
}
当我使用变量 $site 过滤搜索时,它没有找到网站。
我试过将变量放在引号中(Get-SPOSite -Filter{url -like "$site")但它也不起作用.
如有任何帮助,我们将不胜感激。
非常感谢
谢谢Theo and UBK。将您的建议作为答案发布以帮助其他社区成员。
您可以使用 -Identity
参数以及网站集 URL 来查找是否存在。
例如:Get-SPOSite -Identity https://contoso.sharepoint.com
# Verify if site of same name already exists in SharePoint Online
$siteAlreadyExists = Get-SPOSite -Identity $url
# If it does, stop the script
if ($null -ne $siteAlreadyExists) {
Write-Host "Site already exists" -ForegroundColor Red
...
}
如果您的代码在站点不存在时失败,那么您可以尝试以下 try-catch 块:
try {
$siteAlreadyExists = Get-SPOSite -Identity $url
if ($null -ne $siteAlreadyExists) {
Write-Host "Site already exists" -ForegroundColor Red
}
}
catch {
Write-Host "Site already exists" -ForegroundColor Red
}