Dynamically cast a string to an integer in a runbook

Chris 26 Reputation points
2020-11-17T14:14:26.113+00:00

I am trying to create a script that updates an IP address in a NSG in Azure using a webhook connected to a runbook.

My powershell script contains the webhook which I run locally:

Write-Output "Changing IP Address..."

$uri = "https://webhookaddress"

$nsg = @{
SourceAddressPrefix = ([System.String[]] @("123.123.123.12"))
Name = "NSG_Rule_Name"
}

$body = ConvertTo-Json -InputObject $nsg
$header = @{ message="StartedbyContoso"}
$response = Invoke-WebRequest -Method Post -Uri $uri -Body $body -Headers $header
$jobid = (ConvertFrom-Json ($response.Content)).jobids[0]

The runbook input parameter accepts the WEBHOOKDATA as JSON. Sample JSON:

{"WebhookName":"UpdateIPAddress","RequestBody":"{\r\n "Name": "NSG_Rule_Name",\r\n "SourceAddressPrefix": [\r\n "123.123.123.12"\r\n ]\r\n}","RequestHeader":{"Connection":"Keep-Alive","Host":"33ce53a59d11.webhook.ne.azure-automation.net","User-Agent":"Mozilla/5.0","message":"StartedbyContoso","x-ms-request-id":"7c7c8934-0445-478c-84fd-a6761eadc79b"}}

When trying to pass $nsg.SourceAddressPrefix in the runbook, it fails. If I don't cast the IP to a string in the webhook script, it will fail to be in the JSON. So now I'm trying to cast the string back to an integer within the runbook:

$nsg = Get-AzNetworkSecurityGroup -Name "NSG_Name" -ResourceGroupName "RG_Name"
$nsg | Get-AzNetworkSecurityRuleConfig -Name "NSG_Rule_Name"
Set-AzNetworkSecurityRuleConfig -Name "NSG_Rule_Name" -NetworkSecurityGroup $nsg -SourceAddressPrefix = $nsg.SourceAddressPrefix

How can I set this up to pass an IP address through to a runbook and then get the runbook to read the IP address?

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,177 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Andreas Baumgarten 103K Reputation points MVP
    2020-11-17T14:50:05.497+00:00

    Maybe this blogpost is helpful:

    https://blog.ctglobalservices.com/azure/jgs/azure-automation-using-webhooks-part-1-input-data/

    From this blog post I got the information how to provide Firstname and Lastname via webhook for a "Create User" runbook.

    ## These are just snippets for demoing - removed some lines of the full script
    
    ##First snippet
    [CmdletBinding()]
    Param
    ([object]$WebhookData)
    $VerbosePreference = 'continue'
    
    if ($WebHookData)
        {
        $WebhookName     =     $WebHookData.WebhookName
        $WebhookHeaders  =     $WebHookData.RequestHeader
        $WebhookBody     =     $WebHookData.RequestBody
    
        $From = $WebhookHeaders.From
        $Input = (ConvertFrom-Json -InputObject $WebhookBody)
        Write-Verbose "WebhookBody: $Input"
        }
    
    ## Second Snippet
    
    $firstname = $Input.FirstName
    $lastname = $Input.LastName
    
    ## Third Snippet
    $adDisplayName = $firstname +" "+$lastname
    $newUserName = "$firstname.$lastname"
    
    ## Fourth snippet
    New-ADuser -Path "$adOU"`
                        -Name "$adDisplayName"`
                        -DisplayName "$adDisplayName"`
                        -GivenName "$firstName"`
                        -Surname "$lastName"`
                        ### some more things
    

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments