如何编写PowerShell脚本导出超90天未活跃的SharePoint站点数据
前置要求
- 设备已安装SharePoint Online Management Shell模块,未安装的可执行命令安装:
Install-Module -Name Microsoft.Online.SharePoint.PowerShell -Force - 操作账号拥有SharePoint全局管理员或 SharePoint 应用管理员权限
完整可执行脚本
# 定义SharePoint管理中心地址,已匹配你提供的地址 $adminSiteUrl = "https://mobileintra-admin.sharepoint.com/" # 定义闲置天数阈值 $inactiveDays = 90 # 定义结果导出路径 $exportPath = "./InactiveSPOSites_Over90Days.csv" # 连接SharePoint Online管理中心 Connect-SPOService -Url $adminSiteUrl # 获取所有站点并筛选闲置超90天的非活跃站点 $thresholdDate = (Get-Date).AddDays(-$inactiveDays) $inactiveSites = Get-SPOSite -Limit All | Where-Object { $_.LastContentModifiedDate -lt $thresholdDate -and $_.Template -notlike "RedirectSite#0" } | Select-Object Url, Title, Template, LastContentModifiedDate, StorageUsageCurrent, OwnerLoginName # 输出结果到控制台同时导出到CSV文件 $inactiveSites | Format-Table -AutoSize $inactiveSites | Export-Csv -Path $exportPath -NoTypeInformation -Encoding UTF8 Write-Host "非活跃站点数据已导出到:$exportPath,共筛选出$($inactiveSites.Count)个符合条件的站点"
注意事项
- 筛选逻辑默认排除了重定向站点,如有需要可删除
-and $_.Template -notlike "RedirectSite#0"条件 - 如果需要调整闲置天数,直接修改
$inactiveDays变量的数值即可 - 首次运行安装模块时如果提示信任仓库,输入Y确认即可
- 站点数量较多时脚本运行时间会相应变长,无需中断操作
内容的提问来源于stack exchange,提问作者MO-Senpai
相关产品推荐
相关产品推荐

