Powershell Tip #3 - Enumerate all Content Databases in a MOSS 2007 farm

by rahul 5/28/2012 3:21:47 PM

Want to enumerate all the Content databases of a MOSS 2007 Farm using Powershell? Use the following script. It would return all the Content Databases along with Web Application name in an XML file.

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$WebService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
$FileName = "C:\ContentDatabases.xml"  # Change the location of the log file
[xml]$xml = "<ContentDatabases></ContentDatabases>"

$properties = @"
<WebApplicationName>{0}</WebApplicationName>
<DatabaseName>{1}</DatabaseName>
"@

$DBName = [Microsoft.SharePoint.Administration.SPContentDatabase].GetProperty("Name")
$DBServer= [Microsoft.SharePoint.Administration.SPContentDatabase].GetProperty("ServiceInstance")

# Enumerate through all Web applications in the farm
foreach($WebApplication in $WebService.WebApplications)
{
    $ContentDBCollection = $WebApplication.ContentDatabases
    $WebAppName = $WebApplication.name            
    
    foreach($ContentDB in $ContentDBCollection)
    {
        $CurrentDBName = $DBName.GetValue($ContentDB, $null)
        $element = $xml.CreateElement("ContentDatabase")
        $element.InnerXml = $properties -f $WebAppName, $CurrentDBName
        [void]$xml["ContentDatabases"].AppendChild($element)
    }
    $xml.Save($FileName)
} 

Hope this helps, Wave
Rahul


Quote of the day:
Indecision may or may not be my problem. - Jimmy Buffett


blog comments powered by Disqus

Rahul Soni

Rahul Soni  Twitter

 LinkedIn

 Facebook

 Email me



Vivek Kumbhar

Vivek Kumbhar  Twitter

 LinkedIn

 Facebook

 Email me


Stack Exchange

profile for Vivek at Server Fault, Q&A for system administrators and IT professionals

profile for Rahul Soni at Stack Overflow, Q&A for professional and enthusiast programmers

Calendar

<<  May 2013  >>
MoTuWeThFrSaSu
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

View posts in large calendar

All Items
Sign in

Visit Microsoft's Site

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2013, Rahul Soni

Powered by BlogEngine.NET 1.4.5.0