簡體   English   中英

Azure Automation Runbook缺少必需參數

[英]Azure Automation Runbook missing mandatory parameters

我正在嘗試在訂閱中的所有虛擬機上設置標簽,但是在運行Runbook時仍然會出現錯誤。 錯誤如下:

Get-AzureRmVM : Cannot process command because of one or more missing mandatory parameters: ResourceGroupName. At line:30

這是我的Runbook:

    $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.AccountsAz.ComputeAz.Resources ,如果不是,請轉到Browse Gallery Az.Resources >搜索並導入它們。

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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM