【发布时间】:2014-12-05 17:18:11
【问题描述】:
我正在尝试从数组中提取一些文件路径并使用它们来测试文件夹是否位于服务器上。 从二维数组中提取值并将其放入变量中时,我似乎遇到了错误。
这是我的脚本的精简版,非常基本的版本。
$PCS = "PC1","PC2","PC3"
$locations= @("System32","Windows\System32"),
("Public","Users\Public")
ForEach($PC in $PCS){
$i=0
Do
{
$fullpath = "\\" + $PC + "\C$\" + "$locations[$i][1]"
test-path $fullpath
$i++
}
While ($i -le ($locations.length-1) )
}
但是,当我使用 $fullpath 来测试文件夹是否存在时,我得到了错误,并且经过进一步调查,$fullpath 的实际值是:
\\PC1\C$\System.Object[] System.Object[] System.Object[] System.Object[]
如何从数组中获取值,以便将其用作文件路径位置?
【问题讨论】:
-
您正在尝试访问
$locations[$i],但您从未设置$i -
感谢 arco444 - 我错过了部分初始代码,抱歉!现已添加,谢谢
-
我很难相信 do-while 循环甚至开始了,更不用说产生结果了:它基于未定义的 $location。即使您添加了缺少的“s”,它也是
$location.count。以及为什么制作一个二维数组就是你所看到的,是[1]值。一个维度就足够了。另外,您的问题的标题有错字。 -
显然这是“$locations”的拼写错误...已更新。谢谢。编辑:二维数组的原因是因为我在完整代码的另一部分使用了 [0] 值,这只是一个 sn-p,但不管我的问题得到了回答。
标签: arrays windows powershell cmd active-directory