解决Azure automation 报错问题
admin
2023-04-08 16:22:48
0

   今天来分析一波Azure automation的报错问题,Azure automation是个不错的东东,通过 Azure automation可以自动完成频繁的、耗时的、易出错的云管理任务。 有了这 样的自动化,我们可以专注于能够让业务增值的工作,automation可以实现很多功能,比如定时开关机,调整size以节省成本,或者是自动备份、删除文件到blob中,乃至结合一些业务逻辑,定制专门的automation runbook来完成业务需求等


    

   在使用automation的过程中,有遇到过一些大大小小的问题,拿出来和各位分享一下

首先是一个小问题,之前有个需求是要根据业务场景自动创建blob存储,然后将数据传输到这个blob中,完成任务后,再由别的逻辑触发自动删除,这个需求实际上很简单,用automation也可以很方便的完成任务

 

   但是在实际使用中遇到了一点小问题

这是一个之前做测试的runbook,只是简单测试创建storage account然后删除,但是发现居然报错了

解决Azure automation 报错问题



    点开报错信息,发现报错信息如下:

    其实代码很简单,只是一个remove-azurermstorage account,然后加了一个-force的开关,但是为啥会报错呢,报错信息提示的是找不到匹配的参数,一般来说这种问题都是处在这个命令的版本上


    解决Azure automation 报错问题

    

  因此特地输出了一下remove-azurermstorageaccount这个命令的版本来看下,顿时我就被惊到了,居然是他么的1.0.3..难怪不支持-force

解决Azure automation 报错问题




    而我电脑上这个命令的版本是多少呢。。是5.2.0

解决Azure automation 报错问题



    问题很明显了,就是出在命令的版本上,那么就来尝试强制导入5.2.0这个版本

解决Azure automation 报错问题


    可是尝试之后发现居然告诉我没有可用的版本。。难道这个东西智障到连版本都没办法选的吗

解决Azure automation 报错问题



    

  当然不是了,简单看了看automation account的设置,才发现自己犯了一个很傻的错误,这个automation account是很久前创建的,在module这里显示的命令版本都是非常非常低的了。。



    实际上我们只需要更新一波即可,点击update azure modules

解决Azure automation 报错问题


 更新完成

解决Azure automation 报错问题




    如果没有module的,可以尝试在gallery里添加

解决Azure automation 报错问题



到此为止,问题就算是解决了,那么如何防止这类问题再发生呢?其实可以创建一个定期更新module的runbook,然后定期运行,以下是微软提供的代码


<#    
.SYNOPSIS     
    This Azure Automation runbook imports the latest version of the Azure modules from the PowerShell Gallery.    
.DESCRIPTION    
    This Azure Automation runbook imports the latest version of the Azure modules from the PowerShell Gallery.    
    It requires that this runbook be run from the automation service and that the RunAs account is enabled on the     
    automation account.    
    You could put this runbook on a schedule so that it updates the modules each month or call through a webhook    
    as needed.    
.PARAMETER AutomationResourceGroup    
    Required. The name of the Azure Resource Group containing the Automation account.    
.PARAMETER AutomationAccountName    
    Required. The name of the Automation account.    
.PARAMETER ModuleVersionOverrides    
    Optional. A PowerShell HashTable or a JSON dictionary which contains module version overrides. Please be    
    careful of version incompatibility between modules when overriding module versions.    
.PARAMETER AzureEnvironment    
    Optional. The name of the target Azure environment (one of the values returned by 'Get-AzureRmEnvironment | select Name').    
.EXAMPLE    
    Update-AzureModule -AutomationResourceGroup contoso -AutomationAccountName contosoaccount    
.EXAMPLE    
    Update-AzureModule -AutomationResourceGroup contoso -AutomationAccountName contosoaccount -ModuleVersionOverrides @{'Azure'="4.0.2"; 'Azure.Storage'="3.0.2"; 'AzureRM.Profile'="3.0.1"; 'AzureRM.Automation'="3.0.1"; 'AzureRM.Compute'="3.0.1"; 'AzureRM.Resources' = "4.0.1"; 'AzureRM.Sql' = "3.0.1"; 'AzureRM.Storage'="3.0.2"} -AzureEnvironment 'AzureCloud'    
.EXAMPLE    
    Update-AzureModule -AutomationResourceGroup contoso -AutomationAccountName contosoaccount -ModuleVersionOverrides '{"Azure" : "4.0.2", "AzureRM.Sql" : "3.0.1", "AzureRM.Automation" : "3.0.1", "Azure.Storage" : "3.0.2", "AzureRM.Resources" : "4.0.1", "AzureRM.Storage" : "3.0.2", "AzureRM.Compute" : "3.0.1", "AzureRM.Profile" : "3.0.1"}'    
.NOTES    
    AUTHOR: Automation Team, Chase Dafnis    
    LASTEDIT: Nov 6th, 2018     
