Home Forums Programming Powershell restore-sqldatabase dynamic relocate files RE: restore-sqldatabase dynamic relocate files

  • I resolved this with the following.  The restore-sqldatabase requires variables for the relocate files.  I was previously adding the variable contents directly to the array. By creating a variable, then adding that variable value directly to the array it worked.  It's kind of like a dog chasing its tail, but alas, it works.

       $i=1
        $RelocateFiles = @()
       foreach($file in $NightlyDBFiles)
       {
        New-Variable -Name "Relocate$i" -Value (New-Object 'Microsoft.SqlServer.Management.Smo.RelocateFile, Microsoft.SqlServer.SmoExtended, Version=13.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' -ArgumentList $file.Name,$file.Physical_name)
        $RelocateFiles += get-variable -Name "Relocate$i" -ValueOnly
        Remove-Variable -Name "Relocate$i"
        $i++
     
       }