Adam
Adam

Reputation: 89

powershell variable directory is returning null

I am trying to get a list of files that are not the version I want them to be, I created a function with 3 variables:

If the file version doesn't match $version I write the line out so I know the file name and the version number it actually is.

Function Check-Version ($version, $folderName, $folderPath)
{
    Write-Host $version, $folderName, $folderPath
    $list = get-childitem $folderPath\* -include *.dll,*.exe
    foreach ($one in $list)
    {
        If ([System.Diagnostics.FileVersionInfo]::GetVersionInfo($one).FileVersion -ne $version)
        {
            $line = "{0}`t{1}" -f [System.Diagnostics.FileVersionInfo]::GetVersionInfo($one).FileVersion, $one.Name
            Write-Host $line
        }
    }
}

Check-Version ("1.0", "bin", "C:\bin")

My problem is the path variable is NULL when I use get-childitem, but if I use write-host it is correct.

The Write-Host line at the top returns the correct values.

If I try cd $folderPath I get the error:

cd : Cannot process argument because the value of argument "path" is null. Change the value of argument "path" to a non-null value.

I don't understand why the $folderPath is NULL when I try to go to that directory.

Upvotes: 1

Views: 513

Answers (1)

BenH
BenH

Reputation: 10044

Your issue is that you are passing 3 parameters into the first argument as an array, rather than passing three separate arguments. Change Check-Version ("1.0", "bin", "C:\bin") -> Check-Version "1.0" "bin" "C:\bin"

You could see the difference by splitting your Write-Host into 3 lines:

Function Check-Version ($version, $folderName, $folderPath) {
    Write-Host "Version: $version"
    Write-Host "FolderName: $folderName"
    Write-Host "FolderPath: $folderPath"
    $list = get-childitem $folderPath\* -include *.dll,*.exe
    Set-Location $folderPath
    foreach ($one in $list) {
        If ([System.Diagnostics.FileVersionInfo]::GetVersionInfo($one).FileVersion -ne $version) {
            $line = "{0}`t{1}" -f [System.Diagnostics.FileVersionInfo]::GetVersionInfo($one).FileVersion, $one.Name
            Write-Host $line
        }
    }
}

Check-Version "1.0" "bin" "C:\bin"

Upvotes: 1

Related Questions