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,
Rahul
Quote of the day:
Indecision may or may not be my problem. - Jimmy Buffett