1

I have the following function which compares a file version string to an actual file and returns 1 if the file is lower:

function FileVersionDetectionCheck() {
param([string]$file)
$fileversion = (get-item $file).VersionInfo.ProductVersion
$fileversionobject = [System.Version]$fileversion
$targetversion = [System.Version]::Parse("11.0.9700")

Write-Output "File Version:" $fileversionobject
Write-Output "Target Version:" $targetversion

if($fileversionobject -ge $targetversion) {
    return 0
}
else {
    return 1
}
}

FileVersionDetectionCheck("C:\program files\internet explorer\iexplore.exe")

This code works fine. However, if I add a second string parameter...

function FileVersionDetectionCheck() {
param([string]$file,[string]$version)
$fileversion = (get-item $file).VersionInfo.ProductVersion
$fileversionobject = [System.Version]$fileversion
#$targetversion = [System.Version]::Parse("11.0.9700")
$targetversion = [System.Version]$version

Write-Output "File Version:" $fileversionobject
Write-Output "Target Version:" $targetversion

if($fileversionobject -ge $targetversion) {
    return 0
}
else {
    return 1
}
}

FileVersionDetectionCheck("C:\program files\internet explorer\iexplore.exe", 
"11.0.9700")

It errors out with:

get-item : Cannot find path 'C:\program files\internet explorer\iexplore.exe 11.0.9700' because it does not exist.

Seems to be reading both parameters as a single string.

As far as I can tell this is a valid way to pass multiple parameters to a function. Am I doing something wrong or could this be a bug?

2 Answers 2

2

Powershell functions are not like methods in other languages. Remove the parentheses from your function name function FileVersionDetectionCheck() and then when you call the function do not use commas or parenthesis. For example

myFunction firstParam secondParam

With your code from above it would look like this.

FileVersionDetectionCheck "C:\program files\internet explorer\iexplore.exe" "11.0.9700"

1
  • your first statement is really arguable, python, bash, cmd, powershell all work exactly the same
    – 4c74356b41
    Commented Sep 19, 2017 at 18:49
0

To add on to Jason's answer, here is what it would look like with both parameters

function FileVersionDetectionCheck 
{
    param([string]$file,[string]$version)
    $fileversion = (get-item $file).VersionInfo.ProductVersion
    $fileversionobject = [System.Version]$fileversion
    #$targetversion = [System.Version]::Parse("11.0.9700")
    $targetversion = [System.Version]$version

    Write-Output "File Version:" $fileversionobject
    Write-Output "Target Version:" $targetversion

    if($fileversionobject -ge $targetversion) {
        return 0
    }else {
        return 1
    }
}

FileVersionDetectionCheck -file "C:\program files\internet explorer\iexplore.exe" -version "11.0.9700")
1
  • this part is cut off for some reason.. -version "11.0.9700"
    – cet51
    Commented Sep 19, 2017 at 18:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.