August 14, 2018 at 7:57 am
Hello
I'm trying to to insert values of sql server script into my hash table to be used later on another process, the piece that I'm using is:
$instancename ="instancename"
$databasename ="dba"
$FileMapping =@{}
$query=invoke-sqlcmd -ServerInstance $instanceName -Database $Databasename -Query "select n=name,f=filename from sysfiles"
$finalQuery=$query| %{"'"+'{0}'' =''{1}''{2}{3};' -f $_[0],$_[1],$_[2],$_[3] }
$FileMapping=$finalQuery.Clone()
I'm getting this error:
'FileMapping'. Cannot convert the "'DBA' ='S:\Data\DBA.mdf'; 'DBA_log' ='L:\Logs\DBA_log.ldf';" value of type "System.String" to
type "System.Collections.Hashtable".
any ides how to work around this issue?.
tThanks
August 14, 2018 at 10:02 am
Well what exactly do you want in your final hash table? For example this will give you a has table keyed to each file name with the value as the full name.
$query.ForEach({$fileMapping.Add($_.n, $_.f)})
Write-Output $FileMapping
August 14, 2018 at 10:57 am
I'm trying to pass the values of this hash table into "restore-dbaDatabase"
Restore-DbaDatabase -SqlInstance $instanceName -Path $backupPath -WithReplace -MaintenanceSolutionBackup -DatabaseName $Databasename -FileMapping $FileMapping -OutputScriptOnly |Out-File $File
so I was trying to populate $filemapping hash table dynamically.
August 14, 2018 at 11:09 am
I tried $query.ForEach({$fileMapping.Add($_.n, $_.f)}) and it worked perfectly.
Thank you very much.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply