AzureAutomationRunbook.ps1

param (
    [Parameter(Mandatory=$false)]
    [object]$WebhookData
)

if ($WebhookData -ne $null) {
    # Extract the webhook request body (payload)
    $WebhookBody = $WebhookData.RequestBody | ConvertFrom-Json
    $APIKeyRequest = $WebhookBody.APIKey
    $Firstname = $WebhookBody.Firstname
    $Lastname = $WebhookBody.Lastname

    $APIKeyStored = Get-AutomationVariable -Name "APIKey"
    If (!($APIKeyRequest -eq $APIKeyStored))
    {
        throw "401: Unauthorized. Unknown API Key."
    }
    else
    {
        Write-YcLogMessage ("200: Successful authentication.") -ToOutput $true
    }
} else {
    Write-YcLogMessage ("WebhookData is null. No payload was received.") -ToOutput $true
}

$PathToMappingFile = Get-AutomationVariable -Name "PathToMappingFile"
$PathToCsv = Get-AutomationVariable -Name "PathToCsv" 
$OutputCSVPath =  Get-AutomationVariable -Name "OutputCSVPath"

$APIProv_APIAppServicePrincipalId = Get-AutomationVariable -Name "APIProv_APIAppServicePrincipalId" 
$APIProv_AzureAppRegistrationClientId = Get-AutomationVariable -Name "APIProv_AzureAppRegistrationClientId" 
$APIProv_CertificateThumbprint = Get-AutomationVariable -Name "APIProv_CertificateThumbprint" 
$tenantId = Get-AutomationVariable -Name "tenantId" 

Write-YcLogMessage ("Path to mapping file is: "+$PathToMappingFile) -ToOutput $true
Write-YcLogMessage ("PathToCsv is: "+$PathToCsv) -ToOutput $true
Write-YcLogMessage ("OutputCSVPath is: "+$OutputCSVPath) -ToOutput $true
Write-YcLogMessage ("APIProv_APIAppServicePrincipalId is: "+$APIProv_APIAppServicePrincipalId) -ToOutput $true
Write-YcLogMessage ("APIProv_AzureAppRegistrationClientId is: "+$APIProv_AzureAppRegistrationClientId) -ToOutput $true
Write-YcLogMessage ("APIProv_CertificateThumbprint is: "+$APIProv_CertificateThumbprint) -ToOutput $true
Write-YcLogMessage ("tenantId: "+$tenantId) -ToOutput $true

Write-YcLogMessage "Done reading config." -ToOutput $true
Write-YCLogMessage "----------------------------------------------------------------" -ToOutput $true

try {
    Write-YcLogMessage ("Creating .csv from mapping in: "+$PathToMappingFile) -ToOutput $true
    New-YcIAMCsvFromMapping -AttributeMappingFilePath $PathToMappingFile -OutputCSVPath $OutputCSVPath
    Write-YcLogMessage ("Successfully created empty .csv from mapping")
}
catch {
    throw ("Could not create .csv from mapping. Error details: " + $_.Exception.Message)
}

try {
    Write-YcLogMessage ("Adding user info to .csv.. ") -ToOutput $true
    Add-YcIAMRowToImportCSV -csvPath $OutputCSVPath -Firstname $Firstname -LastName $lastname
    Write-YcLogMessage ("Successfully added user info to .csv") -ToOutput $true
}
catch {
    throw ("Could not add user info to .csv. Error Details: " + $_.Exception.Message)
}