Powershell 如何获取目录列表或';无';?
使用PowerShell,我想检查一个目录(在Powershell 如何获取目录列表或';无';?,powershell,powershell-2.0,Powershell,Powershell 2.0,使用PowerShell,我想检查一个目录(在$PathOutput中的全名)是否包含其他目录。如果此路径不包含其他目录,我希望变量$FailedTests具有字符串“none”,否则变量$FailedTests应包含找到的每个目录(非递归),或者在不同的行中,或者逗号分隔,或者其他任何内容 我尝试了以下代码: $DirectoryInfo = Get-ChildItem $PathOutput | Measure-Object if ($directoryInfo.Count -eq 0) {
$PathOutput
中的全名)是否包含其他目录。如果此路径不包含其他目录,我希望变量$FailedTests
具有字符串“none”,否则变量$FailedTests
应包含找到的每个目录(非递归),或者在不同的行中,或者逗号分隔,或者其他任何内容
我尝试了以下代码:
$DirectoryInfo = Get-ChildItem $PathOutput | Measure-Object
if ($directoryInfo.Count -eq 0)
{
$FailedTests = "none"
} else {
$FailedTests = Get-ChildItem $PathOutput -Name -Attributes D | Measure-Object
}
但它会产生以下错误:
Get-ChildItem : A parameter cannot be found that matches parameter name 'attributes'.
At D:\Testing\Data\Powershell\LoadRunner\LRmain.ps1:52 char:62
+ $FailedTests = Get-ChildItem $PathOutput -Name -Attributes <<<< D | Measure-Object
+ CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
Get ChildItem:找不到与参数名称“attributes”匹配的参数。
位于D:\Testing\Data\Powershell\LoadRunner\LRmain.ps1:52 char:62
+$FailedTests=Get ChildItem$PathOutput-Name-Attributes您可以这样做吗?这样,您也不必两次获取子项
$PathOutput = "C:\Users\David\Documents"
$childitem = Get-ChildItem $PathOutput | ?{ $_.PSIsContainer } | select fullname, name
if ($childitem.count -eq 0)
{
$FailedTests = "none"
}
else
{
$FailedTests = $childitem
}
$FailedTests
该错误实际上是不言自明的:
Get ChildItem
(在PowerShell v2中)没有参数-Attributes
。该参数(以及参数-Directory
)是随PowerShell v3添加的。在PowerShell v2中,您需要使用Where Object
过滤器来删除不需要的结果,例如:
$DirectoryInfo = Get-ChildItem $PathOutput | Where-Object {
$_.Attributes -band [IO.FileAttributes]::Directory
}
$DirectoryInfo = Get-ChildItem $PathOutput | Where-Object {
$_.GetType() -eq [IO.DirectoryInfo]
}
$DirectoryInfo = Get-ChildItem $PathOutput | Where-Object { $_.PSIsContainer }
if ($DirectoryInfo) {
$DirectoryInfo | Select-Object -Expand FullName
} else {
'none'
}
或者像这样:
$DirectoryInfo = Get-ChildItem $PathOutput | Where-Object {
$_.Attributes -band [IO.FileAttributes]::Directory
}
$DirectoryInfo = Get-ChildItem $PathOutput | Where-Object {
$_.GetType() -eq [IO.DirectoryInfo]
}
$DirectoryInfo = Get-ChildItem $PathOutput | Where-Object { $_.PSIsContainer }
if ($DirectoryInfo) {
$DirectoryInfo | Select-Object -Expand FullName
} else {
'none'
}
或者(更好)像这样:
$DirectoryInfo = Get-ChildItem $PathOutput | Where-Object {
$_.Attributes -band [IO.FileAttributes]::Directory
}
$DirectoryInfo = Get-ChildItem $PathOutput | Where-Object {
$_.GetType() -eq [IO.DirectoryInfo]
}
$DirectoryInfo = Get-ChildItem $PathOutput | Where-Object { $_.PSIsContainer }
if ($DirectoryInfo) {
$DirectoryInfo | Select-Object -Expand FullName
} else {
'none'
}
您可以输出文件夹列表,如果没有,则输出“无”,如下所示:
$DirectoryInfo = Get-ChildItem $PathOutput | Where-Object {
$_.Attributes -band [IO.FileAttributes]::Directory
}
$DirectoryInfo = Get-ChildItem $PathOutput | Where-Object {
$_.GetType() -eq [IO.DirectoryInfo]
}
$DirectoryInfo = Get-ChildItem $PathOutput | Where-Object { $_.PSIsContainer }
if ($DirectoryInfo) {
$DirectoryInfo | Select-Object -Expand FullName
} else {
'none'
}
因为空结果(
$null
)是。似乎不起作用。如果所讨论的目录不包含任何内容,则仍然使用If语句的“else”部分$在这种情况下,失败的测试是空的…非常感谢,这正是我所需要的,它也在工作!!