Set-AzBatchPool starttask 资源文件未找到构造函数 Microsoft.Azure.Commands.Batch.Models.PSResourceFile

问题描述

我正在尝试通过 powershell 和 Az 模块设置 Azure Batch 池启动任务。需要设置 ReosurceFiles 但不能为其创建对象 System.Collections.Generic.IList[Microsoft.Azure.Commands.Batch.Models.PSResourceFile]

New-AzBatchResourceFile - 不适用,因为它创建了 Microsoft.Azure.Commands.Batch.Models.PSResourceFile 对象类型

当我尝试使用时,我发现了这个错误

异常设置“ResourceFiles”:“无法将“Microsoft.Azure.Commands.Batch.Models.PSResourceFile”类型的“Microsoft.Azure.Commands.Batch.Models.PSResourceFile”值转换为 “System.Collections.Generic.IList`1[Microsoft.Azure.Commands.Batch.Models.PSResourceFile]”。” 在行:1 字符:1 $startTask.ResourceFiles = $file

   CategoryInfo          : NotSpecified: (:) [],SetValueInvocationException
   FullyQualifiedErrorId : ExceptionWhenSetting

这是完整的 PSell 代码

#Set Pool settings
$startTask = New-Object Microsoft.Azure.Commands.Batch.Models.PsstartTask
$UserIdentity = New-Object Microsoft.Azure.Commands.Batch.Models.PSAutoUserSpecification -ArgumentList @("Task","Admin")
$task= "/bin/sh -c `"~/blob2.sh $adadmin $publickey $accountName $accountKey $containerName`""
$startTask.CommandLine = "$task"
$startTask.MaxTaskRetryCount = 2
$startTask.WaitForSuccess = $true
$startTask.UserIdentity = $UserIdentity
$file = New-AzBatchResourceFile -HttpUrl "https://$accountName.blob.core.windows.net/scripts/blob2.sh" -FilePath "blob2.sh"
$startTask.ResourceFiles = $file
$Pool.StartTask = $startTask
Set-AzBatchPool -Pool $Pool -BatchContext $ctx

解决方法

你可以在上面的代码中尝试瘦:

$files = @()
$file = New-AzBatchResourceFile -HttpUrl "https://$accountName.blob.core.windows.net/scripts/blob2.sh" -FilePath "blob2.sh"
$files += $file
$StartTask.ResourceFiles = $files

说明,$files 是一个通用列表,我将向其中添加 $file 类型的 Microsoft.Azure.Commands.Batch.Models.PSResourceFile