I have a script, see below, that searches for the most recent MSDeploy executable on the sys drive.
However, my Compare-FileVersion
function is not called due to the following error:
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
Here's the script:
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;
But here's proof that the types of the variables being passed as parameters are indeed correct (clearly they're not, somehow, else I wouldn't be here on SO):
They're both FileInfo
, the arguments are both of that type. So what am I missing?
You will get this error if you pass arguments like this
$winner = Compare-FileVersions($incumbent,$challenger)
try to separate these two variable with a space like this and it will work.
$winner = (Compare-FileVersions $incumbent $challenger)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With