2013-09-27 44 views
9

我有一個腳本,見下文,它搜索sys驅動器上最新的MSDeploy可執行文件。ParameterArgumentTransformationError

但是,我Compare-FileVersion功能不叫由於以下錯誤:

Compare-FileVersions : Cannot process argument transformation on parameter 'file1'. Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.IO.FileInfo". At C:\DATA\Git\PowerShell\Test-Command.ps1:32 char:39 
+   $winner = Compare-FileVersions($incumbent, $challenger); 
+          ~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidData: (:) [Compare-FileVersions], ParameterBindingArgumentTransformationException 
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Compare-FileVersions 

這裏的腳本:

function Find-Executable() 
{ 
    # Find all MS Deploy executables and then make a table of path and version. Reverse sort and pick top one. 

    pushd; 

    # Workaround for bug in PS where ErrorAction spec'ed in the argument is ignored. http://stackoverflow.com/questions/17489372/ls-recurse-erroraction-silentlycontinue-doesnt-work 

    # Bug is not fixed on build server with this code. 

    $originalEAP = $ErrorActionPreference; 
    $ErrorActionPreference = "SilentlyContinue"; 

    cd $env:SystemDrive; 
    cd \; 
    [System.IO.FileInfo[]]$allExecutables = ls -Include msdeploy.exe -Recurse -Force -ErrorAction SilentlyContinue; 

    $ErrorActionPreference = $originalEAP; 

    popd; 

    if ($allExecutables.Count -lt 1) 
    { 
     throw $("No MS Deploy executables found in folders in " + $env:SystemDrive); 
    }  

    [System.IO.FileInfo]$incumbent = $allExecutables[0]; 
    for($i = 0; $i -lt $allExecutables.Count; $i++) 
    {   
     [System.IO.FileInfo]$challenger = $allExecutables[$i]; 
     $winner = Compare-FileVersions($incumbent, $challenger); 
     $incumbent = $winner; 
    } 

    return $winner; 
} 

function Compare-FileVersions([System.IO.FileInfo]$file1, [System.IO.FileInfo]$file2) 
{ 
    if ($file1.VersionInfo.FileMajorPart -gt $file2.VersionInfo.FileMajorPart) 
    { 
     return $file1; 
    } 
    elseif ($file2.VersionInfo.FileMajorPart -gt $file1.VersionInfo.FileMajorPart) 
    { 
     return $file2; 
    } 

    if ($file1.VersionInfo.FileMinorPart -gt $file2.VersionInfo.FileMinorPart) 
    { 
     return $file1; 
    } 
    elseif ($file2.VersionInfo.FileMinorPart -gt $file1.VersionInfo.FileMinorPart) 
    { 
     return $file2; 
    } 

    if ($file1.VersionInfo.FileBuildPart -gt $file2.VersionInfo.FileBuildPart) 
    { 
     return $file1; 
    } 
    elseif ($file2.VersionInfo.FileBuildPart -gt $file1.VersionInfo.FileBuildPart) 
    { 
     return $file2; 
    } 

    if ($file1.VersionInfo.FilePrivatePart -gt $file2.VersionInfo.FilePrivatePart) 
    { 
     return $file1; 
    } 
    elseif ($file2.VersionInfo.FilePrivatePart -gt $file1.VersionInfo.FilePrivatePart) 
    { 
     return $file2; 
    } 

    # They're both the same at this point. 

    return $file1; 
} 

$version = Find-Executable; 

echo $version; 

但這裏證明了變量的類型被作爲參數傳遞是確實是正確的(顯然它們不是,不知何故,否則我不會在這裏):

Screenshot showing variable types

他們都是FileInfo,參數都是這種類型。那麼我錯過了什麼?

回答

17

如果傳遞參數,像這樣

$winner = Compare-FileVersions($incumbent,$challenger) 

嘗試這兩個變量,這樣的空格分隔,您將收到此錯誤,它會工作。

$winner = (Compare-FileVersions $incumbent $challenger) 
+2

Lord!我希望我能說這是我第一次這樣做,但在C#中度過了10年,我很容易就陷入了我的「母語」。 –

+2

這是我寫第一個PS函數時遇到的第一件事。 – Mitul

+1

如果使用Set-StrictMode -v 2,則當您使用parens傳遞多個參數以幫助完全捕獲此問題時,PowerShell將報告錯誤。 –

相關問題