我有一个脚本,如下所示,它在 sys 驱动器上搜索最新的 MSDeploy 可执行文件。
但是,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;
但这里证明了作为参数传递的变量的类型确实是正确的(显然它们不是,不知何故,否则我不会在这里):
他们都是FileInfo
,论点都是那种类型。那么我错过了什么?