Azure Functions

%3CLINGO-SUB%20id%3D%22lingo-sub-2419908%22%20slang%3D%22en-US%22%3EAzure%20Functions%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2419908%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20There%2C%3C%2FP%3E%3CP%3EIm%20new%20to%20azure%20functions.%3C%2FP%3E%3CP%3EEssentially%20I'd%20like%20to%20run%20a%20PowerShell%20module%20from%20an%20Azure%20function%20to%20get%20an%20Azure%20MetaData%20service's%20Access%20Token%20and%20then%20retrieved%20it%20to%20a%20token%20that%20will%20be%20used%20to%20make%20an%20external%20REST%20call.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI've%20created%20a%20function%20and%20I've%20enabled%20System%20Identity%20for%20that.%20But%20when%20I%20try%20to%20access%20the%26nbsp%3BAzure%20MetaData%20but%20it%2C%20not%20successful.%20Did%20anybody%20called%26nbsp%3BAzure%20MetaData%20services%20from%20Azure%20Functions%3F%20Please%20help%20how%20to%20achieve%20this%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%23%20Input%20bindings%20are%20passed%20in%20via%20param%20block.%0Aparam(%24Timer)%0A%0A%23%20Get%20the%20current%20universal%20time%20in%20the%20default%20string%20format.%0A%24currentUTCtime%20%3D%20(Get-Date).ToUniversalTime()%0A%0A%23%20The%20'IsPastDue'%20property%20is%20'true'%20when%20the%20current%20function%20invocation%20is%20later%20than%20scheduled.%0Aif%20(%24Timer.IsPastDue)%20%7B%0A%20%20%20%20Write-Host%20%22PowerShell%20timer%20is%20running%20late!%22%0A%7D%0A%0A%23%20Write%20an%20information%20log%20with%20the%20current%20time.%0AWrite-Host%20%22PowerShell%20timer%20trigger%20function%20ran!%20TIME%3A%20%24currentUTCtime%22%0A%24headers%20%3D%20New-Object%20%22System.Collections.Generic.Dictionary%5B%5BString%5D%2C%5BString%5D%5D%22%0A%24headers.Add(%22Metadata%22%2C%20%22true%22)%0AWrite-Host%20%22Nirmal%20%3D%3D%3D%20Kumar%20%22%0A%24response%20%3D%20Invoke-RestMethod%20'http%3A%2F%2F169.254.169.254%2Fmetadata%2Fidentity%2Foauth2%2Ftoken%3Fapi-version%3D2018-02-01%26amp%3Bresource%3Dhttps%3A%2F%2Fmanagement.azure.com%2F'%20-Method%20'GET'%20-Headers%20%24headers%0A%20Write-Host%20%22Nirmal%20%3D%3D%3D%3D%20%22%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%24access_token%20%3D%24response.access_token%0AWrite-Host%20%22%24access_token%22%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CDIV%3E%3CDIV%3EThe%20error%20is%20shown%20below%3A%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Nirmalkumnar_Dhanaraj_0-1622988115550.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F286624i12D6DD890CA91E08%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Nirmalkumnar_Dhanaraj_0-1622988115550.png%22%20alt%3D%22Nirmalkumnar_Dhanaraj_0-1622988115550.png%22%20%2F%3E%3C%2FSPAN%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E
Occasional Visitor

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.