微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 Azure PowerShell 中的错误消息中隐藏敏感值?

如何解决如何在 Azure PowerShell 中的错误消息中隐藏敏感值?

在 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"
}

我错过了什么?。

解决方法

由于这是一个外部应用程序,您将无法使用 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'
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。