Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save andreasbotsikas/8ecf428c6cc7066c494110fc427b0fe4 to your computer and use it in GitHub Desktop.
Save andreasbotsikas/8ecf428c6cc7066c494110fc427b0fe4 to your computer and use it in GitHub Desktop.
Generate Request Headers For Azure Batch REST API
$Key = "your azure batch key here"
$BatchAccount = "your azure batch account here"
$endpoint = "pools" # The endpoint https://docs.microsoft.com/en-us/rest/api/batchservice/pool/list
$verb = "GET" # "POST"
$version = "2020-09-01.12.0" # Api version used
$bodySize= "" # This is the body length. You can get it with the first Auth failure
$bodyType= "" # "application/json; odata=minimalmetadata" # Content type needs to be this in POST requests
$sharedKey = [System.Convert]::FromBase64String($Key)
$date = [System.DateTime]::UtcNow.ToString("R")
$stringToSign = "$verb`n`n`n$bodySize`n`n$bodyType`n`n`n`n`n`n`nocp-date:$date`n/$BatchAccount/$endpoint`napi-version:$version"
[byte[]]$dataBytes = ([System.Text.Encoding]::UTF8).GetBytes($stringToSign)
$hmacsha256 = New-Object System.Security.Cryptography.HMACSHA256
$hmacsha256.Key = [Convert]::FromBase64String($key)
$sig = [Convert]::ToBase64String($hmacsha256.ComputeHash($dataBytes))
$authhdr = "SharedKey $BatchAccount`:$sig"
# Needed request headers
$headers = @{
"ocp-date" = $date;
"Authorization" = "$authhdr";
}
ConvertTo-Json $headers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment