Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Solve the problem of Azure automation error

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Today, let's analyze a wave of Azure automation error reports. Azure automation is a good thing, and Azure automation can automate frequent, time-consuming, and error-prone cloud management tasks. With this kind of automation, we can focus on the work that can add value to the business. Automation can achieve many functions, such as turning on and off the computer regularly, adjusting size to save costs, or automatically backing up and deleting files to blob, or even combining some business logic to customize special automation runbook to complete business requirements.

In the process of using automation, I have encountered some problems large and small, so I would like to share them with you.

First of all, there is a small problem. Before, there was a requirement to automatically create blob storage according to the business scenario, and then transfer the data to this blob. After the task is completed, it will be automatically deleted by other logic. This requirement is actually very simple, and the task can be easily completed with automation.

But there is a little problem in practical use.

This is a runbook that did the test before, but the simple test created storage account and then deleted it, but found that it was wrong.

Click on the error message and find that the error message is as follows:

In fact, the code is very simple, just a remove-azurermstorage account, and then add a-force switch, but why report an error? the error message indicates that no matching parameters can be found. Generally speaking, this problem is on the version of this command.

So I specially output the version of the remove-azurermstorageaccount command, and I was surprised that it was his 1.0.3. No wonder it is not supported-force

What is the version of this command on my computer? no, no, no. It's 5.2.0.

The problem is obvious, it is the version of the command, so try to force the import of version 5.2.0.

But after trying, I found that I was told that there was no available version. Is this thing so retarded that you can't even choose a version?

Of course not. After a brief look at the settings of automation account, I found that I made a stupid mistake. This automation account was created a long time ago, and the command version shown here in module is very low.

In fact, we just need to update one wave and click update azure modules.

Update complete

If you don't have module, you can try to add it to gallery.

So far, even if the problem is solved, how to prevent this kind of problem from happening again? In fact, you can create a runbook that updates module regularly and then run it regularly. here is the code provided by Microsoft

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}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report