#>    
Param    
(    
[Parameter(Mandatory=$True)]    
[String] $AutomationResourceGroup,    
[Parameter(Mandatory=$True)]    
[String] $AutomationAccount,    
[Parameter(Mandatory=$False)]    
[object] $ModuleVersionOverrides,    
[Parameter(Mandatory=$False)]    
[String] $AzureEnvironment = 'AzureCloud'    
)    
$versionOverrides = ""    
# Try to parse module version overrides    
if ($ModuleVersionOverrides) {    
if ($ModuleVersionOverrides.GetType() -eq [HashTable]) {    
$versionOverrides = ConvertTo-Json $ModuleVersionOverrides    
} elseif ($ModuleVersionOverrides.GetType() -eq [String]) {    
# Verify that the ModuleVersionOverrides can be deserialized    
try{    
$temp = ConvertFrom-Json $ModuleVersionOverrides -ErrorAction Stop    
}    
catch [System.ArgumentException] {    
$ex = $_     
# rethrow intended    
throw "The value of the parameter ModuleVersionOverrides is not a valid JSON string: ", $ex    
}    
$versionOverrides = $ModuleVersionOverrides    
} else {    
$ex = [System.ArgumentException]::new("The value of the parameter ModuleVersionOverrides should be a PowerShell HashTable or a JSON string")    
throw $ex    
}    
}    
try    
{    
# Pull Azure environment settings    
$AzureEnvironmentSettings = Get-AzureRmEnvironment -Name $AzureEnvironment    
# Azure management uri    
$ResourceAppIdURI = $AzureEnvironmentSettings.ActiveDirectoryServiceEndpointResourceId    
# Path to modules in automation container    
$ModulePath = "C:\Modules"    
# Login uri for Azure AD    
$LoginURI = $AzureEnvironmentSettings.ActiveDirectoryAuthority    
# Find AzureRM.Profile module and load the Azure AD client library    
$PathToProfileModule = Get-ChildItem (Join-Path $ModulePath AzureRM.Profile) -Recurse    
Add-Type -Path (Join-Path $PathToProfileModule "Microsoft.IdentityModel.Clients.ActiveDirectory.dll")    
# Get RunAsConnection    
$RunAsConnection = Get-AutomationConnection -Name "AzureRunAsConnection"    
$Certifcate = Get-AutomationCertificate -Name "AzureRunAsCertificate"    
$SubscriptionId = $RunAsConnection.SubscriptionId    
# Set up authentication using service principal client certificate    
$Authority = $LoginURI + $RunAsConnection.TenantId    
$AuthContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $Authority    
$ClientCertificate = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.ClientAssertionCertificate" -ArgumentList $RunAsConnection.ApplicationId, $Certifcate    
$AuthResult = $AuthContext.AcquireToken($ResourceAppIdURI, $ClientCertificate)    
# Set up header with authorization token    
$AuthToken = $AuthResult.CreateAuthorizationHeader()    
$RequestHeader = @{    
"Content-Type" = "application/json";    
"Authorization" = "$AuthToken"    
}    
# Create a runbook job    
$JobId = [GUID]::NewGuid().ToString()    
$URI =  "$($AzureEnvironmentSettings.ResourceManagerUrl)subscriptions/$SubscriptionId/"`    
+"resourceGroups/$($AutomationResourceGroup)/providers/Microsoft.Automation/"`    
+"automationAccounts/$AutomationAccount/jobs/$($JobId)?api-version=2015-10-31"    
# Runbook and parameters    
if($versionOverrides){    
$Body = @"    
            {    
               "properties":{    
               "runbook":{    
                   "name":"Update-AutomationAzureModulesForAccount"    
               },    
               "parameters":{    
                    "AzureEnvironment":"$AzureEnvironment",    
                    "ResourceGroupName":"$AutomationResourceGroup",    
                    "AutomationAccountName":"$AutomationAccount",    
                    "ModuleVersionOverrides":"$versionOverrides"    
               }    
              }    
           }    
"@    
} else {    
$Body = @"    
            {    
               "properties":{    
               "runbook":{    
                   "name":"Update-AutomationAzureModulesForAccount"    
               },    
               "parameters":{    
                    "AzureEnvironment":"$AzureEnvironment",    
                    "ResourceGroupName":"$AutomationResourceGroup",    
                    "AutomationAccountName":"$AutomationAccount"    
               }    
              }    
           }    
"@    
}    
# Start runbook job    
Invoke-RestMethod -Uri $URI -Method Put -body $body -Headers $requestHeader            
}    
catch     
{    
throw $_.Exception    
}




相关内容

热门资讯

汪苏泷、薛之谦演唱会,出现“发... 演唱会装备党把这个设备拿出来,主办方感觉天都要塌了: 以后前排的票,可怎么卖? 以前年轻人去演唱会,...
拉普拉斯获得实用新型专利授权:... 证券之星消息,根据天眼查APP数据显示拉普拉斯(688726)新获得一项实用新型专利授权,专利名为“...
天舟十号满载科学与梦想 超6吨... 央视网消息:天舟十号货运飞船总长10.6米,由货物舱和推进舱组成,专门用于为空间站运送货物,发射重量...
武大切割OPPO校友:我们到底... OPPO这次的母亲节文案,确实是价值观出了问题。说“我妈有两个老公”,想玩饭圈梗来打破刻板印象,但这...
“我们完全可以相互成就、共同繁... 在华扎根50年,美国企业康明斯与中国伙伴携手成长、共享机遇——“我们完全可以相互成就、共同繁荣”(见...
乌克兰总统办公室前主任被控涉嫌... △乌克兰总统办公室前主任叶尔马克(资料图)当地时间11日,乌克兰国家反腐败局和特别反腐败检察院表示,...
你的蓝牙耳机正在 “偷听”?国... 在科技飞速发展的当下,无线耳机、智能手表、无线键盘等蓝牙设备凭借“一键连接”带来的便捷优势,已广泛应...
光洋股份:公司立足成为最懂制造... 证券日报网5月11日讯 ,光洋股份在接受调研者提问时表示,公司近年来加快布局机器人领域,立足成为最懂...
重庆拟投千亿培育沉浸式与虚拟现... 🤖 由 文心大模型 生成的文章摘要 重庆市发布“十五五”现代服务业发展规划,明确投入1 重庆市发...
内存缩水开倒车!谷歌Pixel... 5月11日消息,据相关媒体报道,受全球DRAM供应紧缺影响,谷歌即将推出的Pixel 11系列在内存...