问题描述
我构建了一个新的 Azure Pipeline 并添加了这个“Azure CLI”任务来尝试运行一些 Azure CLI Powershell 脚本。首先,我想对现有的 AppInsights 资源进行一些检查。 (Azure CLI) https://github.com/microsoft/azure-pipelines-tasks/blob/master/Tasks/AzureCLIV2/Readme.md
在 Azure CLI 任务中运行“Get-AzApplicationInsights”时出现此错误。
'Get-AzApplicationInsights' : The term 'Get-AzApplicationInsights' is not recognized as the name of a cmdlet,function.
整个 yaml 脚本如下所示:
steps:
- task: AzureCLI@2
displayName: 'Azure CLI Powershell'
inputs:
azureSubscription: ####
scriptType: ps
scriptLocation: inlinescript
inlinescript: |
Write-Output "RESULTS:"
az config set extension.use_dynamic_install=yes_without_prompt
Get-AzApplicationInsights -ResourceGroupName ############# -Name ############## Select-String -Pattern "PricingPlan"
我是否遗漏了为什么无法识别 cmdlet?应该先导入一个模块吗?
解决方法
要运行 Azure powershell Get-AzApplicationInsights
,只需在 Azure PowerShell task 中运行它。
或者,如果您想使用 Azure CLI 任务,您可以直接使用 CLI 命令 az monitor app-insights component show
而不是 powershell 命令。
同意王悦的观点。
勾选这个 doc,Get-AzApplicationInsights
是 power shell CMD install 还是 Azure CLI,我们应该通过任务 Azure Power Shell 而不是 Azure CLI 任务来运行它。
构建定义示例:
- task: AzurePowerShell@5
displayName: 'Azure PowerShell script: InlineScript'
inputs:
azureSubscription: '{Subscription}'
ScriptType: InlineScript
Inline: 'Get-AzApplicationInsights -ResourceGroupName "{Resource Group Name}" -Name "{Application Insights name}" -IncludePricingPlan'
azurePowerShellVersion: LatestVersion
结果: