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.

Alex
  • 3
  • 2
  • Does it work if you specify a rooted path? `-InputFile C:\Scripts\01_Upload.sql` – Mathias R. Jessen Jul 31 '20 at 09:47
  • What about if you just remove .\ so its like Invoke-Sqlcmd -ServerInstance 'xxx-xx-xxxx' -Database 'TestDB' -InputFIle .\01_Upload.sql – Daniel Björk Jul 31 '20 at 09: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*] – Lee_Dailey Jul 31 '20 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. – Alex Jul 31 '20 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. – Alex Jul 31 '20 at 10:16

1 Answers1

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

Daniel Björk
  • 2,475
  • 1
  • 19
  • 26
  • Thanks! This must've been a recent update to the SQL cmdlet, because it has been working previously (admittedly not used for 12 months). – Alex Jul 31 '20 at 10:33