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"

Sign up to request clarification or add additional context in comments.

1 Comment

your first statement is really arguable, python, bash, cmd, powershell all work exactly the same
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 Comment

this part is cut off for some reason.. -version "11.0.9700"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.