在 Invoke-AzureRmVMRunCommand 参数散列 table 中传递数组无效

Passing an array in an Invoke-AzureRmVMRunCommand parameter hash table not working

我正在尝试执行 Azure 自动化帐户 PowerShell 运行手册,该手册应使用以下语句在 Azure Win VM 上执行 PowerShell 脚本:

Invoke-AzureRmVMRunCommand -ResourceGroupName $sdvobjVM1.ResourceGroupName -Name $sdvstrNameVM1 -CommandId 'RunPowerShellScript' -ScriptPath $sdvstrScriptFileNameTmp -Parameter $sdvhshParamsFolderCopy

语句参数如下:

$sdvhshParamsFolderCopy = @{Para1='String1'; Para2='String2'; Para3='String3'; Para4='String4'; Para5='String5'; Para6=@('Application', 'Data', 'Execution')}

问题: 哈希 table 中的嵌入数组 ("Para6") 是否可行?如果是,怎么做?

我已经尝试了一段时间但没有成功,因为我无法访问远程脚本中的数组值;有关详细信息和实际代码示例,请参见下文。请有人帮忙!!!

问题详情:

在我的 Azure 自动化操作手册中:

...

[string] $sdvstrFileShare = "$sdvstrNameSA1.file.core.windows.net$sdvstrFileShareName"
[array] $sdvastrCopyFolders = @('Application', 'Data', 'Execution')

...

$sdvhshParamsFolderCopy = @{sdvstrNameSA1 = $sdvstrNameSA1;
                            sdvstrSA1AccessKey = $sdvstrSA1AccessKey;
                            sdvstrFileShare = $sdvstrFileShare;
                            sdvstrSrcDriveLetter = $sdvstrSrcDriveLetter;
                            sdvstrDstDriveLetter = $sdvstrDstDriveLetter;
                            sdvastrCopyFolders = $sdvastrCopyFolders
    }

(此处删除了换行符重音)

在我调用的脚本中:

# Parameters
Param (
    [string] $sdvstrNameSA1,
    [string] $sdvstrSA1AccessKey,
    [string] $sdvstrFileShare,      
    [string] $sdvstrSrcDriveLetter,
    [string] $sdvstrDstDriveLetter,     
    [array] $sdvastrCopyFolders
)

[array] $sdvastrCopyFolders 和 [array[]] $sdvastrCopyFolders 都不起作用。

症状是我正在进入调用的脚本:

$sdvastrCopyFolders | ForEach{$_} | Out-File  -FilePath 'D:\sdvastrCopyFoldersForEach.txt'
System.Object[]

$sdvastrCopyFolders.Item(0) | Out-File  -FilePath 'D:\sdvastrCopyFoldersItem0.txt'
System.Object[]

$sdvastrCopyFolders.GetValue(0) | Out-File  -FilePath 'D:\sdvastrCopyFoldersGetValue.txt'
System.Object[]

,但不是预期的数组成员:

Application
Data
Execution

如果我在普通 PowerShell 中的 2 个脚本之间测试此 HT/Array 参数 passing/extraction,它会按预期工作:

PS Z:\> $sdvastrCopyFolders.Length
3

PS Z:\> (,$sdvastrCopyFolders).GetValue(0)
Application
Data
Execution

PS Z:\> (,$sdvastrCopyFolders).GetValue(0).Item(2)
Execution

根据我的测试,当我们运行命令Invoke-AzureRmVMRunCommand时,后端会做一些动作来处理数组类型的参数值。那么我们就获取不到脚本中的值。但是后端不会处理字符串类型的参数。所以我建议你在脚本中使用字符串参数。如果一个参数需要一些值,可以用特殊字符分隔它们,例如,。例如:

我的自动化运行书:

$connectionName = "AzureRunAsConnection"
try
{
    # Get the connection "AzureRunAsConnection "
    $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName         

    "Logging in to Azure..."
    Add-AzureRmAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 
}
catch {
    if (!$servicePrincipalConnection)
    {
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    } else{
        Write-Error -Message $_.Exception
        throw $_.Exception
    }
}
Add-AzureRmAccount -Credential $mycreds -Tenant e4c9ab4e-bd27-40d5-8459-230ba2a757fb -ServicePrincipal 
}
catch {
    if (!$servicePrincipalConnection)
    {
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    } else{
        Write-Error -Message $_.Exception
        throw $_.Exception
    }
}
[string] $Filesharename = "test"
[string] $Folders ="test,test1"
$Params = @{Filesharename = $Filesharename;

                            Folders = $Folders;
    }


$groupName=' '
$vmName =' '
$path=' '

Invoke-AzureRmVMRunCommand -ResourceGroupName $groupName -Name $vmName -CommandId'RunPowerShellScript' -ScriptPath $path -Parameter $Params


脚本:

  Param (
        [string] $Filesharename, 
        [string] $Folders 
        )


      $Folders | Out-File -FilePath 'C:\test.txt' -Append
      $test =  $Folders -split ','

      $test.GetType() | Out-File -FilePath 'C:\test.txt' -Append

      $test.Length | Out-File -FilePath 'C:\test.txt' -Append

      "-----------------------" | Out-File -FilePath 'C:\test.txt' -Append
      $test | foreach{$_} |  Out-File -FilePath 'C:\test.txt' -Append

      "-----------------------" | Out-File -FilePath 'C:\test.txt' -Append

      $test[0] | Out-File -FilePath 'C:\test.txt' -Append


结果: