I am trying to invoke SQLCMD from a PowerShell script but Powershell does not seem to like how I am passing -InputFile to Invoke-Sqlcmd. MY code looks something like this:
$files = Get-ChildItem -Path $PSScriptRoot -Recurse | Where-Object { $_.Name -match $regex};
Invoke-Sqlcmd -InputFile $files[0].FullName
$files is never empty as printing $files[0].FullName outputs a string like "D:\foo\foo.sql". When my application reaches this line, it throws an error pointing to the same line that I use Invoke-Sqlcmd and the character of the "$" in "$files".
I receives these two error messages:
Invoke-Sqlcmd : The pipeline has been stopped.
+ CategoryInfo : InvalidOperation: (:) [Invoke-Sqlcmd], PipelineStoppedException
+ FullyQualifiedErrorId : SqlExectionError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
Invoke-Sqlcmd : An expression of non-boolean type specified in a context where a condition is expected
+ CategoryInfo : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerShellSqlExecutionException
+ FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
Is there an issue with how I am feeding the filepath of the script I want to run? What would be the correct format. I have also tried to use .PSPath instead of .FullName but I get an error saying that the file path is not supported.
SqlError, and so it probably has nothing to do with what you do in PowerShell. You can get this, for example, withSELECT 1 WHERE 1, as1is not valid as a condition. - Jeroen Mostert