繁体   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