Azure Functions

Copper Contributor

Hi There,

Im new to azure functions.

Essentially I'd like to run a PowerShell module from an Azure function to get an Azure MetaData service's Access Token and then retrieved it to a token that will be used to make an external REST call.

 

I've created a function and I've enabled System Identity for that. But when I try to access the Azure MetaData but it, not successful. Did anybody called Azure MetaData services from Azure Functions? Please help how to achieve this

 

# Input bindings are passed in via param block.
param($Timer)

# Get the current universal time in the default string format.
$currentUTCtime = (Get-Date).ToUniversalTime()

# The 'IsPastDue' property is 'true' when the current function invocation is later than scheduled.
if ($Timer.IsPastDue) {
    Write-Host "PowerShell timer is running late!"
}

# Write an information log with the current time.
Write-Host "PowerShell timer trigger function ran! TIME: $currentUTCtime"
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Metadata", "true")
Write-Host "Nirmal === Kumar "
$response = Invoke-RestMethod 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://management.azure.com/' -Method 'GET' -Headers $headers
 Write-Host "Nirmal ==== "                        
$access_token =$response.access_token
Write-Host "$access_token"

 

The error is shown below:
 
Nirmalkumnar_Dhanaraj_0-1622988115550.png

 

1 Reply
Not sure if you got this sorted, but take a look at this Blog post:

https://tech.nicolonsky.ch/azure-functions-powershell-modules/

Tells you where you can upload your PS module too to make sure the cmdlet is there or editing the 'requirements.psd1' file.