문제

PowerShell을 사용하면 다음 명령을 사용하여 디렉토리를 가져올 수 있습니다.

Get-ChildItem -Path $path -Include "obj" -Recurse | `
    Where-Object { $_.PSIsContainer }
.

명령이 더 읽기 쉽기 때문에 기능을 작성하는 것을 선호합니다.예 :

Get-Directories -Path "Projects" -Include "obj" -Recurse
.

및 다음 함수는 PROMENACODICETAG 코드를 우아하게 처리하는 것을 제외하고는 정확히 수행합니다.

Function Get-Directories([string] $path, [string] $include, [boolean] $recurse)
{
    if ($recurse)
    {
        Get-ChildItem -Path $path -Include $include -Recurse | `
            Where-Object { $_.PSIsContainer }
    }
    else
    {
        Get-ChildItem -Path $path -Include $include | `
            Where-Object { $_.PSIsContainer }
    }
}
.

GET-Directory 함수에서 -Recurse 문을 어떻게 제거 할 수 있습니까? 아니면이 작업을 수행하는 더 좋은 방법입니까?

도움이 되었습니까?

해결책

시도 :

# nouns should be singular unless results are guaranteed to be plural.
# arguments have been changed to match cmdlet parameter types
Function Get-Directory([string[]]$path, [string[]]$include, [switch]$recurse) 
{ 
    Get-ChildItem -Path $path -Include $include -Recurse:$recurse | `
         Where-Object { $_.PSIsContainer } 
} 
.

- -Recurse : $ false는 동일하지 않은 것과 동일하지 않은 것으로 나타납니다.

다른 팁

PowerShell 3.0에서는 -File -Directory 스위치로 구워졌습니다.

dir -Directory #List only directories
dir -File #List only files
.

답변 오이신이 지시가됩니다.나는 이것이 프록시 기능이되고 싶지 않으려는 것에 가깝게 스커트가있는 것을 추가하고 싶었습니다. PowerShell 커뮤니티 확장 2.0이 설치되어 있으면 이미이 프록시 기능이 있습니다.이를 활성화해야합니다 (기본적으로 사용 중지됨).pscx.userpreferences.ps1 파일을 편집 하고이 줄을 변경하여 아래 그림과 같이 $ true로 설정하십시오.

GetChildItem = $true # Adds ContainerOnly and LeafOnly parameters 
                     # but doesn't handle dynamic params yet.
.

동적 매개 변수와 관련된 제한 사항을 유의하십시오.이제 PSCX를 가져올 때 다음과 같이하십시오 :

Import-Module Pscx -Arg [path to Pscx.UserPreferences.ps1]
.

이제이 작업을 수행 할 수 있습니다.

Get-ChildItem . -r Bin -ContainerOnly
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top