.DESCRIPTION
Many SQL Server database administrators use Ola Hallengren's SQL Server Maintenance Solution which can be found at http://ola.hallengren.com
Hallengren uses a predictable backup structure which made it relatively easy to create a script that can restore an entire SQL Server database instance, down to the master database (next version), to a new server. This script is intended to be used in the event that the originating SQL Server becomes unavailable, thus rendering my other SQL restore script (http://goo.gl/QmfQ6s) ineffective.

.PARAMETER SqlServer
Required. The SQL Server to which you will be restoring the databases.

.PARAMETER ReuseSourceFolderStructure
Restore-SqlBackupFromDirectory will restore to the default user data and log directories, unless this switch is used. Useful if you're restoring from a server that had a complex db file structure.

.PARAMETER Databases
Migrates ONLY specified databases. This list is auto-populated for tab completion.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

.OUTPUTS
$true if success
$true if failure
#>[CmdletBinding()]param([Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()][object]$server,[Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()][string]$dbname,[Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()][string]$backupfile,[Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()][string]$filetype,[Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()][object]$filestructure)$servername=$server.name$server.ConnectionContext.StatementTimeout=0$restore=New-Object"Microsoft.SqlServer.Management.Smo.Restore"$restore.ReplaceDatabase=$trueforeach($filein$filestructure.values){$movefile=New-Object"Microsoft.SqlServer.Management.Smo.RelocateFile"$movefile.LogicalFileName=$file.logical$movefile.PhysicalFileName=$file.physical$null=$restore.RelocateFiles.Add($movefile)}try{$percent=[Microsoft.SqlServer.Management.Smo.PercentCompleteEventHandler]{Write-Progress-id1-activity"Restoring $dbname to $servername"-percentcomplete$_.Percent-status([System.String]::Format("Progress: {0} %",$_.Percent))}$restore.add_PercentComplete($percent)$restore.PercentCompleteNotification=1$restore.add_Complete($complete)$restore.ReplaceDatabase=$true$restore.Database=$dbname$restore.Action=$filetype$restore.NoRecovery=$true$device=New-Object-TypeNameMicrosoft.SqlServer.Management.Smo.BackupDeviceItem$device.name=$backupfile$device.devicetype="File"$restore.Devices.Add($device)Write-Progress-id1-activity"Restoring $dbname to $servername"-percentcomplete0-status([System.String]::Format("Progress: {0} %",0))$restore.sqlrestore($server)Write-Progress-id1-activity"Restoring $dbname to $servername"-status"Complete"-Completedreturn$true}catch{$x=$_.ExceptionWrite-Warning"Restore failed: $x"return$false}}if(!([string]::IsNullOrEmpty($Path))){if(!($Path.StartsWith("\\"))){throw"Path must be a valid UNC path (\\server\share)."}if(!(Test-Path$Path)){throw"$Path does not exist or cannot be accessed."}}Write-Output"Attempting to connect to SQL Server.."$server=Connect-SqlServer-SqlServer$SqlServer-SqlCredential$SqlCredential$server.ConnectionContext.StatementTimeout=0if($server.versionMajor-lt8-and$server.versionMajor-lt8){throw"This script can only be run on SQL Server 2000 and above. Quitting."}if($server.versionMajor-eq8-and$IncludeSystemDbs){throw"Migrating system databases not supported in SQL Server 2000."}# Convert from RuntimeDefinedParameter object to regular array$Databases=$psboundparameters.Databases$Exclude=$psboundparameters.Exclude}PROCESS{$dblist=@();$skippedb=@{};$migrateddb=@{};$systemdbs=@("master","msdb","model")$subdirectories=(Get-ChildItem-Directory$Path).FullNameforeach($subdirectoryin$subdirectories){if((Get-ChildItem$subdirectory).Name-eq"FULL"){$dblist+=$subdirectory;continue}}if($dblist.count-eq0){throw"No databases to restore. Did you use the correct file path? Format should be \\fileshare\share\sqlbackups\sqlservername"}foreach($dbin$dblist){$full=Get-ChildItem"$db\FULL\*.bak"|sortLastWriteTime|select-last1$since=$full.LastWriteTime;$full=$full.FullName$diff=$null;$logs=$nullif(Test-Path"$db\DIFF"){$diff=Get-ChildItem"$db\DIFF\*.bak"|Where{$_.LastWriteTime-gt$since}|sortLastWriteTime|select-last1$since=$diff.LastWriteTime;$diff=$diff.fullname}if(Test-Path"$db\LOG"){$logs=(Get-ChildItem"$db\LOG\*.trn"|Where{$_.LastWriteTime-gt$since})$logs=($logs|Sort-ObjectLastWriteTime).Fullname}$restore=New-Object"Microsoft.SqlServer.Management.Smo.Restore"$device=New-Object-TypeNameMicrosoft.SqlServer.Management.Smo.BackupDeviceItem$full,"FILE"$restore.Devices.Add($device)try{$filelist=$restore.ReadFileList($server)}catch{throw"File list could not be determined. This is likely due to connectivity issues or tiemouts with the SQL Server, the database version is incorrect, or the SQL Server service account does not have access to the file share. Script terminating."}$header=$restore.ReadBackupHeader($server)$dbname=$header.DatabaseNameif($systemdbs-contains$dbname){continue}if(!([string]::IsNullOrEmpty($Databases))-and$Databases-notcontains$dbname){continue}if(!([string]::IsNullOrEmpty($Exclude))-and$Exclude-contains$dbname){$skippedb.Add($dbname,"Explicitly Skipped")Continue}if($systemdbs-contains$dbname){continue}if($server.databases[$dbname]-ne$null-and!$force-and$systemdbs-notcontains$dbname){Write-Warning"$dbname exists at $SqlServer. Use -Force to drop and migrate."$skippedb[$dbname]="Database exists at $SqlServer. Use -Force to drop and migrate."continue}if($server.databases[$dbname]-ne$null-and$force-and$systemdbs-notcontains$dbname){If($Pscmdlet.ShouldProcess($SqlServer,"DROP DATABASE $dbname")){Write-Output"$dbname already exists. -Force was specified. Dropping $dbname on $SqlServer."$dropresult=Remove-SqlDatabase$server$dbnameif(!$dropresult){$skippedb[$dbname]="Database exists and could not be dropped.";continue}}}$filestructure=Get-OfflineSqlFileStructure$server$dbname$filelist$ReuseSourceFolderStructureif($filestructure-eq$false){Write-Warning"$dbname contains FILESTREAM and filestreams are not supported by destination server. Skipping."$skippedb[$dbname]="Database contains FILESTREAM and filestreams are not supported by destination server."continue}$backupinfo=$restore.ReadBackupHeader($server)$backupversion=[version]("$($backupinfo.SoftwareVersionMajor).$($backupinfo.SoftwareVersionMinor).$($backupinfo.SoftwareVersionBuild)")Write-Output"Restoring FULL backup to $dbname to $SqlServer"$result=Restore-Database$server$dbname$full"Database"$filestructureif($result-eq$true){if($diff){Write-Output"Restoring DIFFERENTIAL backup"$result=Restore-Database$server$dbname$diff"Database"$filestructureif($result-ne$true){$result|fl-force;return}}if($logs){Write-Output"Restoring $($logs.count) LOGS"foreach($login$logs){$result=Restore-Database$server$dbname$log"Log"$filestructure}}}if($result-eq$false){Write-Warning"$dbname could not be restored.";continue}if($norecovery-eq$false){$sql="RESTORE DATABASE [$dbname] WITH RECOVERY"try{$server.databases['master'].ExecuteNonQuery($sql)$migrateddb.Add($dbname,"Successfully restored.")Write-Output"Successfully restored $dbname."}catch{Write-Error"$dbname could not be set to recovered."}try{try{$sa=Get-SqlSaLogin$server}catch{$sa="sa"}$server.databases.refresh()$server.databases[$dbname].SetOwner($sa)$server.databases[$dbname].Alter()Write-Output"Successfully changed $dbname dbowner to sa"}catch{Write-Error"Could not update dbowner to sa."}}}#end of for each database folder}END{#Clean up$server.ConnectionContext.Disconnect()Write-Output"Database restores complete"}}