Powershell 如何从数组中的值引用原始对象?

Powershell 如何从数组中的值引用原始对象?,powershell,Powershell,我已经做了很多搜索,但似乎找不到这个问题的答案,但是如果已经找到了答案,我道歉,如果可以的话,请将我链接到它 我要做的是根据当前文件数最少的路径,在6个不同的路径上分发文件 我想做的是将这些($Queue1-6只是文件路径)的响应添加到一个数组中,然后对它们进行排序并从第一个对象获取路径 $QueueFiles1 = ( Get-ChildItem $Queue1 | Measure-Object ).Count $QueueFiles2 = ( Get-ChildItem $Queue2 |

我已经做了很多搜索,但似乎找不到这个问题的答案,但是如果已经找到了答案,我道歉,如果可以的话,请将我链接到它

我要做的是根据当前文件数最少的路径,在6个不同的路径上分发文件

我想做的是将这些($Queue1-6只是文件路径)的响应添加到一个数组中,然后对它们进行排序并从第一个对象获取路径

$QueueFiles1 = ( Get-ChildItem $Queue1 | Measure-Object ).Count

$QueueFiles2 = ( Get-ChildItem $Queue2 | Measure-Object ).Count

$QueueFiles3 = ( Get-ChildItem $Queue3 | Measure-Object ).Count

$QueueFiles4 = ( Get-ChildItem $Queue4 | Measure-Object ).Count

$QueueFiles5 = ( Get-ChildItem $Queue5 | Measure-Object ).Count

$QueueFiles6 = ( Get-ChildItem $Queue6 | Measure-Object ).Count

$FileNumArray = @($QueueFiles1, $QueueFiles2, $QueueFiles3, $QueueFiles4, $QueueFiles5, $QueueFiles6)

$FileNumArray = $FileNumArray | Sort-Object
问题是(据我所知),在将这些值添加到数组后,对象丢失,剩下的就是值,因此现在我不知道如何引用原始对象以获取路径信息

如果有更简单的方法比较这些文件计数值并获得最低值的路径信息,则不需要像这样使用数组来完成此操作

此外,如果有多个路径具有最低值,则返回哪个路径无关紧要


提前感谢您的帮助。

请注意,我使用了自己的一些文件夹来代替
$queue
。还取决于这些文件的位置,您可能能够为每个文件构建一个简单的循环,即:如果它们都是同一父文件的子文件夹

$Queue1 = "C:\Temp\NewName"
$Queue2 = "C:\temp\TaskManagement"
$Queue3 = "C:\temp\message_log.csv"

# Build a hashtable. Add each queue to the hash table. 
$fileCount = @{}
# Set the queue as the name and the count as the value
$fileCount.Add("$Queue1", (Get-ChildItem $Queue1 | Measure-Object ).Count)
$fileCount.Add("$Queue2", (Get-ChildItem $Queue2 | Measure-Object ).Count)
$fileCount.Add("$Queue3", (Get-ChildItem $Queue4 | Measure-Object ).Count)

# Sort the results by the value of the hashtable (Counts from earlier) and select only the one. 
$fileCount.GetEnumerator() | Sort-Object value | Select-Object -First 1 -ExpandProperty Name
解释最后一行

排序哈希表需要使用
.GetEnumerator()
。 默认情况下,Sort对象是升序对象,因此无需提及它。
选择Object-First 1
如果你不在乎你得到的是哪一个,只要它有最小数量的文件。
-ExpandProperty Name
,因为您实际上只需要路径,而不需要哈希表条目本身

$QueueFiles1=(获取ChildItem$Queue1 |度量对象)。计数
捕获的只是计数。此处未指定对象。我正在使用哈希表构建答案。