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

[玩转系统] SharePoint Online:使用 PowerShell 获取内容类型库存报告

作者:精品下载站 日期:2024-12-14 14:31:23 浏览:15 分类:玩电脑

SharePoint Online:使用 PowerShell 获取内容类型库存报告


要求: SharePoint Online PowerShell 获取内容类型报告。

SharePoint Online:使用 PowerShell 获取内容类型

跟踪 SharePoint Online 环境中使用的所有内容类型可能是一项艰巨的任务。 PowerShell 可以通过提供所有类型的清单来帮助您管理和跟踪 SharePoint Online 内容类型。本博文将向您展示如何使用 PowerShell 获取 SharePoint Online 中所有内容类型的报告。此报告有助于记录您的环境中使用的内容类型。

让我们使用 PowerShell 获取 SharePoint Online 网站的所有内容类型并将它们导出到 CSV 文件:


#Load SharePoint CSOM 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"

#Config Parameters
$SiteURL="https://crescent.sharepoint.com"
$OutputFile ="C:\Temp\ContentTypes.csv"

Try {
    #Get Credentials to connect
    $Cred= Get-Credential

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
    #sharepoint online powershell get all content types
    $ContentTypes = $Ctx.Web.ContentTypes
    $Ctx.Load($ContentTypes)
    $Ctx.ExecuteQuery()
 
    $ContentTypes | Select Name,Description,Id, Group
    #Export to CSV
    $ContentTypes | Select Name,Description,Id, Group | Export-CSV $OutputFile -notypeinformation -Delimiter "," -Append
}
Catch {
    write-host -f Red "Error Getting Content Types!" $_.Exception.Message
}

此脚本生成一个 CSV 文件,其中包含给定站点的所有内容类型:

[玩转系统] SharePoint Online:使用 PowerShell 获取内容类型库存报告

使用 PowerShell 生成内容类型清单 HTML 报告:

让我们生成一个 HTML 报告,其中包含上述所有数据、字段以及每种内容类型的字段类型。


#Load SharePoint CSOM 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"

#Config Parameters
$SiteURL="https://crescent.sharepoint.com"
$ReportOutput ="C:\Temp\ContentTypes.htm"

#Define CSS Styles
$HeadTag = @"
<style type="text/css">
table {
 border-collapse: collapse; font-family: verdana,arial,sans-serif;
 font-size:11px; color:#333333; border-width: 1px; border-color: #a9c6c9;
 border: b1a0c7 0.5pt solid; border-spacing: 1px; border-collapse: separate; /*Sal Table format */  
}
 
th {
border-width: 1px; padding: 5px; background-color:#8064a2;
border: #b1a0c7 0.5pt solid; font-family: Calibri; height: 15pt; 
color: white;  font-size: 11pt;  font-weight: 700;  text-decoration: none;
}
 
td {
 border: #b1a0c7 0.5pt solid; font-family: Calibri; height: 15pt; color: black; 
 font-size: 11pt; font-weight: 400; text-decoration: none; 
 padding:5px;
}
 
tr:nth-child(even) { background-color: #e4dfec; }
</style>
"@
 
Try {
    #Get Credentials to connect
    $Cred= Get-Credential

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
    #Get all content types from the web
    $ContentTypes = $Ctx.Web.ContentTypes
    $Ctx.Load($ContentTypes)
    $Ctx.ExecuteQuery()
 
    $BodyContent="<center><h3> SharePoint Online: Content Type Inventory Report </h3></center>"
    $BodyContent += $ContentTypes | Select Name,Description,Id, Group | ConvertTo-Html -Fragment

    ForEach($CType in $ContentTypes)
    {
        #Get Content type fields
        $Ctx.load($CType.Fields)
        $Ctx.ExecuteQuery()

        #Add the Data to Report
        $BodyContent += "<br/><th><b>Content Type Name: </b>$($CType.Name)</th>"
        $BodyContent += $CType.Fields | where { $_.Hidden -eq $false -and $_.Title -ne "Content Type" } | Select Title, @{Expression={$_.TypeDisplayName }; Label="Field Type"} | ConvertTo-Html -Fragment
    }
    ConvertTo-HTML -Title "Content Type Inventory Report" -Head $HeadTag -Body $BodyContent | Out-File $ReportOutput

    Write-host -f Green "Content Type Report Generated Successfully!" $_.Exception.Message
}
Catch {
    write-host -f Red "Error Generating Content Types Report!" $_.Exception.Message
}

以及报告的示例输出,其中列出了所有内容类型及其字段:

[玩转系统] SharePoint Online:使用 PowerShell 获取内容类型库存报告

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

取消回复欢迎 发表评论:

关灯