-1

It might be that the title of the question is somewhat misleading, but I suspect that there has been done some changes from 2.0 to 4.0 which I am not aware of...so here goes

The following code will provide me with the expected result on our Windows 2008 servers using Powershell 2.0

## Get files/items using above variables
$LogFiles = @(Get-ChildItem "$IhsLogFolder\*.*" -include $LogFileMatch)  
$log.debugFormat("Found in {0} are {1}", $IhsLogFolder, $LogFiles)
foreach ($LogFile in $LogFiles) {
   $log.debugFormat("Found {0}", $LogFile.Name)
}

$LogFiles += @(Get-ChildItem "$PluginLogFolder" -recurse -include $LogFileMatch)
$log.debugFormat("Found in {0} are {1}", $PluginLogFolder, $LogFiles)

The command being executed will be

Get-ChildItem e:\logs\HTTPServer\*.* -include error.log,access.log,http_plugin.log

and

Get-ChildItem e:\logs\WebPlugin\webserver\*.* -include error.log,access.log,http_plugin.log

Trying the same script on Windows 2012 with Powershell 4.0 the above code will not return any files, but if I try just the command in Powershell it will find files

PS C:\> Get-ChildItem e:\logs\HTTPServer\*.* -include error.log,access.log,http_plugin.log
Directory: E:\logs\HTTPServer
Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        20.10.2015     15:35    4296922 access.log
-a---        19.10.2015     15:26     189102 error.log

Are there any obvious reasons for the @(Get-ChildItem... not returning files in Powershell 4.0?

Update

  1. Ref. comment - Yes I'm sure that $log is not the problem
  2. I did just figure out that the following does work (hardcoding the include) $LogFiles = @(Get-ChildItem "$IhsLogFolder\*.*" -include error.log,access.log,http_plugin.log), applying the include as a variable with more than one file does not return any results on our 4.0 servers, but using the variable and just a single file works.

A better code snippet

$pathToFolder = "d:\temp\*.*"
# This does not return results
$includeFile = "file1.txt,file2.txt"
# This will return results
#$includeFile = "file1.txt"
Write-Host $includeFile 
# Below does not work if more than one file in the include
#$result = @(Get-ChildItem $pathToFolder -include $includeFile)
# Below does work even if more than one file in the include
$result = @(Get-ChildItem $pathToFolder -include file1.txt,file2.txt)
Write-Host $result

if ($result) {
    Write-Host "Yes"
} else {
    Write-Host "No"
}`
rhellem
  • 769
  • 1
  • 11
  • 26

1 Answers1

0

The -include parameter takes a string array:

> Get-Help Get-ChildItem -Full
  ...
  -Include <string[]>

So, constructing $includeFile as an array should work, currently you're setting it as a string.

Try instead:

$includeFile = "file1.txt","file2.txt"
$result = Get-ChildItem $pathToFolder -include $includeFile
arco444
  • 22,002
  • 12
  • 63
  • 67