• I put together this script to copy a production database backup to a development server and restore it. The job will run at night, and keeps the development data parallel to production. We are using SQL Server 2012 SP2 (build 11.0.5058).

    [font="Courier New"]import-module SQLPS -DisableNameChecking

    #

    #knock off existing connections

    Invoke-Sqlcmd "ALTER DATABASE DB_VIEWS SET OFFLINE WITH ROLLBACK IMMEDIATE;"

    Invoke-Sqlcmd "ALTER DATABASE DB_VIEWS SET ONLINE;"

    #

    # find the latest full backup to be restored

    $a = Get-ChildItem Filesystem::\\RC-DBPROD\Backup\DB_VIEWS\DB_VIEWS*.bak | sort LastWriteTime | select -last 1

    #

    # production DB uses different disks then this development server

    $RelocateData = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile("DB_VIEWS", "C:\MSSQL\DB_VIEWS.mdf")

    $RelocateLog = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile("DB_VIEWS_log", "C:\MSSQL\DB_VIEWS_log.ldf")

    Restore-SqlDatabase -ServerInstance RC-DBUPD58 -Database DB_VIEWS -BackupFile $a -RelocateFile @($RelocateData,$RelocateLog) -replacedatabase

    [/font]