Get-SqlData?

Aug 25, 2010 at 12:40 AM

I am trying to use Get-SqlData to run t-sql to get the logical file names for a database so I can pass them to Invoke-SQLRestore but I am not having any success getting the results from Get-SqlData into a variable.  Below is the two lines of code I have.

Thanks

$DBLogSQL = "`"select name from sys.master_files where database_id = DB_ID(N'$dbname') and physical_name not like '%ldf'`""
$DBDataFileName = Get-SqlData $primary $dbname $DBDataSQL

Coordinator
Aug 25, 2010 at 2:47 AM

I see a few problems, first your query has some extra double quotes and should be this:

$DBLogSQL = "select name from sys.master_files where database_id = DB_ID(N'$dbname') and physical_name not like '%ldf'"

Second the important thing to remember with PowerShell everything is an object, so you'll need to reference the column name as properties. Here's one way to do it, if you're only expecting a single record in PowerShell V2:

$DBDataFileName = get-sqldata  -sqlserver "$primary" -dbname pubs -qry $DBLogSQL | select -ExpandProperty name

Third declare a variable $DBLogSQL, but then use a variable $DBDataSQL