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

[玩转系统] 使用 PowerShell 将 SharePoint 配置清单导出到 XML

作者:精品下载站 日期:2024-12-14 14:17:28 浏览:13 分类:玩电脑

使用 PowerShell 将 SharePoint 配置清单导出到 XML


要求:我们必须比较不同环境(例如 Web 应用程序、内容数据库等)之间的特定 SharePoint 库存。因此必须将 SharePoint 配置提取并导出到 XML 文件中。

将 Web 应用程序列表导出为 XML 的 PowerShell 脚本

使用 PowerShell 将 SharePoint 列表数据导出到 XML:


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Create new XML File
[System.XML.XMLDocument]$XMLDocument=New-Object System.XML.XMLDocument

#Add XML Declaration
$Declaration = $XMLDocument.CreateXmlDeclaration("1.0","UTF-8",$null)
$XMLDocument.AppendChild($Declaration)

#Create Root Node
[System.XML.XMLElement]$XMLRoot=$XMLDocument.CreateElement("WebApplications")

#Get all web applications and loop through
$WebApps = Get-SPWebApplication
ForEach($webApp in $WebApps)
{
    $WebAppElement = $XMLDocument.CreateElement("WebApplication")
    $WebAppElement.SetAttribute("Name", $WebApp.DisplayName)
    $WebAppElement.SetAttribute("URL", $WebApp.URL)
    #Append the data to Root node
    $XMLRoot.AppendChild($WebAppElement)
}
# Append Root Node to XML
$XMLDocument.AppendChild($XMLRoot)

#Save File
$XMLDocument.Save("c:\WebApps.xml")

脚本输出:

[玩转系统] 使用 PowerShell 将 SharePoint 配置清单导出到 XML

库存 Web 应用程序及其与 XML 相关的内容数据库

让我们向其中添加数据库信息:


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Create new XML File
[System.XML.XMLDocument]$XMLDocument=New-Object System.XML.XMLDocument

#Add XML Declaration
$Declaration = $XMLDocument.CreateXmlDeclaration("1.0","UTF-8",$null)
$XMLDocument.AppendChild($Declaration)

#Create Root Node "WebApplications"
[System.XML.XMLElement]$WebAppRootElement=$XMLDocument.CreateElement("WebApplications")

#Get all web applications and loop through
$WebApps = Get-SPWebApplication
ForEach($webApp in $WebApps)
{
    $WebAppElement = $XMLDocument.CreateElement("WebApplication")
    $WebAppElement.SetAttribute("Name", $WebApp.DisplayName)
    $WebAppElement.SetAttribute("URL", $WebApp.URL)

    #Append the WebApplication data to "WebApplications" Node
    $WebAppRootElement.AppendChild($WebAppElement)

    #Database Element
    $DatabaseRootElement = $XMLDocument.CreateElement("Databases")

    $ContentDBs = $webApp.ContentDatabases
    ForEach($Database in $ContentDBs)
    {
        $DatabaseElement = $XMLDocument.CreateElement("Database") 
        $DatabaseElement.SetAttribute("Name", $Database.DisplayName)
        $DatabaseElement.SetAttribute("ID", $Database.ID)
           
        #Append the Database elements to "WebApplication" node
        $WebAppElement.AppendChild($DatabaseElement)
    }
}
# Append Root Node to XML
$XMLDocument.AppendChild($WebAppRootElement)

#Save File
$XMLDocument.Save("c:\WebApps-inventory.xml")

PowerShell 将 SharePoint 服务应用程序清点为 XML

让我们盘点一下 SharePoint 2016 服务应用程序:


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Create new XML File
[System.XML.XMLDocument]$XMLDocument=New-Object System.XML.XMLDocument

#Add XML Declaration
$Declaration = $XMLDocument.CreateXmlDeclaration("1.0","UTF-8",$null)
$XMLDocument.AppendChild($Declaration)

#Create Root Node "ServiceApplications"
[System.XML.XMLElement]$ServiceAppRootElement=$XMLDocument.CreateElement("ServiceApplications")

#Get all Service applications and loop through
$ServiceApps = Get-SPServiceApplication
Foreach($ServiceApp in $ServiceApps)
{
    $ServiceAppElement = $XMLDocument.CreateElement("ServiceApplication")
    $ServiceAppElement.SetAttribute("TypeName",$ServiceApp.TypeName)
    $ServiceAppElement.SetAttribute("DisplayName", $ServiceApp.DisplayName)
    $ServiceAppElement.SetAttribute("Status", $ServiceApp.Status)    

    #Get the Account and Proxy Group of the Service App
    $ServiceAccountElement = $XMLDocument.CreateElement("ServiceAccount")
    $ServiceAccountElement.Set_InnerText($ServiceApp.ApplicationPool.ProcessAccountName)
    #Append to "ServiceApplication" node
    $ServiceAppElement.AppendChild($ServiceAccountElement)

    $ServiceAppProxyElement = $XMLDocument.CreateElement("ProxyGroup")
    $ServiceAppProxyElement.Set_InnerText($ServiceApp.ServiceApplicationProxyGroup.FriendlyName)
    #Append to "ServiceApplication" node
    $ServiceAppElement.AppendChild($ServiceAppProxyElement)    

    #Append the Service Application data to "ServiceApplications" Node
    $ServiceAppRootElement.AppendChild($ServiceAppElement)
}
# Append Root Node to XML
$XMLDocument.AppendChild($ServiceAppRootElement)

#Save File
$XMLDocument.Save("c:\ServiceAppsInventory.xml")

它产生以下输出:

[玩转系统] 使用 PowerShell 将 SharePoint 配置清单导出到 XML

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

取消回复欢迎 发表评论:

关灯