Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Get-ChildItem parameter -include not working

$
0
0

Hi,

I am looking for a solution to parse a variable as input for the command "Get-ChildItem" - include option. First of all the purpose of the function. I need a function to dynamic scan a folder where the filter, what to process, is the file name extention.

I use the Get-ChildItem. To get only the files with the extention i need the -include property contains one or more file extentions (example -include "*.exe", "*.dll". It works fine in the following case:

$directory = "y:\"
Get-ChildItem $directory -Recurse -Include "*.exe", "*.dll"

Next thing is to build a function to make the static filter dynamic. The script looks like:    

 param (
    [string]$dbserver = "localhost",
    #[Parameter(Mandatory=$true)][string]$voorbeeld,
    [string]$directory = $( Read-Host "Input folder to scan"), 
    [string]$extentions = '"*.dll", "*.txt"'
 )

function getdeployinfo()
{
    param(
        [Parameter(Mandatory=$true)][string]$Path,
        [Parameter(Mandatory=$true)][string]$filter        
    )
    Write-Debug $extentions
    Write-Debug $filter

    foreach ($item in Get-ChildItem $directory -Recurse -Include $filter)
    {
    Write-Host $item.Name
    }
}

Write-Debug $extentions
getdeployinfo -path $directory -filter ([string]$extentions)

The result for the debug is:

DEBUG: "*.dll", "*.txt"
DEBUG: "*.dll", "*.txt"
DEBUG: "*.dll", "*.txt"

There are no items found. I looks like the -include will not accept a variable. When i change "-Include $filter" to -include "*.dll", "*.txt" i get results. 

What's wrong with my script code?


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>