Update:我按照下面 BenV 的建议使用了 Azure 自动化,并且成功了!更多信息可以找到here.
我有一个 PowerShell 脚本,需要运行一些 Azure 命令,例如New-AzureStorageContext
, Get-AzureStorageContainer
, Set-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 和 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
+ FullQualifiedErrorId:CommandNotFoundException