在我的函数中,我没有3个没有必填项,也没有位置参数Folder1, Folder2 and Folder3,在运行我的函数之前,我想检查是否存在每个选定的参数文件夹。它必须是动态的,因为用户可以随机指定一个或两个或所有树文件夹。提前致谢。
您可以将数组传递给Test-Path
Test-Path c:, d:, e:
True
True
True
Run Code Online (Sandbox Code Playgroud)
编辑
因此,从您的评论中我相信您会有这样的事情:
$mypaths = "c:","d:",$null
Test-Path $mypaths
Run Code Online (Sandbox Code Playgroud)
哪个发出以下错误:
Test-Path : Cannot bind argument to parameter 'Path' because it is null.
Run Code Online (Sandbox Code Playgroud)
然后,您可以尝试以下操作:
$mypaths = "c:","d:",$null
$mypaths | Foreach { if ($_){Test-Path $_}}
Run Code Online (Sandbox Code Playgroud)
结果只有两个有效路径。
因此,您可以考虑检查返回的值:
$valid = $mypaths | Foreach { if ($_){Test-Path $_}}
write-host "Variable `$mypaths contains $($mypaths.count) paths, and $($valid.count) were found valid"
Run Code Online (Sandbox Code Playgroud)
哪个输出:
Variable $mypaths contains 3 paths, and 2 were found valid
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3913 次 |
| 最近记录: |