当前版本的问题:
我初始化一个数组 $tbd_list 如下:
$tbd_list = @()
foreach($i in $tbd) {
# -- If statement prevents duplicate id's
if(!($tbd_list -match $i.id)){
$tbd_list += $i.id
}
}
此时,如果我打印 $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*.*"
($tbd_list.GetType().FullName 返回 System.Object)
然后我尝试运行以下代码:
Get-ChildItem F:\store_test\ -Recurse -Include $tbd_list
但是,我没有得到任何回报。
但是,如果我手动输入:
$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*.*"
此时,如果我打印 $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*.*"
并运行同一行($tbd_list.GetType().FullName 仍返回 System.Object):
Get-ChildItem F:\store_test\ -Recurse -Include $tbd_list
它完美地工作。有什么建议么?
解决方案
以下两个答案都是正确的,但我只能选择一个。
在确保变量是一个数组之后,我必须删除前导引号和尾随引号,脚本才能工作。谢谢!