Hello All,
I have been a longtime reader of MSDN & TechNet(Great community & folks!), but this is the first time I have asked a question. I am attempting to use a power shell script that I found online. I have a reasonable level of knowledge about .net extension
and power shell. I gave this script one parameter and wrapped everything inside a function. Below is the script code I found online along with the image of an error. Below those is my problem. Thanks for reading - Joe
Function Restore-RenameDBA{ Param( [parameter(Mandatory=$True)] [String]$Path ) #================================================================| # Restore a Database using PowerShell and SQL Server SMO | # Restore to the a new database name, specifying new mdf and ldf | #================================================================| #clear screen cls #load assemblies [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null #Need SmoExtended for backup [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null [Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | Out-Null [Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum") | Out-Null $backupFile = $Path ##$backupFile = $Path #we will query the database name from the backup header later $server = New-Object ("Microsoft.SqlServer.Management.Smo.Server") "(local)" $backupDevice = New-Object("Microsoft.SqlServer.Management.Smo.BackupDeviceItem") ($backupFile, "File") $smoRestore = new-object("Microsoft.SqlServer.Management.Smo.Restore") Write-Host SMO Server is $server #restore settings $smoRestore.NoRecovery = $false; $smoRestore.ReplaceDatabase = $true; $smoRestore.Action = "Database" $smoRestorePercentCompleteNotification = 10; $smoRestore.Devices.Add($backupDevice) #get database name from backup file $smoRestoreDetails = $smoRestore.ReadBackupHeader($server) #display database name "Database Name from Backup Header : " +$smoRestoreDetails.Rows[0]["DatabaseName"] #give a new database name $smoRestore.Database =$smoRestoreDetails.Rows[0]["DatabaseName"] + "_Temp" #specify new data and log files (mdf and ldf) $smoRestoreFile = New-Object("Microsoft.SqlServer.Management.Smo.RelocateFile") $smoRestoreLog = New-Object("Microsoft.SqlServer.Management.Smo.RelocateFile") #the logical file names should be the logical filename stored in the backup media $smoRestoreFile.LogicalFileName = $smoRestoreDetails.Rows[0]["DatabaseName"] $smoRestoreFile.PhysicalFileName = $server.Information.MasterDBPath + "\" + $smoRestore.Database + "_Data.mdf" $smoRestoreLog.LogicalFileName = $smoRestoreDetails.Rows[0]["DatabaseName"] + "_Log" $smoRestoreLog.PhysicalFileName = $server.Information.MasterDBLogPath + "\" + $smoRestore.Database + "_Log.ldf" $smoRestore.RelocateFiles.Add($smoRestoreFile) $smoRestore.RelocateFiles.Add($smoRestoreLog) #restore database $smoRestore.SqlRestore($server) }
When I do a check to see what '(local)' is using or sees as its host for SQL management studio SP1 - that works.
Write-Host SMO Server is $server
But when it comes down to the final line of code(below) I get the exception above.
$smoRestore.SqlRestore($server)
In short, my computer is named JOEPC and I have even tried hardcoding JOEPC instead of $Server, but either way I get the same error. Any help or insight would be greatly appreciated.
Thanks again,
Joseph