azure-functions-powershell-worker
azure-functions-powershell-worker copied to clipboard
azure cli from azure function powershell
How to run azure cli from azure function powershell?
Can you use the Azure PowerShell module instead?
Not from functions. When I'm loading azurerm module for SQL scaling (thats my task) and than login/connected nf to azure I'm getting error message.
On Tue, 14 May 2019, 06:50 Dongbo Wang, [email protected] wrote:
Can you use the Azure PowerShell module instead?
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Azure/azure-functions-powershell-worker/issues/221?email_source=notifications&email_token=AEVINAV63GQNJIQXPMGA5FDPVJHKLA5CNFSM4HMQGFKKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODVKKXCI#issuecomment-492088201, or mute the thread https://github.com/notifications/unsubscribe-auth/AEVINAUKDXLQHAWLMR72KDTPVJHKLANCNFSM4HMQGFKA .
Btw. it works (4ex: Get-AzContext ) when running from vscode, but not after deployment into azure function.
On Tue, 14 May 2019 at 06:50, Dongbo Wang [email protected] wrote:
Can you use the Azure PowerShell module instead?
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Azure/azure-functions-powershell-worker/issues/221?email_source=notifications&email_token=AEVINAV63GQNJIQXPMGA5FDPVJHKLA5CNFSM4HMQGFKKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODVKKXCI#issuecomment-492088201, or mute the thread https://github.com/notifications/unsubscribe-auth/AEVINAUKDXLQHAWLMR72KDTPVJHKLANCNFSM4HMQGFKA .
Ok I found: first on your VM/workstation: Connect-AzAccount Save-AzContext
copy context json file to your azure function file system.
in your functions:
Import-AzContext -Path "D:\home\site\wwwroot\
works.
On Tue, 14 May 2019 at 11:19, Dariusz Bzówka [email protected] wrote:
Btw. it works (4ex: Get-AzContext ) when running from vscode, but not after deployment into azure function.
On Tue, 14 May 2019 at 06:50, Dongbo Wang [email protected] wrote:
Can you use the Azure PowerShell module instead?
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Azure/azure-functions-powershell-worker/issues/221?email_source=notifications&email_token=AEVINAV63GQNJIQXPMGA5FDPVJHKLA5CNFSM4HMQGFKKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODVKKXCI#issuecomment-492088201, or mute the thread https://github.com/notifications/unsubscribe-auth/AEVINAUKDXLQHAWLMR72KDTPVJHKLANCNFSM4HMQGFKA .
Did you enable MSI for your FunctionApp? Once you enable MSI and assign the necessary roles to your identity object, the default profile.ps1 will log you in Azure automatically.
It didn't work.
I can try any step by step guide if you send me step by step instructions.
The only way it works for me is the one I described.
On Tue, 14 May 2019, 18:05 Dongbo Wang, [email protected] wrote:
Did you enable MSI for your FunctionApp? Once you enable MSI and assign the necessary roles to your identity object, the default profile.ps1 will log you in Azure automatically.
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Azure/azure-functions-powershell-worker/issues/221?email_source=notifications&email_token=AEVINASTU533Q37LCGD5T33PVLWODA5CNFSM4HMQGFKKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODVMEMYQ#issuecomment-492324450, or mute the thread https://github.com/notifications/unsubscribe-auth/AEVINATGTJGGPQQODMRQYPDPVLWODANCNFSM4HMQGFKA .
az cli is not available currently out the box in the functions powershell environment. This is a feature ask and we are looking into it.
In the meantime, the following workaround has been verified:
- Deploy the content of the local "C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2" folder to the app (e.g. upload it to the file share for the function app using storage explorer).
- Add an alias for the az command to this path in the profile.ps1:
Set-Alias -Name az -Value "d:\home\data\CLI2\wbin\az.cmd"
After that, the following function runs successfully:
param($Request, $TriggerMetadata)
az login --identity
$FunctionApps = az functionapp list | ConvertFrom-Json
$FunctionApps.Name
@AnatoliB can you elaborate on where to to upload the CLI2 folder?
You said:
to the file share for the function app using storage explorer
I am using SE and I do not see a file share.
@VictorioBerra When you create a PowerShell Function app, a storage account is normally created in the same resource group, and this storage account contains an automatically created file share. You can access it in multiple ways, for example:
- Using Storage Explorer: In the Portal, find the Function app and click on the Overview tab, then follow the link to the resource group. You will find a storage account in this resource group. Open this storage account in Storage Explorer, go to file shares and find a share with an auto-generated name. I recommend uploading CLI2 to the data directory.
- Using Kudu: In the Portal, find the Function app and click Platform features -> Advanced tools (Kudu), then select Debug console -> PowerShell, and then you can drag-and-drop zip files to be uploaded there.
@AnatoliB I have no file shares in mine. Does it matter that i'm hosting an existing app service plan on Windows? As for Kudu, if I upload the CLI2 as a zip I assume I simply unzip using a PS command?
@VictorioBerra I see. Yes we use a different kind of file storage on app service plan, so it is not on Azure Files, you are right. In this case, the Kudu method should still work. When you drag-and-drop a zip file into the browser window, Kudu will automatically unzip the content into the current location, preserving the directory structure in the zip.
@AnatoliB I must have done something wrong with Kudu, I zipped the CLI2 directory, dragged the zip into my browser, it uploaded successfully but never extracted. This was about 30 minutes ago now

