问题描述
更新:我按照下面的BenV的建议使用了Azure自动化,它可以正常工作!可以在此处找到更多信息. .
Update: I used Azure Automation per BenV's suggestion below and it worked! More info can be found here.
我有一个PowerShell脚本,需要运行一些Azure命令,例如New-AzureStorageContext
,Get-AzureStorageContainer
,Set-AzureStorageBlobContent
等.我想将该脚本作为Webjob运行.
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.
当我以Webjob身份运行此脚本时,在Azure命令下面收到错误消息.其他PS命令可从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.
我搜索了StackOverflow,但找不到从Webjob运行Azure命令时生成的这些错误的帖子.提到的一些相关帖子使用Import-Module
,与下面给出的建议类似.
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.
一个较老的MSDN博客文章建议在PS脚本中添加导入模块Azure.ps1",并在webjob zip文件中包含Azure.ps1. (实际上是来自本地C:\ Program Files(x86)\ Microsoft SDKs \ Azure \ PowerShell \ ServiceManagement \ Azure的Azure.psd1).另外,我尝试使用Azure.psd1然后使用Azure.ps1进行Import-Module,以为错误可能与文件扩展名有关,但事实并非如此.
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.
我的webjob .zip文件只有我的.CMD文件GetLinks.ps1和Azure.ps1.
My webjob .zip file only has my .CMD file, GetLinks.ps1 and Azure.ps1.
我的.CMD文件使用以下命令启动我的PS脚本:PowerShell.exe -ExecutionPolicy RemoteSigned -File GetLinks.ps1
My .CMD file launches my PS script with: PowerShell.exe -ExecutionPolicy RemoteSigned -File GetLinks.ps1
在此.ps1文件的顶部,我有:导入模块.\ Azure.ps1".由于我在WebJob运行日志中看到"INFO"语句,因此此操作成功运行.
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.
接下来,我的PS脚本尝试运行Azure PS命令,但仍然出现相同的错误,例如下面的一个示例错误.
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
推荐答案
运行WebJobs的沙箱当前不支持Azure Azure.有多种因素:
Azure PowerShell is not currently supported from the sandbox in which WebJobs runs. There are a variety of factors:
- 未在工作服务器上安装CmdLets
- 即使已安装,仍有一些问题会阻止它们正常运行
- 即使它们运行,您也需要在运行命令之前进行身份验证.最后一部分可以使用服务主体解决.
因素2是最大的阻碍.我们希望做到这一点,但是现在这很棘手.
Factor #2 is the biggest blocker. We'd like to get to a point where this is possible, but right now it is tricky.
一个潜在的解决方法是直接执行ARM请求,尽管这需要做更多的工作(并且您仍然需要使用Service Principal进行身份验证).您也可以编写C#代码进行调用.
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.
这篇关于从Webjob运行Azure PowerShell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!