4
votes

Update: I used Azure Automation per BenV's suggestion below and it worked! More info can be found here.


I have a PowerShell script that needs to run a few Azure commands like New-AzureStorageContext, Get-AzureStorageContainer, Set-AzureStorageBlobContent, etc. I'd like to run the script as a webjob.

When I run this script as a webjob I receive errors below on the Azure commands. Other PS commands run successfully from the webjob.

I searched StackOverflow and couldn't find posts for these errors generated when Azure commands are run from a webjob. Somewhat related posts mentioned to use Import-Module which is similar to the advice given below.

An older MSDN blogpost suggested adding “Import-Module Azure.ps1” in the PS script and include Azure.ps1 inside the webjob zip file. (It's really Azure.psd1 from my local C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure). Separately, I tried Import-Module with Azure.psd1 then Azure.ps1 thinking the errors might be related to the file extension, but it wasn't.

My webjob .zip file only has my .CMD file, GetLinks.ps1 and Azure.ps1.

My .CMD file launches my PS script with: PowerShell.exe -ExecutionPolicy RemoteSigned -File GetLinks.ps1

At the top of this .ps1 file I have: "Import-Module .\Azure.ps1". This runs successfully since I see "INFO" statements in my WebJob run log.

Next my PS script tries to run the Azure PS commands and I still get the same errors like the one example error below.


New-AzureStorageContext : The term 'New-AzureStorageContext' 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.
At D:\local\Temp\jobs\triggered\getlinks2\b2025qk5.ddj\GetLinks.ps1:75 char:19
+ $storageContext = New-AzureStorageContext -StorageAccountName $storageAccountNam ...
+  ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (New-AzureStorageContext:String) [], CommandNotFoundException

+ FullyQualifiedErrorId : CommandNotFoundException

2
What are the requirements driving you to use WebJobs? Any reason you couldn't use Azure Automation?BenV
Thanks. I'll look into Azure Automation. Another option is to write a C# app as David suggests below.RandomTask

2 Answers

4
votes

Azure PowerShell is not currently supported from the sandbox in which WebJobs runs. There are a variety of factors:

  1. The CmdLets are not installed on the worker
  2. Even if installed, there are issues that prevent them from running correctly
  3. Even if they run, you'd need to authenticate before running command. This last part is solvable using Service Principals.

Factor #2 is the biggest blocker. We'd like to get to a point where this is possible, but right now it is tricky.

One potential workaround is to do ARM requests directly, though that's a bit more work (and you still need to auth using Service Principal). You could also write C# code to make the calls.

0
votes

Another possible option might be to use a node script and the azure cli. Alas, I also tried to work around this issue with .sh script but that will fail trying to setup azure cli env (see https://github.com/projectkudu/kudu/issues/1935). Finally, if just storage functions you need might try using SAS tokens and http requests to do basic stuff within your own ps functions...