Search code examples
azurewebhooksazure-powershellazure-automationazure-runbook

Using powershell to move azure runbooks and their webhooks to another automation account


So, as the title says .. I have an Azure automation account with few runbooks and some of them can have webbhooks .. one or more

I use Powershell script to

  • Create automation account - DONE, creted.

    New-AzAutomationAccount -Name $newAutomationAccountName -Location $location - 
    ResourceGroupName $resourceGroupName
    
  • Supplying the name of the source automation and target automation account names, and resource group, I am creating run books by their respective names in the new AutomationAccount. - DONE, I can see those created

    #Copy automation runbooks from source
    $runBooks = (Get-AzAutomationRunbook -AutomationAccountName $srcAutomationAccountName -ResourceGroupName $resourceGroupName).Name
    $runBooks.foreach{
        Write-Host "Processing RunBook $_"
    
        #Create runbooks
        Invoke-Expression "$Workfolder\powershellScripts\createAzureAutomationRunBooks.ps1 - 
        resourceGroupName $resourceGroupName -location $location -srcAutomationAccountName 
        $srcAutomationAccountName -newAutomationAccountName $newAutomationAccountName - 
        Workfolder $Workfolder -runBookName $_"
    

I externalize the calls to create runbooks to separate script that seems straight forward,

if( -not(Get-AzAutomationRunbook -AutomationAccountName $newAutomationAccountName - ResourceGroupName $resourceGroupName -Name $runBookName -ErrorAction SilentlyContinue)) { Write-Host "Creating new Runbook $runBookName" New-AzAutomationRunbook -AutomationAccountName $newAutomationAccountName -Name $runBookName -ResourceGroupName $resourceGroupName -Type PowerShell
Start-Sleep -Seconds 5 #Wait some time for Runbook to settle succesfully } else { Write-Host "Updating Runbook $runBookName" Set-AzAutomationRunbook -AutomationAccountName $newAutomationAccountName -Name $runBookName -LogVerbose $True -ResourceGroupName $resourceGroupName
Start-Sleep -Seconds 5 #Wait some time for Runbook to settle succesfully }

but they are created. However, when i go to the new Automation Account and "Edit" the runbook I do not have any content in it.
Please, what is going on here?

Also, another question: When in the similar manner I copy WebHooks from Original RunBooks, do their URL change? If so, how can I obtain the new URLS?


Solution

  • Using PowerShell to move azure runbooks and their webhooks to another automation account: -

    After an analysis of all of your requirements, I've provided detailed answers to each of your queries.

    1.

    It is possible to import content from one runbook to another runbook which is located in another automation account. To do that, use Export-AzAutomationRunbook and Import-AzAutomationRunbook PowerShell commands inside your source automation account and the script is given below.

    connect-azaccount -identity
    $sourceAutomationAccount = "newauto"
    $DestAutomationAccount= "newauto1"
    $resourceGroup = "xxxx"
    $runbooklist = (Get-AzAutomationRunbook -AutomationAccountName $sourceAutomationAccountName -ResourceGroupName $resourceGroup).Name
    foreach ($runbook  in  $runbooklist) {
    Export-AzAutomationRunbook -ResourceGroupName $resourceGroup -AutomationAccountName $DestAutomationAccount -Name "xxx" -OutputFolder "C:\xxxx.ps1"
    }
    foreach ($runbook in  $runbooklist) {
    Import-AzAutomationRunbook -Path "C:\xxxx.ps1" -ResourceGroupName $resourceGroup -AutomationAccountName $DestAutomationAccount -Type PowerShell
    }
    

    Output:

    enter image description here

    After importing the source runbook into the target runbook under another automation account, I was able to view the same content.

    enter image description here

    2.

    When it comes to webhooks, there is no way to import and export them from one to another. Because webhooks always have a unique Url that is only visible at the time of webhook creation. After that, it is not visible to users for security reasons.

    As a result, one must do it manually. If you want to add the same webhooks to the target runbook, create a webhook with the same name as the source runbook webhook and set it with the exact configuration as the source configuration, including the expiry time.

    3.

    Do their URL change:

    Yes, webhook URLs are unique, and they will change even if you generate them with the same settings and name as existing ones.

    It is an unique address that a client calls with an HTTP POST to start the runbook linked to the webhook. It's automatically generated when you create the webhook. You can't specify a custom URL.

    Reference doc for more relevant information.