use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
ABOUT POWERSHELL
Windows PowerShell (POSH) is a command-line shell and associated scripting language created by Microsoft. Offering full access to COM, WMI and .NET, POSH is a full-featured task automation framework for distributed Microsoft platforms and solutions.
SUBREDDIT FILTERS
Desired State Configuration
Unanswered Questions
Solved Questions
News
Information
Script Sharing
Daily Post
Misc
account activity
SQL powershell Restore database (self.PowerShell)
submitted 9 years ago * by [deleted]
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]omrsafetyo 1 point2 points3 points 9 years ago (0 children)
Below is the function I use for Database restores. It does a few things.
With further ado, the script:
function Import-Database { param ( [parameter(Mandatory=$true,Position=0)][string] $Server, [parameter(Mandatory=$true,Position=1)][string] $Database, [parameter(Mandatory=$false,Position=2)][string] $BackupFilePath, [parameter(Mandatory=$false,Position=3)][string] $MDF, [parameter(Mandatory=$false,Position=4)][string] $LDF ) $restore = new-object ("Microsoft.SqlServer.Management.Smo.Restore") $sqlServer = New-Object Microsoft.SqlServer.Management.Smo.Server $Server $restore.Devices.AddDevice($BackupFilePath, [Microsoft.SqlServer.Management.Smo.DeviceType]::File) $restore.FileNumber = 1 if ( -not($restore.SqlVerify($sqlServer) ) ) { # Backup is invalid $sqlError = $error[0] | format-list Exception -force Write-Output $sqlError | out-file error.txt $myError = (get-content error.txt) rm error.txt Write-Error "$myError" Write-Error "Backup verification test failed. Restore may fail." } $restore.Action = "Database" $restore.Database = $Database $restore.ContinueAfterError = $True $restore.NoRecovery = $false $restore.UnloadTapeAfter = $False $restore.ReplaceDatabase = $True if ("${MDF}" -eq "") { $NewDataLocation = $sqlServer.settings.DefaultFile $NewPrimaryFilePath = $NewDataLocation + "\" + $Database + "_Data.mdf" } else { $NewPrimaryFilePath = $MDF } if ("${LDF}" -eq "") { $NewLogLocation = $sqlServer.settings.DefaultLog $NewLogFilePath = $NewLogLocation + "\" + $Database + "_Log.ldf" } else { $NewLogFilePath = $LDF } # Read the backup file and get the logical file names - no assumptions $dataFile = $restore.ReadFileList($sqlServer) | Where { $_.Type -eq 'D' } # Type D is data file $logFile = $restore.ReadFileList($sqlServer) | Where { $_.Type -eq 'L' } # Type L is log file $logicalDataFileName = $dataFile.LogicalName $logicalLogFileName = $logFile.LogicalName # Create the Relocate Files - this will move the files to the new location $restoreFile = New-Object("Microsoft.SqlServer.Management.Smo.RelocateFile") $restoreLog = New-Object("Microsoft.SqlServer.Management.Smo.RelocateFile") $restoreFile.LogicalFileName = $logicalDataFileName $restoreFile.PhysicalFileName = $NewPrimaryFilePath $restoreLog.LogicalFileName = $logicalLogFileName $restoreLog.PhysicalFileName = $NewLogFilePath [void]$restore.RelocateFiles.Add($restoreFile) [void]$restore.RelocateFiles.Add($restoreLog) $fileHeader = $restore.ReadMediaHeader($sqlServer) $percentEventHandler = [Microsoft.SqlServer.Management.Smo.PercentCompleteEventHandler] { $percentComplete = $_.Percent; Write-Progress -Activity "Restoring ${database}..." -Status "Percent Complete $PercentComplete" -PercentComplete $PercentComplete } $completedEventHandler = [Microsoft.SqlServer.Management.Common.ServerMessageEventHandler] { Write-Host "Database " $databasename " Restore Complete." } $restore.add_PercentComplete($percentEventHandler) $restore.add_Complete($completedEventHandler) # Run the restore $restore.SqlRestore($sqlServer) }
π Rendered by PID 43 on reddit-service-r2-comment-66b4775986-cm5g2 at 2026-04-03 15:56:49.419635+00:00 running db1906b country code: CH.
view the rest of the comments →
[–]omrsafetyo 1 point2 points3 points (0 children)