【发布时间】:2020-01-17 15:18:35
【问题描述】:
我正在尝试从 PowerShell 脚本调用 SQLCMD,但 Powershell 似乎不喜欢我将 -InputFile 传递给 Invoke-Sqlcmd 的方式。我的代码看起来像这样:
$files = Get-ChildItem -Path $PSScriptRoot -Recurse | Where-Object { $_.Name -match $regex};
Invoke-Sqlcmd -InputFile $files[0].FullName
$files 永远不会为空,因为打印 $files[0].FullName 会输出类似“D:\foo\foo.sql”的字符串。当我的应用程序到达这一行时,它会抛出一个错误,指向我使用 Invoke-Sqlcmd 和“$files”中的“$”字符的同一行。
我收到以下两条错误消息:
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
我如何输入要运行的脚本的文件路径有问题吗?什么是正确的格式。我也尝试使用 .PSPath 而不是 .FullName 但我收到一条错误消息,指出不支持文件路径。
【问题讨论】:
-
这是脚本中的命令导致的错误,可以从
SqlError中识别出来,因此它可能与您在 PowerShell 中执行的操作无关。例如,您可以使用SELECT 1 WHERE 1来获取此信息,因为1作为条件无效。
标签: powershell sqlcmd invoke-sqlcmd