This project is read-only.

Using the -file parameter to Invoke-SqlQuery

Mar 10, 2010 at 4:24 PM

My query text is kept in a file and I want to pass the file name to Invoke-SqlQuery using the '-file' parameter.

This works if my file is in my '$home' directory.

It does not work if the file is in some other directory.  In the following example the file is in the 'SQL Server Management Studio\Projects' directory, but Invoke-SqlQuery seems to be looking for it in my $home directory.

C:\Users\wirwin\Documents\SQL Server Management Studio\Projects   --   March 10; 11:18:31 AM
PS:11> invoke-sqlquery -server:$srv -database:$db -file:".\UP_Params_1.sql" -parameter:(${up_name="UP_USPO-1846"})
Cannot find path 'C:\Users\wirwin\UP_Params_1.sql' because it does not exist.
At C:\Users\wirwin\Documents\WindowsPowerShell\Modules\InvokeSqlQuery\InvokeSqlQuery.psm1:5 char:7
+ switch <<<<  -regex -file $file {
    + CategoryInfo          : ObjectNotFound: (C:\Users\wirwin\UP_Params_1.sql:String) [], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound

Am I doing something wrong here?

I can make it work by doing

invoke-sqlquery -server:$srv -database:$db -file:"$(pwd)\UP_Params_1.sql" -parameter:(${up_name="UP_USPO-1846"})

but it seems like this should not be necessary (and is not consistent with other Powershell usages)

Jun 2, 2010 at 11:20 PM

This problem just been corrected!

Please download version 1.3. 

Thanks for using Powershell4SQL!