I am using a fairly simple PowerShell script to automate the process of restoring databases:
#Script to restore database.
$serverInstance = $args[0]
$dbName = $args[1]
$file = $args[2]
$dataDestination = $args[3]
$logDestination = $args[4]
Import-Module sqlps
$relocateData = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile(###LOGICALFILENAME###, $dataDestination)
$relocateLog = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile(###LOGICALFILENAME###, $logDestination)
Restore-SqlDatabase -ServerInstance $serverInstance -Database $dbName -BackupFile $file -RelocateFile @($relocateData,$relocateLog)
I am looking for a way to dynamically fetch the logical file names of the files contained in a database backup ($file) and store them into a variable so the can be renamed accordingly.
Anyone have any ideas? I've been banging my head against this for way too long! :)
Thanks for your help!

As always with Powershell and SQL Server Invoke-Sqlcmd is your friend. It returns a DataTable that's simple to navigate. EG