0

I have a Powershell script which I have used successfully for some time but only recently it has stopped liking the relative paths that are setup. Any idea why or how this might happen?

The location of the script I am trying to call is C:\Scripts\01_Upload.sql.

$scripts = "C:\Scripts"
cd $scripts
Invoke-Sqlcmd -ServerInstance 'xxx-xx-xxxx' -Database 'TestDB' -InputFIle .\01_Upload.sql

I have censored the server name for security. This line has previously worked, I've tried different also just using 01_Upload.sql and also by surround it in quotes but it now returns this error:

Invoke-Sqlcmd : The given path's format is not supported.

If I include the full path, the code works fine. Why are my relative paths not working? I have recently updated from Windows 7 to Windows 10 if this has impacted things.

5
  • Does it work if you specify a rooted path? -InputFile C:\Scripts\01_Upload.sql Commented Jul 31, 2020 at 9:47
  • What about if you just remove .\ so its like Invoke-Sqlcmd -ServerInstance 'xxx-xx-xxxx' -Database 'TestDB' -InputFIle .\01_Upload.sql Commented Jul 31, 2020 at 9:47
  • since relative paths are almost always a really bad idea ... why don't you take this as an opportunity to correct a weak point in your code & get rid of those totally needless but ludicrously risky relative paths? [grin] Commented Jul 31, 2020 at 10:11
  • @MathiasR.Jessen Yep, if I specify a full rooted path it works completely fine. Was just curious why my relative paths have just broke all of a sudden in a script that's always worked. Commented Jul 31, 2020 at 10:16
  • @DanielBjörk Yep, as mentioned in the post I tried it without the .\ and it still returns the same error. Only way I can get it to work is to use the full path. Commented Jul 31, 2020 at 10:16

1 Answer 1

1

According to documentation full path to file is required.

-InputFile Specifies a file to be used as the query input to this cmdlet. The file can contain Transact-SQL statements, XQuery statements, and sqlcmd commands and scripting variables. Specify the full path to the file. Spaces are not allowed in the file path or file name.

https://learn.microsoft.com/en-us/powershell/module/sqlserver/invoke-sqlcmd?view=sqlserver-ps

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This must've been a recent update to the SQL cmdlet, because it has been working previously (admittedly not used for 12 months).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.