Using Azure Automation to Manage local resources | Part I : Get started on Hybrid Runbook Worker

Microsoft Azure Automation provides a way for users to automate the manual, long-running, error-prone, and frequently repeated tasks that are commonly performed in a cloud and enterprise environment.

In this article, I will introduce how to install hybrid runbook worker, then we can manage our on-premise resources via azure automation later.

Introduction

The Hybrid Runbook Worker feature of Azure Automation allows you to run runbooks directly on the computer hosting the role and against resources in the environment to manage those local resources. Runbooks are stored and managed in Azure Automation and then delivered to one or more designated computers.

Prerequisites:

  1. Azure subscription
  2. Windows PowerShell 4.0 or later. You can use “$PSVersionTable.PSVersion” to look up the version, on my side, it is

 

Steps:

  1. Create an azure automation account.        
  2. On your computer, start Windows PowerShell in Administrator mode.
  3. Download the New-OnPremiseHybridWorker.ps1 script by running “Install-Script -name New-OnPremiseHybridWorker”
  4. Run “New-OnPremiseHybridWorker -AutomationAccountName <NameofAutomationAccount> `

-ResourceGroupName <NameofOResourceGroup> -HybridGroupName <NameofHRWGroup> `

-SubscriptionId <AzureSubscriptionId> -WorkspaceName <NameOfOMSWorkspace>”,

after downloading and installing, it shows messages as below.

  1. After that, you can see a new group under the Hybrid worker groups.

 

  1. To access local machine through hybrid worker, you need to specify a credential asset that has access to local resources, and all runbooks run under these credentials when running on a Hybrid Runbook Worker in the group.
     The user name for the credential must be in one of the following formats:
         1). domain\username
         2). username@domain
         3). username (for accounts local to the on-premises computer)

 

  1. After that, you can see your credential under the Credentials menu.

8. Create a PowerShell runbook workflow. This workflow shows how to copy files under a folder from source to destination.

workflow Copy-LocalFiles

{
    Param
    (
        [parameter(Mandatory=$true)]
        [String] $SourcePath,
        [parameter(Mandatory=$true)]
        [String] $DestinationPath
    )
    Write-Output "Executing runbook on hybrid runbook worker: $env:ComputerName"
    Write-Output "Source: $SourcePath"
    Write-Output "Destination: $DestinationPath"
    $result = InlineScript
    {
        try
        {
            Copy-Item -Path "$using:SourcePath" -Destination "$using:DestinationPath" -Recurse
        }
        catch
        {
            $errorMessage = $error[0].Exception.Message
        }
        if($errorMessage -eq $null)
        {
            return "Successfully copied files"
        }
        else
        {
            return "Failed: Encountered error(s) while copying files. Error message=[$errorMessage]"
        }
    }
    Write-Output $result
    Write-Output "Execution finished"
}

 

9. Save and go to test pane, you need to fill the Parameters, SOURCEPATH is the folder you want to copy, and DESTINATIONPATH is the place you want to copy to. Note that no quotes are required, otherwise there will be errors.

10. Select run on hybrid worker, and choose your hybrid worker group.

11. Go to your local machine, the files have been copied successfully.

Summary

In this article, i’ve showed how to manage the local resources via azure automation hybrid runbook worker. Welcome to contact us via aka.ms/devchat if you have any questions. In the next article, i will show you how to copy the local resources to azure storage

Leave a Reply

Your email address will not be published. Required fields are marked *