【问题标题】:How to hide sensitve values in error messages in Azure PowerShell?如何在 Azure PowerShell 的错误消息中隐藏敏感值?
【发布时间】:2023-04-01 08:55:01
【问题描述】:

在 azure Powershell 中,我使用命令“az vm run-command invoke”对虚拟机执行脚本,问题是由于某种原因它不起作用时,它会在错误消息中打印所有参数的值清除,并且这些参数是敏感数据,出于安全原因,我不必在错误消息中打印。

这是我尝试执行的:

az vm run-command invoke -g $ResourceGroupName -n $VMname[-1] --command-id RunPowerShellScript --scripts @$pathScript --parameters "param1=$($Data1)" `
    "param2=$($Data2)" 

在我的情况下,Data1 可能是敏感的,我从不在代码中打印该值,但在错误消息中,该值以清晰的形式显示,我不必允许这样做。

请记住,当我添加正确的参数时,@$pathScript 中定义并在机器中执行的脚本运行良好。

我尝试将代码放入 try catch 块中,但这仍然会抛出错误消息,并且不会打印在 catch 部分中添加的消息

try{
    az vm run-command invoke -g $ResourceGroupName -n $VMname[-1] --command-id RunPowerShellScript --scripts @$pathScript --parameters "param1=$($Data1)" `
    "param2=$($Data2)" 
}catch{
    Write-Error "Error : Please check if your are passing the good arguments - Check the code if you need and debug it"
}

我错过了什么?

【问题讨论】:

  • param1 是否在您的脚本中定义为安全字符串?
  • 好点@Thomas,我会检查一下

标签: azure powershell azure-cloud-shell sensitive-data


【解决方案1】:

由于这是一个外部应用程序,您将无法使用 try/catch 捕获错误。您可以尝试将错误流重定向到 $null (2>$null) 以便看不到它,然后检查 $LASTEXITCODE。下面我只是大致检查 $LASTEXITCODE 是否不等于 0 并生成错误,但您可能想检查您得到的确切退出代码是什么,然后只写错误。

az vm run-command invoke -g $ResourceGroupName -n $VMname[-1] --command-id RunPowerShellScript --scripts @$pathScript --parameters "param1=$($Data1)" `
    "param2=$($Data2)" 2>$null
if ($LASTEXITCODE) {
    Write-Error 'Error : Please check if your are passing the good arguments - Check the code if you need and debug it'
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-24
    • 1970-01-01
    • 1970-01-01
    • 2018-01-31
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    • 2010-11-30
    相关资源
    最近更新 更多