当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 PowerShell 查找所有 OneDrive for Business 网站

作者:精品下载站 日期:2024-12-14 21:08:40 浏览:16 分类:玩电脑

使用 PowerShell 查找所有 OneDrive for Business 网站


要求:从 SharePoint Online 获取所有 OneDrive 网站集的列表。

我被要求获取所有 OneDrive 站点的列表,以了解 Office 365 租户中 OneDrive for Business 的使用情况。查找所有 OneDrive 网站集有点棘手,以下是我使用 Get-SPOSite cmdlet 实现此目的的方法:

如何列出所有 OneDrive for Business URL?

OneDrive for Business 是一种基于云的文件存储服务,是 Microsoft 365 工具套件的一部分。作为管理员,您可能需要获取组织中所有 OneDrive 网站的列表,以进行审核或管理。在本文中,我们将概述使用 Microsoft 365 管理中心和 PowerShell 获取 Microsoft 365 中的所有 OneDrive 网站的步骤。

若要通过 Microsoft 365 管理中心获取组织中所有 OneDrive for Business 网站的列表,请执行以下操作:

  1. 登录 Microsoft 365 管理中心 https://admin.microsoft.com
  2. 单击左侧导航中的“报告”>> 单击“使用情况”。
  3. 在 OneDrive 文件下,单击“查看更多”
  4. 向下滚动一点可查看提供所有 OneDrive 站点列表的报告。您还可以将报告数据导出为 CSV 格式以供进一步分析。

    [玩转系统] 使用 PowerShell 查找所有 OneDrive for Business 网站

使用 SharePoint Online Management Shell 查找所有 OneDrive 网站:

OneDrive for Business 提供集中存储来保存和共享文件。 OneDrive中存储的文件可以在任何设备上下载并同步到云端,以确保数据一致性。让我们使用 PowerShell 查找 SharePoint Online 中的所有 OneDrive 网站集。此 PowerShell 脚本通过筛选网站模板属性来获取 SharePoint Online 中的所有 OneDrive 网站集。


$AdminSiteURL="https://crescent-admin.sharepoint.com"

#Get Credentials to connect to SharePoint Admin Center
$Cred = Get-Credential

#Connect to SharePoint Online Admin Center
Connect-SPOService -Url $AdminSiteURL -credential $Cred

#Get all Personal Site collections and export to a Text file
Get-SPOSite -Template "SPSPERS" -limit ALL -includepersonalsite $True | Select URL, Owner | Format-table -autosize | Out-string -width 8096 | Out-file C:\temp\OneDriveSites.txt 

只需将 $AdminSiteURL 更改为您的域,提供 SharePoint Online 管理员的凭据,然后运行脚本即可。此脚本在“C:\Temp\OneDriveSites.txt”文件上生成输出。

[玩转系统] 使用 PowerShell 查找所有 OneDrive for Business 网站

使用 PowerShell 列出所有 OneDrive for Business 网站

您还可以使用 -Filter 您还可以在 Get-SPOSite cmdlet 中使用 -Filter 开关,如下所示:


Get-SPOSite -IncludePersonalSite $true -Limit all -Filter "Url -like '-my.sharepoint.com/personal/'"

使用 PnP PowerShell 获取所有 OneDrive 站点

我们可以使用 PnP PowerShell 检索所有 OneDrive 网站集的列表,如下所示:


#Config Variables
$TenantSiteURL =  "https://crescent-admin.sharepoint.com/"
  
#Connect to PnP Online
Connect-PnPOnline -Url $TenantSiteURL -Interactive

#Get All OneDrive Sites
Get-PnPTenantSite -IncludeOneDriveSites -Filter "Url -like '-my.sharepoint.com/personal/'"

让我们将所有 OneDrive 站点导出到 CSV 文件:


#Parameter
$Tenant =  "CrescentIntranet"
$CSVPath = "C:\Temp\OneDriveSites.csv"

#Connect to PnP Online
Connect-PnPOnline -Url "https://$tenant-admin.sharepoint.com" -Interactive

#Get OneDrive Site Details and export to CSV
Get-PnPTenantSite -IncludeOneDriveSites -Filter "Url -like 'https://$tenant-my.sharepoint.com/personal/'" |
    Select Title, URL, Owner, LastContentModifiedDate, StorageUsage | 
        Export-Csv -Path $CSVPath -NoTypeInformation

使用 PowerShell 获取所有 OneDrive 站点 - CSOM:

作为替代方法,您可以使用 PowerShell CSOM 获取所有 OneDrive 网站集。此脚本使用 UserProfileService 获取所有 SharePoint Online 用户并检查用户是否有个人网站。以下是使用 PowerShell 列出所有 OneDrive for Business 网站的脚本:


#Add references to SharePoint client assemblies
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.UserProfiles")

# Specify sharepoint online admin url 
$AdminSiteURL = "https://crescent-admin.sharepoint.com"
$MySiteURL="https://crescent-my.sharepoint.com"

#Specify the location where the list of OneDrive sites should be saved
$LogFile = "C:\Temp\OneDriveSites.txt"
 
#Setup Credentials to connect
$AdminCred= Get-Credential
$AdminCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($AdminCred.Username, $AdminCred.Password)
 
#Set User Profile Service path using SPO admin URL 
$ProxyAddr = "$AdminSiteURL/_vti_bin/UserProfileService.asmx?wsdl"
# Create a new webservice proxy to access UserProfileService
$UserProfileService= New-WebServiceProxy -Uri $ProxyAddr -UseDefaultCredential False
$UserProfileService.Credentials = $AdminCredentials
 
#Set authentication cookies
$StrAuthCookie = $AdminCredentials.GetAuthenticationCookie($AdminSiteURL)
$URI = New-Object System.Uri($AdminSiteURL)
$Container = New-Object System.Net.CookieContainer
$Container.SetCookies($URI, $StrAuthCookie)
$UserProfileService.CookieContainer = $Container
 
# Sets the first User profile, at index -1
$UserProfileResult = $UserProfileService.GetUserProfileByIndex(-1)
 
$NumProfiles = $UserProfileService.GetUserProfileCount()
$i = 1
 
# As long as the next User profile is NOT the one we started with (at -1)...
While ($UserProfileResult.NextValue -ne -1) 
{
    Write-Host "Checking profile $i of $NumProfiles" -foreground Yellow
 
    # Look for the Personal Space object in the User Profile and retrieve it  
    $Prop = $UserProfileResult.UserProfile | Where-Object { $_.Name -eq "PersonalSpace" } 
    $URL= $Prop.Values[0].Value
 
    # If "PersonalSpace" (which we've copied to $Url) exists, log it to our file...
    if ($URL) 
    {
        $SiteUrl = $MySiteURL+$URL
        # Write OneDrive site url to the console
        Write-Host $SiteUrl -foreground Green
        $SiteUrl | Out-File $LogFile -Append -Force
    }
 
    # And now we check the next profile the same way...
    $UserProfileResult = $UserProfileService.GetUserProfileByIndex($UserProfileResult.NextValue)
    $i++
}

结论:

通过执行这些简单的步骤,您可以使用 Microsoft 365 管理中心或 PowerShell 快速获取组织中所有 OneDrive 网站的列表。这可用于多种目的,例如审计、报告或管理。 Microsoft 365 管理中心提供了一个用户友好的界面来管理 OneDrive 环境,而 PowerShell 提供了更强大的自动化功能。无论您选择哪种方法,该过程都很简单且易于遵循。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