Let’s say you have this ginormous farm with hundreds of databases and you want to update the farm to a new CU. First thing you can do is get a list of your content databases using this powershell
Get-SPContentDatabase | ft DisplayName | out-file c:\PathToFile.txt ## You may also want to know the web application that they're attached Get-SPContentDatabase | ft DisplayName, webApplication | out-file c:\PathToSomeOtherFile.txt ## This above powershell will give you the web application name, if you want the url run something like this Get-SPWEbApplication | ft URL
Once you’ve got that list of database names and you know the associated web application URL, you can go ahead and dismount the databases, and one way to do that is with a modified version of the script you will use to mount the databases. The way it works is you get the database names in a text file and then call this powershell:
Note: This example assumes you know the name of the text file and the path to it. You save this powershell in a .ps1 file and you put the ps1 in the same directory where you have the databases.txt file. If you used the above powershell verbatim, then you need to change either the name of the text file from PathtoFile.txt to databases.txt, or you need to change the script that follows, Your call boss, you are in charge here!
Make sure the text file is just a list of database names and if you used the get-spcontentdatabase command above, make sure to remove the header info and the dashed lines, else SharePoint will try to create a database named name and ——-
add-pssnapin microsoft.sharepoint.powershell -EA 0 [string]$curloc = get-location $webappURL = Read-Host "Enter Web app URL:" $wa = Get-SPWebApplication $webappURL Get-spcontentdatabase $wa | DISmount-spcontentdatabase $db -webapplication $wa Write-Host -ForegroundColor Green "All databases dismounted from web application at $webappURL""
Notice: on line 8 above, we’ve got Dismount-spcontentdatabase being called into action, like some sort of rebel force, bent on the dismounting of all databases that were attached to whatever web application you entered into the screen when prompted….and oh ya! you will be prompted when you run this command. You will want to make sure you enter the exact URL of the web application including the http:// or https:// , e.g. https://chinet.governmentagencysharePoint.gov
Then go ahead and apply the CU to your SharePoint farm
When finished it is time to mount the databases back to the web application using the following powershell
add-pssnapin microsoft.sharepoint.powershell -EA 0 [string]$curloc = get-location $strDBs = Get-Content ("$curloc\databases.txt") $wa = Get-SPWebApplication "Update with the web application URL" foreach ($db in $strDBs) { mount-spcontentdatabase $db -webapplication $wa Write-Host -ForegroundColor Green "Mounted $db successfully!" }
Please take notice of line 10, this time we are mounting databases.
Finally, you will probably want to make sure all the databases are upgraded, so you can run this:
add-pssnapin microsoft.sharepoint.powershell -EA 0 [string]$curloc = get-location $strDBs = Get-Content ("$curloc\databases.txt") foreach ($db in $strDBs) { upgrade-spcontentdatabase $db -confirm:$false Write-Host -ForegroundColor Green "Upgraded $db successfully!" }
and if you get any errors, try this one
add-pssnapin microsoft.sharepoint.powershell -EA 0 [string]$curloc = get-location $strDBs = Get-Content ("$curloc\databases.txt") foreach ($db in $strDBs) { upgrade-spcontentdatabase $db -skipIntegrityChecks -confirm:$false Write-Host -ForegroundColor Green "Upgraded $db successfully!" }
A couple of gotchas with this approach:
- If your databases have been moved around alot between environments, some of them may have orphaned top sites, meaning make sure you know which database is attached to the top of each web application and make sure to mount that database first, as the get-spcontentdatabase cmdlet does not give you this info. If you fail to do this you could end up with the wrong content on the top of the site and a really bad user experience. Now nobody wants that, right?!??
- Make sure to attach the databases to the correct web application. This is why it’s so paramount to know your environment, not only with that first gotcha that you just read, but also with the overall topology of your farm.
I hope this helps someone down the road of their epic SharePoint journey. Please hit me up at stacy@anothersharepointblog.com if you have any questions about this post or any suggestions on how I could improve it. Make it a great day and have a good one!
Happy SharePointey goodness to you and yours!