我在powershell中定义了一个哈希表,如下所示:
$jobs = @{
"Test1"=[Array](
[Array]("\\server1\file1.txt", "\\server2\file1.txt")
);
"Test2"=[Array](
[Array]("\\sever1\file2.txt", "\\server2\file2.txt"),
[Array]("\\server1\file3.txt", "\\server2\file3.txt")
);
}
我试图枚举这个集合并在每对上调用一个函数。 Test1有1个文件比较,Test2有两个文件比较。
$jobs.GetEnumerator() | Sort-Object Key | foreach {
LogWrite($_.Key + ": comparing files")
$_.value | foreach {
LogWrite("Comparing files '" + $_[0] + "' and '" + $_[1] + "'")
#$r = CompareFiles($_[0], $_[1])
#LogWrite("Result : " + $r)
}
LogWrite($_.Key + ": comparison successful")
LogWrite("")
}
我得到的输出是:
Test1: comparing files
Comparing files '\' and '\'
Comparing files '\' and '\'
Test1: comparison successful
Test2: comparing files
Comparing files '\\server1\file2.txt' and '\\server2\file2.txt'
Comparing files '\\server1\file3.txt' and '\\server2\file3.txt'
Test2: comparison successful
Powershell(某事)似乎正在创造相同大小的数组。任何人都可以提出更好的数据结构或解决方案吗?
答案 0 :(得分:3)
引用Avoiding Agnostic Jagged Array Flattening in Powershell,如果在单元素数组中添加逗号,则会出现这种情况,它会阻止它被展平:
$jobs = @{
"Test1"=[Array](,
[Array]("\\server1\file1.txt", "\\server2\file1.txt")
);
"Test2"=[Array](
[Array]("\\sever1\file2.txt", "\\server2\file2.txt"),
[Array]("\\server1\file3.txt", "\\server2\file3.txt")
);
}
在Powershell中处理数组的方式一定有些奇怪。