在 Azure runbook 中调用 Sqlcmd 时,我应该使用 PowerShell 子表达式吗?

问题描述

我们将 Azure 自动化 powershell 运行手册用于 Azure sql 维护,类似于 https://www.2azure.nl/2020/07/28/how-to-use-azure-automation-to-maintain-sql-indexes-and-statistics/(和 Azure Automation Powersell Runbook fails: 'Invoke-Sqlcmd' is not recognized as the name of a cmdlet 答案)中所述

$sqlOutput = $(Invoke-sqlcmd -ServerInstance $AzuresqlServerName 
  -Username $Cred.UserName -Password $Cred.GetNetworkCredential().Password
  -Database $AzuresqlDatabaseName -Query $sql 
  -QueryTimeout 65535 -ConnectionTimeout 60 -Verbose) 4>&1
Write-Output $sqlOutput

但我们发现,如果出现错误(特别是当用户名错误时),即使错误记录在输出中,作业也会成功结束。 这是否意味着子表达式 $(command) 会地捕获异常? https://ss64.com/ps/syntax-operators.html 中未提及。

将子表达式保存到 $sqlOutput 的另一个问题是我们只能在 $(Invoke-sqlcmd) 完成后才能看到输出,而不是在处理过程中。 如果 $(Invoke-sqlcmd) 花费了 3 个多小时,Azure 停止了作业,我们无法看到哪些已处理,哪些未处理。

我是不是应该完全不使用子表达式而直接调用 Invoke-sqlcmd?

Invoke-sqlcmd -ServerInstance $AzuresqlServerName 
  -Username $Cred.UserName -Password $Cred.GetNetworkCredential().Password 
  -Database $AzuresqlDatabaseName -Query  $sql 
  -QueryTimeout 65535 -ConnectionTimeout 60 -Verbose) 4>&1

与子表达式相比,它有什么缺点吗?

解决方法

为了在Invoke-Sqlcmd中报错我加了2个参数

 -ErrorAction Stop   

来自Powershell Try Catch invoke-sqlcmd

 -AbortOnError  

来自Error detection from Powershell Invoke-Sqlcmd not always working?

还没确认,把子表达式保存到$SQLOutput有什么好处