Gemma
Gemma

Reputation: 11

Querying for multiple file versions using Test-Path

I want it to query version of several files using multi-host text file then output to CSV. If I only enter one $filename variable in the $filepath variable, then it works. Just can't put $filename, $filename1, and $filename2.

$filename = "\Windows\System32\browser.dll"
$filename1 = "\Program Files\Logitech\SetPointP\setpoint.exe"
$filename2 = "\Program Files\MAGIX\Photostory Deluxe\2018\Fotos_dlx.exe"

$obj = New-Object System.Collections.ArrayList 

$computernames = Get-Content C:\Temp\computers.txt 
foreach ($computer in $computernames) 
{ 
$filepath = Test-Path "\\$computer\c$\$filename,$filename1,$filename2" 

if ($filepath -eq "True") { 
$file = Get-Item "\\$computer\c$\$filename" 


        $obj += New-Object psObject -Property @{'Computer'=$computer;'FileVersion'=$file.VersionInfo|Select FileVersion} 
        } 
     } 

$obj | select computer, FileVersion | Export-Csv -Path 'c:\Temp\File_Results.csv' -NoTypeInformation

Upvotes: 1

Views: 129

Answers (3)

No Refunds No Returns
No Refunds No Returns

Reputation: 8336

Shorter is simpler?

$found = true ; $filepath = Test-Path "\\$computer\c$\$filename", "$filename1" , "$filename2" | % {$found = $found -and $_ }

Test-Path does accept an array of file names and you can enter it much as you started out in your example. This is a single-line example and some purists may suggest using ForEach-Object in place of %.

Alternately you could possibly simplify by using the -notContains syntax suggested by @zdan.

Upvotes: 0

user6811411
user6811411

Reputation:

You'll need a ForEach to process the array of files,
but you can maintain a boolean end result by anding the current Test-Path:

## Q:\Test\2018\11\23\SO_53452648.ps1
$filename = "\Windows\System32\browser.dll"
$filename1 = "\Program Files\Logitech\SetPointP\setpoint.exe"
$filename2 = "\Program Files\MAGIX\Photostory Deluxe\2018\Fotos_dlx.exe"
$filesToTest = @($filename, $filename1, $filename2)

Clear-Variable filesExist -ErrorAction SilentlyContinue
$Computer = $ENV:COMPUTERNAME

$filesToTest | ForEach-Object {
    "{0,5} Test-Path {1}" -f (Test-Path "\\$computer\c$\$_"),"\\$computer\c$\$_"
    $filesexist = $filesexist -and (Test-Path "\\$computer\c$\$_")
}
"="*40
"{0,5} filesexist on {1}" -f $filesexist,$computer

Sample output.

> Q:\Test\2018\11\23\SO_53452648.ps1
 True Test-Path \\HP-G1610\c$\\Windows\System32\browser.dll
 True Test-Path \\HP-G1610\c$\\Program Files\Logitech\SetPointP\setpoint.exe
False Test-Path \\HP-G1610\c$\\Program Files\MAGIX\Photostory Deluxe\2018\Fotos_dlx.exe
========================================
False filesexist on HP-G1610

Upvotes: 0

zdan
zdan

Reputation: 29450

Test-Path will accept an array of files to test, but you aren't building the array correctly. First put all the file paths you want to test in an array

$filename = "\Windows\System32\browser.dll"
$filename1 = "\Program Files\Logitech\SetPointP\setpoint.exe"
$filename2 = "\Program Files\MAGIX\Photostory Deluxe\2018\Fotos_dlx.exe"
$filesToTest = @($filename, $filename1, $filename2)

Then later, you can test each:

$filesExist = $filesToTest | foreach {Test-Path "\\$computer\c$\$_"}

$filesExist contains an array of booleans so you can just check to make sure they're all true:

if($filesExist -notcontains $false)
     #get the file info

Upvotes: 2

Related Questions