Search This Blog

Wednesday, December 19, 2018

SharePoint- Get All DBName and WebApplication using PowerShell




Step : Copy and Save .PS1 run the powershell


# add the snapin so executed directly from the standard PowerShell console
Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

# Get the current script execution path so our CSV file gets saved back there
[string]$curloc = get-location
out-file -filepath "$curloc\DBSites.csv"

# Create the empty CSV array
$arrCSV = @()
# Get an object of all of the Web Applications in the Farm
$webapps = get-spwebapplication

# Loop through each Web App

foreach ($webapp in $webapps)
{   
    # Loop through each database in the Web Application
    foreach ($database in $webapp.contentdatabases)
    {
       # Create the empty record object and start populating it with information about the current Web Application and database
       $objDB = "" | select DBName,DBStatus,WebApplication
       $objDB.DBName = $database.name
       $objDB.DBStatus = $database.status
       $objDB.WebApplication = $webapp.URL

       # Add the new record as a new node in the CSV array
       $arrCSV += $objDB 
    }
    # Now that we’ve added all of the records to the array we need to write the CSV array out to the file system
    $arrCSV | Export-Csv "$curloc\DBSites.csv" -NoTypeInformation
}




ref: https://sharepoint.rackspace.com/how-to-start-using-powershell-with-sharepoint

No comments:

Post a Comment