[英]Azure Automation Runbook missing mandatory parameters
I'm trying to set a Tag on all virtual machines in my subscription but I keep getting errors when running the Runbook. 我正在尝试在订阅中的所有虚拟机上设置标签,但是在运行Runbook时仍然会出现错误。 The error is the following:
错误如下:
Get-AzureRmVM : Cannot process command because of one or more missing mandatory parameters: ResourceGroupName. At line:30
Here is my Runbook: 这是我的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"
I tried importing the right modules but this doesn't seem to affect the outcome. 我尝试导入正确的模块,但这似乎并不影响结果。 Running the same commands in Cloud Shell does work correctly.
在Cloud Shell中运行相同的命令确实可以正常工作。
I can reproduce your issue, the error was caused by this part Get-AzureRmVM -Name $azurevm
, when running this command, the -ResourceGroupName
is needed. 我可以重现你的问题,错误是由这部分引起的
Get-AzureRmVM -Name $azurevm
,运行此命令时, -ResourceGroupName
是必要的。
You need to use the Az
command Get-AzVM -Name $azurevm
, it will work. 您需要使用
Az
命令Get-AzVM -Name $azurevm
,它将起作用。
Running the same commands in Cloud Shell does work correctly.
在Cloud Shell中运行相同的命令确实可以正常工作。
In Cloud shell, azure essentially uses the new Az
module to run your command, you can understand it runs the Enable-AzureRmAlias
before the command, you could check that via debug mode. 在Cloud Shell中,azure本质上使用新的
Az
模块运行您的命令,您可以了解它在命令之前运行Enable-AzureRmAlias
,您可以通过调试模式进行检查。
Get-AzureRmVM -Name joyWindowsVM -debug
To solve your issue completely, I recommend you to use the new Az
module, because the AzureRM
module was deprecated and will not be updated. 为了完全解决您的问题,我建议您使用新的
Az
模块,因为AzureRM
模块已被弃用,不会被更新。
Please follow the steps below. 请按照以下步骤操作。
1.Navigate to your automation account in the portal -> Modules
, check if you have imported the modules Az.Accounts
, Az.Compute
, Az.Resources
, if not, go to Browse Gallery
-> search and import them. 1.在门户网站->
Modules
导航到您的自动化帐户,检查是否已导入模块Az.Accounts
, Az.Compute
, Az.Resources
,如果不是,请转到Browse Gallery
Az.Resources
>搜索并导入它们。
2.After import successfully, change your script to the one like below, then it should work fine. 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.