Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get-ChildItem using variable for -Include Not Working

Tags:

powershell

Current Version of the Question:

I initialize an array $tbd_list as follows:

$tbd_list = @()
foreach($i in $tbd) {
    # -- If statement prevents duplicate id's
    if(!($tbd_list -match $i.id)){
        $tbd_list += $i.id
    }
}

At this point, if I print $tbd_list I get:

"dce50fdd-2298-45db-8aac-fb13a176fe1a*.*"
"79c1538f-09b1-4ed7-b84c-ff1a7934596a*.*"
"60511686-7a87-4352-a781-62e7c04913e5*.*"
"0821f8ee-c1ca-4568-ab68-bb5e30d07268*.*"
"3b9f5da0-fc2d-49e3-a11d-943f5ac2296a*.*"
"ff7720fd-2c0f-43c8-871a-e283f231c823*.*"

($tbd_list.GetType().FullName returns System.Object)

I then try to run the following code:

Get-ChildItem F:\store_test\ -Recurse -Include $tbd_list

But, I get nothing in return.

However, if I manually type this:

$tbd_list = "dce50fdd-2298-45db-8aac-fb13a176fe1a*.*", "79c1538f-09b1-4ed7-b84c-ff1a7934596a*.*", "60511686-7a87-4352-a781-62e7c04913e5*.*", "0821f8ee-c1ca-4568-ab68-bb5e30d07268*.*", "3b9f5da0-fc2d-49e3-a11d-943f5ac2296a*.*", "ff7720fd-2c0f-43c8-871a-e283f231c823*.*"

At this point, if I print $tbd_list I still get:

"dce50fdd-2298-45db-8aac-fb13a176fe1a*.*"
"79c1538f-09b1-4ed7-b84c-ff1a7934596a*.*"
"60511686-7a87-4352-a781-62e7c04913e5*.*"
"0821f8ee-c1ca-4568-ab68-bb5e30d07268*.*"
"3b9f5da0-fc2d-49e3-a11d-943f5ac2296a*.*"
"ff7720fd-2c0f-43c8-871a-e283f231c823*.*"

and run the same line ($tbd_list.GetType().FullName still returns System.Object):

Get-ChildItem F:\store_test\ -Recurse -Include $tbd_list

It works perfectly. Any suggestions?

Solution

Both answers below are correct, but I can only choose one.

After making sure the variable was an array, I then had to remove the leading and trailing quotes and the script works. Thanks!

like image 965
jascissom Avatar asked Apr 09 '12 14:04

jascissom


2 Answers

$tbd_list.GetType().FullName gives System.String

While not apparent from your question how you defined the $tbd_list, but it should have been an array and only if it were an array would it work with Get-ChildItem's Include option. It wouldn't work in the way you expect if it were a string. Define it properly:

$tbd_list = "dce50fdd-2298-45db-8aac-fb13a176fe1a*.", "79c1538f-09b1-4ed7-b84c-ff1a7934596a.*", "dce50fdd-2298-45db-8aac-fb13a176fe1a*.", "79c1538f-09b1-4ed7-b84c-ff1a7934596a.*", "dce50fdd-2298-45db-8aac-fb13a176fe1a*.", "60511686-7a87-4352-a781-62e7c04913e5.*", "0821f8ee-c1ca-4568-ab68-bb5e30d07268*.", "3b9f5da0-fc2d-49e3-a11d-943f5ac2296a.*", "ff7720fd-2c0f-43c8-871a-e283f231c823*.*"

Using the above line works for me with Get-ChildItem

Update:

This is where you are doing it wrong:

$tbd_list = "$tbd_list, " + $i.id

It will construct a string.

You will have to initialize $tbd_list as an array:

$tbd_list = @()

and add the items you want to it:

$tbd_list += $i.id
like image 101
manojlds Avatar answered Oct 22 '22 08:10

manojlds


$i.id should not include the quote chars. On the command line, they designate a string but when those characters are part of the string stored in a string variable, it's not going to work for you e.g.:

9 >  gci t*.txt


    Directory: Microsoft.PowerShell.Core\FileSystem::C:\Users\Keith


Mode           LastWriteTime       Length Name
----           -------------       ------ ----
-a---      4/3/2012  8:54 PM           13 test.txt
-a---    10/20/2011  9:13 AM         2872 trace.txt


10 >  $a = "`"t*.txt`""
11 >  $a
"t*.txt"
12 >  gci * -Include $a

Strip off the leading and trailing quotes like so:

$tbd_list += $i.id.Trim('"')
like image 33
Keith Hill Avatar answered Oct 22 '22 07:10

Keith Hill