I was able to unzip manually in the PS console by just using unzip CLI2.zip. Its not done unzipping yet but it seems to be working.
EDIT: No luck. When I tried to do az login --identity in my run.ps1 I got the following error:
Exception: The term 'd:\home\data\CLI2\wbin\az.cmd' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
I have Set-Alias -Name az -Value "d:\home\data\CLI2\wbin\az.cmd" in my profile.ps1
I gave up and used the REST API to accomplish my task. IMO the PS Functions are severely lacking without the AZ CLI. The Azure Powersehll Module is not enough. For example, what is the equivalent of this in APM? https://docs.microsoft.com/en-us/cli/azure/webapp/webjob/triggered?view=azure-cli-latest#az-webapp-webjob-triggered-list
@anirudhgarg what is the ETA of the feature ask? Do you have a link we can use to track the progress of the feature. Like on https://feedback.azure.com/?
@VictorioBerra by now it looks like the only way. see: anirudhgarg commented on 28 May 2019 • az cli is not available currently out the box in the functions powershell environment. This is a feature ask and we are looking into it.
I am looking to use az cli for some features that are not available in PS yet. https://docs.microsoft.com/en-us/cli/azure/container?view=azure-cli-latest#az-container-start Please let me know if there is an alternate. Az PS commands works from within the functions.
@g0pinath Which Az PS commands are working for you within the functions? I am trying this command "az login --identity", still does not work for me. I have followed the steps given by @AnatoliB
@anirudhgarg could you please tell when this feature will be available?
Hi Team, I followed the above steps of uploading azure cli from mac book onto azure powershell functions and updated the profile.ps1, but still receive az command in not found. It would help if a detailed step by step instructions are provided for the workaround till the time microsoft provide inbulilt az cli support in azure functions.Thanks.
Its been more than a year and We still do not have this feature.
Hi All,
We able to run the Azure CLI commands from Function App (Powershell) But we are facing PERFORMANCE issue, like "az account set" command take minimum 2 mins and "az aks show" command takes minimum 3 mins. we achieved our requirement by using these commands inside the loop. so it impacts much.
- Why the Azure CLI commands taking long time in Azure Function App? because in Windows Azure CLI the "az account set" command takes around 3 secs and "az aks show" command takes around 6 secs to run.
- Do we have any option to improve the performance of the Azure CLI commands from Azure Function App?
Please let me know if anything is not clear and share your thoughts on this.
Thanks, Seenivasan.M
@MasilamaniSeenivasan 2-3 min sounds way more than expected. How do you deploy Azure CLI? Do you include it in your app content, or upload it separately? What is the target location? Also, how do you invoke it and how you measure the time? Could you please share the code?
-
Manually upload CLI2 zip file from local machine to Function App (location:D:\home\data) through kudu and unzip file in using PowerShell command.
-
In Profile.ps1 Set-Alias -Name az -Value "d:\home\data\CLI2\wbin\az.cmd"
-
Code snippet,
#onetime login using function MSI az login --identity
#used in loop to achieve our functionality (Get-Date).ToUniversalTime() #set subscription of current AKS az account set -s <SubscriptionName> (Get-Date).ToUniversalTime() #get AKS's msi objectId $objId = $(az aks show -g <ResourceGroupName> -n <AksName> --query identityProfile.kubeletidentity.objectId -o tsv) (Get-Date).ToUniversalTime()
Please let me know if anything else required.
Is this still not supported?
I have Az listed in my requirements.psd1

And I also invoked Enable-AzureRmAlias in my profile.ps1

But I'm still getting the "The term 'az' is not recognized" error... I don't particularly want to go down the route of having to manually upload the CLI2 folder, as that just seems gross.
@OwainJ Please note that Azure PowerShell (Az/AzureRm modules) and Azure CLI are separate and independent tools. The requirements.psd1 and profile.ps1 code in your comment refers specifically to Azure PowerShell. In order to use Azure CLI, the workarounds mentioned above are still required.
Please do let us know, however, which capabilities of Azure CLI you are interested in. Can this be currently achieved by Azure PowerShell instead? Chances are, extending Azure PowerShell is a better idea, but this depends on the details. Your feedback will help us prioritize, thank you.
Hi @AnatoliB, thanks for your reply!
Ahh, I see, thanks for clearing that up! I'm very much a newbie with this stuff, so apologies in advance if anything I say is completely wrong.
Basically, I've been given a PowerShell command that creates a container instance and pushes the latest release image of a crawler to it. That command currently runs as an inline script (Azure CLI task) on an Azure DevOps release pipeline, which is triggered on a timer schedule.
This is the script that we have in the release pipeline Azure CLI task:
az container create --resource-group $(resourceGroupName ) --name crawler-incremental-$(deploymentName) --image $(acrLoginServer)/crawlers/incremental:$(Build.BuildId) --dns-name-label crawler-incremental-$(deploymentName) --ports 80 --registry-login-server $(acrLoginServer) --registry-username $(acrName) --registry-password $(acrPassword) --cpu 4 --memory 16 --location $(location) --azure-file-volume-account-key $(storageAccountKey) --azure-file-volume-account-name $(storageAccountName) --azure-file-volume-mount-path /crawler/ --azure-file-volume-share-name crawler-incremental-$(deploymentName) -fs --restart-policy Never
I was hoping that I could move this to a Timer based PowerShell Function App, so that it can then keep it in source control, and deploy it to our client's azure resource group, rather than having it just float around in a DevOps release pipeline.
Yes that should be possible using the corresponding Azure PowerShell Module for Azure Container Instances.
Have a look here: https://docs.microsoft.com/en-us/azure/container-instances/container-instances-quickstart-powershell https://docs.microsoft.com/en-us/azure/container-instances/container-instances-quickstart-powershell#create-a-container
@anirudhgarg Thank you so much, that worked perfectly!
Still looking for this feature, I'm looking to run an ad-hoc command in Azure Container Instance using az container exec from Azure CLI as Powershell Az module lacks this functionality
https://docs.microsoft.com/en-us/powershell/module/az.containerinstance/?view=azps-5.0.0#container-instances