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!
$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
$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('"')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With