[玩转系统] SharePoint Online:使用 PowerShell 检查网站集或子网站是否存在
作者:精品下载站 日期:2024-12-14 14:20:41 浏览:13 分类:玩电脑
SharePoint Online:使用 PowerShell 检查网站集或子网站是否存在
要求:使用 PowerShell 检查 SharePoint Online 中是否存在网站或网站集。
如何检查 SharePoint Online 中是否存在网站?
您是否曾经需要检查 SharePoint Online 中是否存在某个网站?也许您刚刚获得了潜在新网站的 URL,并且希望在开始创建之前确保该网站已创建,或者您正在编写 PowerShell 脚本并需要在继续之前验证该网站是否存在你的代码。无论哪种情况,都有几种方法可以检查站点是否存在。让我们来看看它们吧!
要确认 SharePoint Online 中是否存在网站集,您可以快速转到:
好吧,现在我将向您展示如何使用 PowerShell 检查 SharePoint Online 网站是否存在。
SharePoint Online PowerShell CSOM 检查给定 URL 中是否存在网站:
如何判断某个网站是否已存在,而无需从管理中心搜索所有网站? PowerShell 来救援!此 PowerShell 脚本检查给定 URL 中是否存在网站集:
#Set Parameters
$AdminCenterURL="https://crescent-admin.sharepoint.com"
$SiteURL = "https://crescent.sharepoint.com/sites/suppliers"
#Connect to SharePoint Online
Connect-SPOService -Url $AdminCenterURL -Credential (Get-Credential)
#Get all site collections
$Sites = Get-SPOSite -Limit All | Select -ExpandProperty URL
#Check if any site with the URL exists already
If($Sites -notcontains $SiteURL)
{
Write-Host "Site Collection URL '$SiteURL' is available!" -f Green
}
Else
{
Write-Host "Site URL '$SiteURL' is not available!" -f Yellow
}
当您尝试自动化流程并需要在采取进一步操作之前确保站点存在时,这是一个方便的脚本。通过 CSOM 检查网站或网站集是否存在如何?
#Load SharePoint Online Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Custom Function to Check if Site Collection Exists in Given URL
Function Check-SiteExists($SiteURL, $Credentials)
{
#Setup context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
$Web = $Ctx.Web
$Ctx.Load($web)
Try {
$Ctx.ExecuteQuery()
Return $True
}
Catch [Exception] {
Write-host $_.Exception.Message -f Red
Return $False
}
}
#Variable for Site collection URL
$URL= "https://crescent.sharepoint.com/sites/sales/"
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
$SiteExists = Check-SiteExists -SiteURL $URL -Credentials $Cred
#Call the function to check site exists
if($SiteExists -eq $True)
{
write-host "Given Site Collection Exists!" -f Green
#Proceed with your script
}
else
{
write-host "Site Collection doesn't Exists on given URL!" -f Red
}
请注意,如果出现任何网络相关问题或身份验证失败,函数 Check-SiteExists 可能会返回 false。
PnP PowerShell 检查该网站是否存在于 SharePoint Online
PowerShell 是一种功能强大的脚本语言,可用于自动执行 Microsoft SharePoint Online 中的任务。让我向您展示如何使用 PnP PowerShell 检查 SharePoint Online 中是否存在网站:
#Parameters
$AdminCenterURL = "https://crescent-admin.sharepoint.com"
$SiteURL = "https://crescent.sharepoint.com/sites/procurement"
Try
{
#Connect to Tenant Admin
Connect-PnPOnline -URL $AdminCenterURL -Interactive
#Check if site exists
$Site = Get-PnPTenantSite | Where {$_.Url -eq $SiteURL}
If ($Site -eq $null)
{
Write-host "Site Collection $($SiteURL) doesn't exists!" -foregroundcolor Yellow
}
Else
{
Write-host "Site $($SiteURL) exists already!" -foregroundcolor Green
}
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
PnP PowerShell 检查网站集中是否存在子网站
#Parameters
$SiteCollURL = "https://crescent.sharepoint.com/sites/Retail"
$SubSiteURL = "/sites/Retail/archived/2010" #ServerRelativeUrl
Try
{
#Connect to Tenant Admin
Connect-PnPOnline -URL $SiteCollURL -Interactive
#Get All Webs from the site
$Webs = Get-PnPSubWeb -Recurse -IncludeRootWeb
#Check if site exists
$Subsite = ($Webs | Select -ExpandProperty ServerRelativeUrl) -contains $SubSiteURL
If ($Subsite -eq $false)
{
Write-host "SubSite $($SubSiteURL) doesn't exists!" -foregroundcolor Yellow
}
Else
{
Write-host "Site $($SubSiteURL) exists!" -foregroundcolor Green
}
}
Catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
这对于自动化目的或验证站点是否处于活动状态(或已被删除!)非常有用。我的其他服务器端脚本,用于在本地 SharePoint 中使用 PowerShell 检查 SharePoint 中是否存在网站集或子网站。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag