我正在尝试创建一个powershell脚本,它将检查文件夹中的3个特定文件。如果这3个文件存在,则继续。
我一直在尝试使用测试路径命令。我得到的最接近的是:
$checkwim = test-path $imagepath\* -include OS.wim, data.wim, backup.wim.但是,如果找到了3中的任何一个,它将返回"True“,这对我不起作用。我要确保这三个人都存在。
我用下面的方法完成了它的工作,但我希望有一个更简单的、更短的方法。
$checkwimos = test-path $imagepath\* -include OS.wim
$checkwimdata = test-path $imagepath\* -include Data.wim
$checkwimonline = test-path $imagepath\* -include Online.wim
if (($checkwimos -ne $True) -or ($checkwimdata -ne $True) -or ($checkwimonline -ne $True))
{
Echo "WIM file(s) not located. Script Aborting"
exit
}有更简单的方法吗?
发布于 2014-09-03 21:24:19
另一种策略:
$files = @('os.wim','data.wim','backup.wim')
if (($files | foreach {test-path $imagepath\$_}) -contains $false)
{
Echo "WIM file(s) not located. Script Aborting"
exit
}发布于 2014-09-03 20:28:31
如果您想避免对每种文件类型进行硬编码,那么可以这样做:
$files = @('os.wim','data.wim','backup.wim')
$checkWim = $files | foreach-object {test-path $imagepath\* -Include $_} | Where-Object {$_ -eq $false}
If($checkWim -eq $false){"WIM file(s) not located. Script Aborting"}
else{
#do stuff
}您还可以导入文件列表,而不是创建数组。
发布于 2014-09-03 19:57:07
您的方式已经相当简单了,尽管您可以通过以下方法使其更易读:
$checkwimos = test-path (Join-Path $imagepath OS.wim)
$checkwimdata = test-path (Join-Path $imagepath Data.wim)
$checkwimonline = test-path (Join-Path $imagepath Online.wim)
if (-not ($checkwimos -and $checkwimdata -and $checkwimonline))
{
Echo "WIM file(s) not located. Script Aborting"
exit
}https://stackoverflow.com/questions/25650411
复制相似问题