Hey Guys,
I've about 180 MDF (and LDF files) in a folder and I'm trying to automate the creation of SQL databases from these files.
I've tried to use the Invoke-SqlCmd, but it doesn't seem to interpret the variables I've included in the SQL code.
<# .Synopsis Create new DBs from restored MDF/LDF files .DESCRIPTION Long description .REQUIRES Requirements .EXAMPLE Example of how to use this cmdlet #> #------------------------------------- Set-StrictMode -Version Latest $Error.Clear() Add-PSSnapin sqlserverprovidersnapin100 Add-P5snapin sqlservercmdletsnapin100 #------------------------------------- $AllMDF = Get-ChildItem E:\MSSQL1O_50.SHAREPOINT\MSSQL\DATA Foreach ($i in $AllMDF) { Invoke-Sqlcmd -ServerInstance “BE-SQL2008-O1\SHAREPOINT” -Query “ Use master Go Create DATABASE [‘Si_.BaseName] ON (Filename = N’E:V4SSQL1O_SO.SHAREPOINT\MSSQL\DATA\`$i.BaseName.mdf’), (Filename = N’F:\MSSQL1O_50.SHAREPOINT\J4SSQL\Logs\`$i.BaseName.ldf’) For ATTACH; "
}
But this returns me the following error; so we can see that the script did not interpret `$i.BaseName as a variable.
Any idea how I could achieve this? Or what I'm missing in my script ?