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