【问题标题】:Running Azure PowerShell commands from a webjob从 Web 作业运行 Azure PowerShell 命令
【发布时间】:2015-12-31 20:20:45
【问题描述】:

更新:我按照 BenV 的以下建议使用了 Azure 自动化,它成功了!更多信息可以找到here


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

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

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

一篇较早的 MSDN 博文建议在 PS 脚本中添加“Import-Module Azure.ps1”,并将 Azure.ps1 包含在 webjob zip 文件中。 (它实际上是来自我本地 C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure 的 Azure.psd1)。另外,我尝试了使用 Azure.psd1 的 Import-Module 然后 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 的要求是什么?有什么理由不能使用Azure Automation
  • 谢谢。我将研究 Azure 自动化。另一种选择是按照 David 下面的建议编写 C# 应用程序。

标签: powershell azure azure-webjobs


【解决方案1】:

运行 WebJobs 的沙盒当前不支持 Azure PowerShell。有多种因素:

  1. 工作程序上未安装 CmdLets
  2. 即使已安装,也存在阻止它们正常运行的问题
  3. 即使它们运行,您也需要在运行命令之前进行身份验证。最后一部分可以使用服务主体解决。

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

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

【讨论】:

  • 还是这样吗?似乎 webjobs 可以运行 ps 和 bash 脚本,但我仍然看到与上述类似的错误
【解决方案2】:

另一个可能的选项可能是使用节点脚本和 azure cli。唉,我也尝试使用 .sh 脚本解决此问题,但尝试设置 azure cli env 将失败(请参阅https://github.com/projectkudu/kudu/issues/1935)。最后,如果您只需要存储功能,可以尝试使用 SAS 令牌和 http 请求在您自己的 ps 函数中执行基本操作...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-21
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 2020-04-12
    • 2018-02-16
    • 1970-01-01
    相关资源
    最近更新 更多