Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

To restore an SQL Server 2008 database, I would lile to be able to just do something like this in powershell:

./restore.ps1 DatabaseName.bak

Then the powershell script would by convention restore it to a database with name "DatabaseName". It would disconnect any user connected to this database so that it can restore the DB. It would store the mdf and ldf in the default location.

This would mainly be while developing on my personal machine. Just a quick way to restore a DB.

Anyone has such a script?

Thanks

share|improve this question
    
If you're familiar with ADO.NET it isn't hard to create some wrappers around SqlConnection/SqlComment to set up parameters from a hash and then call ExecuteNonQuery. –  Richard Feb 27 '11 at 10:51
add comment

3 Answers

up vote 3 down vote accepted

I created a script and posted it on PoshCode which should meet your requirements. The script uses the SQLPSX sqlserver module. Note I had to add a restore file list only option to the invoke-sqlrestore function, so you'll need to grab the module from source code section rather than download section. This will be added to a future release of SQLPSX:

Module: http://sqlpsx.codeplex.com/SourceControl/changeset/view/58378#564810

Script: http://poshcode.org/2530

share|improve this answer
add comment

Please refer PowerShell script to backup a database and restore on other server http://bi-bigdata.com/2012/12/16/power-shell-script-to-backup-a-database-and-restore-on-other-server/

share|improve this answer
add comment

I am currently using custom script created by following this article. The best part is that it does not use any third party tools or script, it only need couple of Microsoft modules installed to gain access to a whole slew of new PowerShell CMDlets. You can follow this post to install them on your or any other management machine and use it to connect to any MSSQL since 2000 to manage it remotely with a single script.

The commands are multiple and give you easy access to every option, parameter and action of the SQL server. You can list them with Get-Command -Module SQLPS once you load the module.

Here you can find more info about it.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.