azure - Azure 自动化 Runbook 缺少必需参数
问题描述
我正在尝试在订阅中的所有虚拟机上设置标签,但在运行 Runbook 时不断出现错误。错误如下:
Get-AzureRmVM : Cannot process command because of one or more missing mandatory parameters: ResourceGroupName. At line:30
这是我的运行手册:
$azureConnection = Get-AutomationConnection -Name 'AzureRunAsConnection'
#Authenticate
try {
Clear-Variable -Name params -Force -ErrorAction Ignore
$params = @{
ServicePrincipal = $true
Tenant = $azureConnection.TenantID
ApplicationId = $azureConnection.ApplicationID
CertificateThumbprint = $azureConnection.CertificateThumbprint
}
$null = Add-AzureRmAccount @params
}
catch {
$errorMessage = $_
Throw "Unable to authenticate with error: $errorMessage"
}
# Discovery of all Azure VM's in the current subscription.
$azurevms = Get-AzureRmVM | Select-Object -ExpandProperty Name
Write-Host "Discovering Azure VM's in the following subscription $SubscriptionID Please hold...."
Write-Host "The following VM's have been discovered in subscription $SubscriptionID"
$azurevms
foreach ($azurevm in $azurevms) {
Write-Host "Checking for tag $vmtagname on $azurevm"
$tagRGname = Get-AzureRmVM -Name $azurevm | Select-Object -ExpandProperty ResourceGroupName
$tags = (Get-AzureRmResource -ResourceGroupName $tagRGname -Name $azurevm).Tags
If ($tags.UpdateWindow){
Write-Host "$azurevm already has the tag $vmtagname."
}
else
{
Write-Host "Creating Tag $vmtagname and Value $tagvalue for $azurevm"
$tags.Add($vmtagname,$tagvalue)
Set-AzureRmResource -ResourceGroupName $tagRGname -ResourceName $azurevm -ResourceType Microsoft.Compute/virtualMachines -Tag $tags -Force `
}
}
Write-Host "All tagging is done"
我尝试导入正确的模块,但这似乎不会影响结果。在 Cloud Shell 中运行相同的命令确实可以正常工作。
解决方案
我可以重现您的问题,错误是由这部分引起的Get-AzureRmVM -Name $azurevm
,运行此命令时,-ResourceGroupName
需要。
您需要使用Az
命令Get-AzVM -Name $azurevm
,它会起作用。
在 Cloud Shell 中运行相同的命令确实可以正常工作。
在 Cloud shell 中,azure 本质上使用新Az
模块来运行您的命令,您可以理解它Enable-AzureRmAlias
在命令之前运行,您可以通过调试模式检查。
Get-AzureRmVM -Name joyWindowsVM -debug
为了彻底解决您的问题,我建议您使用新Az
模块,因为该AzureRM
模块已被弃用并且不会更新。
请按照以下步骤操作。
1.在门户中导航到您的自动化帐户-> Modules
,检查您是否已导入模块Az.Accounts
,,,,如果没有Az.Compute
,Az.Resources
请转到Browse Gallery
->搜索并导入它们。
2.导入成功后,把你的脚本改成下面这样,就可以正常工作了。
$azureConnection = Get-AutomationConnection -Name 'AzureRunAsConnection'
#Authenticate
try {
Clear-Variable -Name params -Force -ErrorAction Ignore
$params = @{
ServicePrincipal = $true
Tenant = $azureConnection.TenantID
ApplicationId = $azureConnection.ApplicationID
CertificateThumbprint = $azureConnection.CertificateThumbprint
}
$null = Connect-AzAccount @params
}
catch {
$errorMessage = $_
Throw "Unable to authenticate with error: $errorMessage"
}
# Discovery of all Azure VM's in the current subscription.
$azurevms = Get-AzVM | Select-Object -ExpandProperty Name
Write-Host "Discovering Azure VM's in the following subscription $SubscriptionID Please hold...."
Write-Host "The following VM's have been discovered in subscription $SubscriptionID"
$azurevms
foreach ($azurevm in $azurevms) {
Write-Host "Checking for tag $vmtagname on $azurevm"
$tagRGname = Get-AzVM -Name $azurevm | Select-Object -ExpandProperty ResourceGroupName
$tags = (Get-AzResource -ResourceGroupName $tagRGname -Name $azurevm).Tags
If ($tags.UpdateWindow){
Write-Host "$azurevm already has the tag $vmtagname."
}
else
{
Write-Host "Creating Tag $vmtagname and Value $tagvalue for $azurevm"
$tags.Add($vmtagname,$tagvalue)
Set-AzResource -ResourceGroupName $tagRGname -ResourceName $azurevm -ResourceType Microsoft.Compute/virtualMachines -Tag $tags -Force `
}
}
Write-Host "All tagging is done"
推荐阅读
- testing - 在 Gitlab CI/CD 中使用不同的依赖版本进行测试
- laravel - 如何使用 LARAVEL SQL 将 String 转换为 INT?
- python - 关于python中的def和return函数
- java - SessionBean 上的实例 LoggerFactory.getLogger 上的 StackOverFlowError
- flutter - 使用 Flutter 连接到 Wifi
- asp.net - 带有 EFCore 的 Docker Windows 容器 API 连接到 microsoft/mssql-server-windows-developer 容器
- events - 自定义事件:任务完成的实例垃圾暴徒要求
- amazon-dynamodb - DynamoDB 如何在 PartiQL 查询中使用索引?
- google-bigquery - 参数化 BigQuery 查询不会从联接表中产生任何结果
- python - 如何在不更改其他级别的顺序的情况下按一个级别对 MultiIndex 进行排序