我正在寻找一些帮助,在PowerShell中编写代码以检查和报告。
我想C:MyDataMyCustomerNames
搜索文件夹MyTests
、MyProducts
和MyReturns
,然后报告C:MyDataMyCustomerNamesMyTests is Present
或C:MyDataMyCustomerNamesMyProducts is Missing
。
我知道代码Test-Path C:MyDataMyCustomerNamesMyProducts
可以工作,但我也想测试*.xml
、*.docx
等。 另外,路径中唯一是变量的部分是 MyCustomerNames
.
如果你能指出一些可行的东西,那就太棒了,或者提供一个比我提供的更能说明的例子。
请,谢谢!
打柳的建议。将位置保存在数组中,然后使用 foreach
语句对数组中的每个值运行循环。您可以在 if
语句中使用Get-ChildItem
来搜索这些目录并评估它们是否存在。
$folders = "MyTests", "MyProducts", "MyReturns"
foreach ($folder in $folders) {
if (Get-ChildItem -Path "C:MyDataMyCustomerNames" -Filter $folder -Directory) {
"$folder exists"
} else {
"$folder does not exist"
}
}