首页 > 解决方案 > 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 中运行相同的命令确实可以正常工作。

标签: azureautomation

解决方案


我可以重现您的问题,错误是由这部分引起的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.ComputeAz.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"

推荐阅读