Thursday, December 3, 2015

GetContentDB script for mounting/unmounting and upgrade during SharePoint patching


#Purpose:  Scans all attached Databases and can create mount/unmount scripts to add or drop all sharepoit content dbs. This script does not touch config or service application application database.

#The script is provided as is and holds no warranty.

#Usage:  create script to mount all databases

$ver = $host | select version
if ($ver.Version.Major -gt 1) {$host.Runspace.ThreadOptions = "ReuseThread"}
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) { Add-PSSnapin "Microsoft.SharePoint.PowerShell" }
$stuff = '$ver = $host | select version; if ($ver.Version.Major -gt 1) {$host.Runspace.ThreadOptions = "ReuseThread"}; if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) { Add-PSSnapin "Microsoft.SharePoint.PowerShell" }; $Log = ($EnvPrefix + "_Log_"  + (get-date -format "yyyymmdd_HHMMss") + ".PS1")'

$EnvPrefix = read-host "Enter Environment:  DEV | IT | UAT | PROD "

$fileAttach = ($EnvPrefix + "_AttachDB.ps1")
$fileDetach = ($EnvPrefix + "_DetachDB.ps1")

# Load Microsoft.SharePoint Assembly
[void] [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[void] [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Administration")

"# Detach all Content DB's "  | Out-File $fileDetach
"# Attach all Content DB's "  | Out-File $fileAttach
$stuff | Out-File $fileDetach
$stuff | Out-File $fileAttach 
foreach($webapp in get-spwebapplication)
{
 'get-date -format "yyyy-mm-dd HH:mm:ss"'| Out-File $fileDetach -Append
 'get-date -format "yyyy-mm-dd HH:mm:ss"'| Out-File $fileAttach -Append
 'write "Processing Web Application: ' + $webapp.Name + '"' | Out-File $fileDetach -Append
 'write "Processing Web Application: ' + $webapp.Name + '"' | Out-File $fileAttach -Append
    " "  | Out-File $fileDetach -Append
    " "  | Out-File $fileAttach -Append
 $get_Server = [Microsoft.SharePoint.Administration.SPContentDatabase].getmethod("get_Server")
 $get_Database = [Microsoft.SharePoint.Administration.SPContentDatabase].getmethod("get_Name")

 foreach($cd in $webapp.ContentDatabases)
      {
  $url = $webapp.GetResponseUri("Default").OriginalString
  $dbName = $get_Database.Invoke($cd, "instance,public", $null, $null, $null)
  $server = $get_Server.Invoke($cd, "instance,public", $null, $null, $null)
  'get-date -format "yyyy-mm-dd HH:mm:ss"'| Out-File $fileDetach -Append
  'get-date -format "yyyy-mm-dd HH:mm:ss"'| Out-File $fileAttach -Append
  "Mount-SPContentDatabase "+ $dbname + " -DatabaseServer "+ $server + " -WebApplication " + $url | Out-File $fileAttach -Append
  "dismount-SPContentDatabase "+ $dbname | Out-File $fileDetach -Append
  '$date = get-date -format "yyyy-mm-dd HH:mm:ss"; $date + ";Detach;" + ' + '"' + $dbName +'"' + ' | Out-File $log -Append ' | Out-File $fileDetach -Append
  '$date = get-date -format "yyyy-mm-dd HH:mm:ss"; $date + ";Attach;" + ' + '"' + $dbName +'"' + ' | Out-File $log -Append ' | Out-File $fileAttach -Append
        }
}

No comments:

Post a Comment