【问题标题】:A positional parameter cannot be found that accepts argument '+' error using SqlClient in Powershell在 Powershell 中使用 SqlClient 找不到接受参数“+”错误的位置参数
【发布时间】:2018-09-18 23:35:39
【问题描述】:

我在 Azure 上运行手册的 powershell 脚本中遇到错误:

Write-Error : A positional parameter cannot be found that accepts argument '+'.
At Test-Update-IndexesForallShardsFromShardManagerRunbook:61 char:61
+ 
    + CategoryInfo          : InvalidArgument: (:) [Write-Error], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.WriteErrorCommand

根据我在 Azure autmation 帐户上看到的运行作业的日志,我在以下代码中的某处查明了脚本中错误的根源:

$updateStatisticSql = "UPDATE STATISTICS [$Using:tableName] ( [$Using:statName] );"
$CmdUpdateStats=new-object system.Data.SqlClient.SqlCommand($updateStatisticSql, $Conn1)
$CmdUpdateStats.CommandTimeout=1500

Try
{
    $Ds=New-Object system.Data.DataSet
    $Da=New-Object system.Data.SqlClient.SqlDataAdapter($CmdUpdateStats)
    [void]$Da.fill($Ds)
}
Catch
{
    # Will catch the exception here so other statistics can be processed.
    Write-Error "Statistic " + $tableName + "(" + $statName + ") could not be updated. Investigate the statistic."
}

似乎在每行之后添加日志记录后,它不会在“填充”函数之后记录,所以我认为那里出了问题。但我没有看到错误与此功能之间的关系。它似乎也不是脚本中断错误,因为它永远不会进入捕获状态,并且其余脚本运行良好。即使出现错误,我也验证了统计信息已更新。

【问题讨论】:

    标签: powershell azure azure-sql-database azure-automation


    【解决方案1】:

    所以您看到的错误是因为您正在尝试使用连接构建字符串,这意味着您在调用 cmdlet 时使用空格和空格来分隔参数。将所有连接放入括号中:

    Write-Error ("Statistic " + $tableName + "(" + $statName + ") could not be updated. Investigate the statistic.")
    

    【讨论】:

    • 确实,愚蠢的我监督了这个......谢谢你的回答。
    猜你喜欢
    • 1970-01-01
    • 2015-05-21
    • 1970-01-01
    • 2019-05-07
    • 2017-11-06
    • 2016-05-27
    • 2021-04-12
    • 1970-01-01
    • 2016-07-06
    相关资源
    最近更新 更多