Saturday, July 9, 2016

Export SharePoint Configuration Inventory to XML using PowerShell

Requirement: We had to compare certain SharePoint inventories between different environment, such as web applications, content databases, etc. So had to extract and export SharePoint configurations into XML file.

PowerShell Script to Export Web Applications List into XML
SharePoint export data to 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")

Script output:
sharepoint export data to xml

Inventory Web Applications and Its Content Databases associated to XML
Lets add database information to it:
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 to Inventory SharePoint Service Applications to XML
Lets Inventory SharePoint 2016 Service Applications:
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")
and it produces the following output:
sharepoint export to xml



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!


You might also like:

Related Posts Plugin for WordPress, Blogger...