【问题标题】:Cannot process argument transformation on parameter无法处理参数的参数转换
【发布时间】:2015-01-21 16:16:11
【问题描述】:

我声明了一个元组数组如下:

 [System.Tuple[string,string][]] $files = @()

我有以下工作流程:

Workflow Perform-Download
{
    Param (
        [Parameter(Mandatory = $true)]
        [System.Tuple[string,string][]] $Files
    )
    ForEach -Parallel ($file in $Files)
    {
        Parallel{Save-File -Url $file.Item1 -DestinationFolder $file.Item2}
    }
}

我正在尝试执行以下操作:

Perform-Download -Files $files

但我收到以下错误:

Perform-Download : Cannot process argument transformation on parameter 'Files'. Cannot convert the 
"System.Tuple`2[System.String,System.String][]" value of type "System.Tuple`2[[System.String, mscorlib, Version=4.0.0.0, 
Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, 
PublicKeyToken=b77a5c561934e089]][]" to type "System.Tuple".
At line:1 char:26
+ Perform-Download -Files $files
+                          ~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Perform-Download], ParameterBindingArgumentTransformationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Perform-Download

我做错了什么?

【问题讨论】:

    标签: powershell arguments


    【解决方案1】:

    利用 What Will Be Cool 所写的内容,我尝试了以下方法。基本上$Files 作为[array] 传递,然后它被类型转换为一个元组。 我创建了一个虚拟Save-File,它只将参数写入输出。

    我不知道为什么不能直接将元组作为参数传递,你可能发现了一个错误。

    $files = @([System.Tuple]::Create("Flinstone","Rubble"), [System.Tuple]::Create("Simpsons","Flanders"))
    function Save-File 
    {
        Param ($URL, $DestinationFolder)
        Write-output ("{0} {1}" -f $URL, $DestinationFolder)    
    }
    Workflow Perform-Download
    {
        Param (
        [Parameter(Mandatory = $true)]
        [array] $Files
        )
    
        $Files = [System.Tuple[string,string][]] $Files
        ForEach -Parallel ($file in $Files)
        {
            Parallel{Save-File -Url $file.Item1 -DestinationFolder $file.Item2}
        }
    }
    
    Perform-Download -Files $files
    

    【讨论】:

      【解决方案2】:

      这在一个简单的脚本中起作用:

      param([System.Tuple[string,string][]]$files)
      
      foreach ($file in $files) {
          [console]::WriteLine("Item1: {0}, Item2: {1}", $file.Item1, $file.Item2)
      }
      

      然后设置数据并调用脚本。

      PS C:\data> $fileList = @([System.Tuple]::Create("Flinstone","Rubble"), [System.Tuple]::Create("Simpsons","Flanders"))
      
      PS C:\data> .\soTuple.ps1 -files $fileList
      Item1: Flinstone, Item2: Rubble
      Item1: Simpsons, Item2: Flanders
      

      using Tuples in PowerShell上查看这篇文章

      【讨论】:

      • 您好,感谢您的回答。我已经知道,如果我使用函数而不是工作流,脚本就可以工作。我只是不明白为什么它不适用于工作流
      猜你喜欢
      • 2014-02-26
      • 2021-11-22
      • 2023-03-29
      • 2022-01-12
      • 2017-08-22
      • 2021-05-22
      • 2015-05-14
      • 2015-01-11
      • 2014-05-31
      相关资源
      最近更新 更多