运行 来自 Web 作业的 Azure PowerShell 命令

Running Azure PowerShell commands from a webjob

更新:我根据下面 BenV 的建议使用了 Azure Automation,它成功了!可以找到更多信息 here


我有一个 PowerShell 脚本需要 运行 一些 Azure 命令,例如 New-AzureStorageContextGet-AzureStorageContainerSet-AzureStorageBlobContent 等。我想 运行 作为网络作业的脚本。

当我 运行 此脚本作为 webjob 时,我在 Azure 命令中收到以下错误。其他 PS 命令 运行 从 webjob 成功。

我搜索了 Whosebug,但找不到有关当 Azure 命令 运行 来自 webjob 时生成的这些错误的帖子。一些相关的帖子提到使用 Import-Module 这类似于下面给出的建议。

一篇较早的 MSDN 博文建议在 PS 脚本中添加“Import-Module Azure.ps1”,并在 webjob zip 文件中包含 Azure.ps1。 (它实际上是我本地 C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure 中的 Azure.psd1)。另外,我尝试使用 Azure.psd1 和 Azure.ps1 导入模块,认为错误可能与文件扩展名有关,但事实并非如此。

我的 webjob .zip 文件只有我的 .CMD 文件,GetLinks.ps1 和 Azure.ps1。

我的 .CMD 文件启动我的 PS 脚本:PowerShell.exe -ExecutionPolicy RemoteSigned -File GetLinks。ps1

在这个 .ps1 文件的顶部,我有:"Import-Module .\Azure.ps1"。由于我在我的 WebJob 运行 日志中看到 "INFO" 语句,因此 运行 成功了。

接下来,我的 PS 脚本尝试 运行 Azure PS 命令,但我仍然遇到与下面的示例错误相同的错误。


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 运行s 所在的沙箱当前不支持 Azure PowerShell。有多种因素:

  1. CmdLets 没有安装在 worker 上
  2. 即使安装了,也有一些问题会阻止它们运行正确
  3. 即使他们 运行,您也需要在 运行ning 命令之前进行身份验证。最后一部分可以使用服务主体解决。

因素 #2 是最大的障碍。我们希望能够做到这一点,但现在还很棘手。

一种可能的解决方法是直接执行 ARM 请求,尽管这需要更多工作(并且您仍然需要使用服务主体进行授权)。您也可以编写 C# 代码来进行调用。

另一种可能的选择是使用节点脚本和 azure cli。 las,我也尝试使用 .sh 脚本解决这个问题,但是尝试设置 azure cli env 会失败(请参阅 https://github.com/projectkudu/kudu/issues/1935)。最后,如果您只需要存储功能,可以尝试使用 SAS 令牌和 http 请求在您自己的 ps 功能中做一些基本的事情...