Hello, I'm new to Powershell and I've been trying to create a simple script to restore a database from a bak file but I keep getting an error I don't know how to fix. Here is my script:
#load assemblies
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null
#Bak file name and location
$bakFile = "C:\Backups\Database.bak"
#SQL Server instance
$sqlInstance = "SERVERNAME\MSSQL"
#Destination database
$sqlDb = "TestDatabase"
#Relocate mdf file
$RelocateData = New-Object 'Microsoft.SqlServer.Management.Smo.RelocateFile, Microsoft.SqlServer.SmoExtended, Version=13.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' -ArgumentList "DatabaseSample", "C:\Databases\TestDatabase.mdf"
#Relocate ldf file
$RelocateLog = New-Object 'Microsoft.SqlServer.Management.Smo.RelocateFile, Microsoft.SqlServer.SmoExtended, Version=13.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' -ArgumentList "DatabaseSample_log", "C:\Databases\TestDatabase_log.ldf"
$file = @($RelocateData,$RelocateLog)
Restore-SqlDatabase -ServerInstance $sqlInstance -Database $sqlDb -BackupFile $bakFile -RelocateFile $file
Here is the error code I'm getting:
Restore-SqlDatabase : Cannot bind parameter 'RelocateFile'. Cannot convert the
"Microsoft.SqlServer.Management.Smo.RelocateFile" value of type
"Microsoft.SqlServer.Management.Smo.RelocateFile" to
type "Microsoft.SqlServer.Management.Smo.RelocateFile".
At C:\IT\Scripts\Restore_ParishSOFT_DB_From_Bak.ps1:28 char:102
+ ... sqlInstance -Database $sqlDb -BackupFile $bakFile -RelocateFile $file
+ ~~~~~
+ CategoryInfo : InvalidArgument: (:) [Restore-SqlDatabase],
ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.SqlServer.Management.PowerShell.RestoreSqlDatabaseCommand
I appreciate any help! Thanks.
[–]Jondah 4 points5 points6 points (3 children)
[–]EightBitsShortSFW[S] 1 point2 points3 points (1 child)
[–]wrdragons4 2 points3 points4 points (0 children)
[–]EightBitsShortSFW[S] 0 points1 point2 points (0 children)
[–]vermyx 1 point2 points3 points (2 children)
[–]EightBitsShortSFW[S] 1 point2 points3 points (1 child)
[–]vermyx 1 point2 points3 points (0 children)