Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 使用流水线的数组_Powershell_Powershell 2.0 - Fatal编程技术网

Powershell 使用流水线的数组

Powershell 使用流水线的数组,powershell,powershell-2.0,Powershell,Powershell 2.0,我试图创建一个数组,其中有一个指向文件路径数组的文件夹名数组: Folder1: File1, File2, File3 Folder2: File1, File2, File3 etc... 我提出的代码是: #Paths of the folders being patched $HF_Folders_To_Patch_LIST = Get-Childitem $HF_Source_Path | Where-Object {$_.PSIsContainer} | Foreach-Objec

我试图创建一个数组,其中有一个指向文件路径数组的文件夹名数组:

Folder1: File1, File2, File3
Folder2: File1, File2, File3
etc...
我提出的代码是:

#Paths of the folders being patched
$HF_Folders_To_Patch_LIST = Get-Childitem $HF_Source_Path | Where-Object {$_.PSIsContainer} | Foreach-Object {$_.FullName}
$HF_FILES_LIST = $HF_Folders_To_Patch_LIST | ForEach-Object { ,@(Get-ChildItem -Path $FolderPath | Foreach-Object {$_.FullName}) }

根据我的理解,我应该使用“@()”或“,@()”,但是我似乎找不到太多关于在数组中在线创建数组的资源,我可能在谷歌上搜索错了。我这样做是对的,还是有可能这样做?我可以创建一个for循环,并可能得到我想要的结果,但我觉得在使用管道时,我好像误解了阵列在powershell中的工作方式

TechNet博客上有关于阵列中阵列的基本知识,以及如何通过阵列上的位置访问数据

可以使用ArrayList轻松地将项添加到数组中,添加到数组中的项可以是另一个数组

因此,让我们创建一个arraylist:

$myMainArrayList = New-Object System.Collections.ArrayList
现在,让我们创建一个新的arraylist,可以添加到主arraylist中

$mySubArrayList = New-Object System.Collections.ArrayList
现在可以将项目添加到子列表中:

$stringObject = "This is a string"
$mySubArrayList.add($stringObject)
现在您可以将子阵列添加到主阵列

$mySubArrayList.add($mySubArrayList)

您可以在主阵列中添加任意数量的子阵列。

查看下面的答案并意识到我需要类似hashmap的东西后,powershell hashtable函数就是答案。以下是适用于我的代码:

$HF_Folders_To_Patch_LIST = Get-Childitem $HF_Source_Path | Where-Object {$_.PSIsContainer} | Foreach-Object {$_.FullName}

$HF_FILES_LIST = @{}
$HF_Folders_To_Patch_LIST | ForEach-Object { $HF_FILES_LIST.Add($_, @(Get-ChildItem -Path $_ | Foreach-Object {$_.FullName})) }

现在我看到了,我看到了我的错误,我需要使用一个哈希表,在这里我希望将文件夹名作为键,将值作为文件路径数组。管道会给我一个未知数量的密钥,每个密钥有未知数量的文件。请不要在你的问题中编辑答案。如果您提出了自己的解决方案,您可以自由发布自己的答案。已修复。谢谢你的建议。