Author Posts

November 12, 2017 at 3:03 am

I'm building a small alternative to Get-ChildItem when working with the file system by using [System.IO]. I'm trying to find a way to use $PSBoundParameters.containskey in a more elegant way. The code I have below doesn't really seem like the best way to be using it when working with multiple parameters that can all be on at the same time. I've seen examples using the add method and splatting but that was with Powershell cmdlets and not the underlying dot net. Any suggestions would be greatly appreciated.

function Get-EnumeratedItem {

    [CmdletBinding()]
    Param (

        [Parameter(Mandatory=$true,
                   ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true)]
        [Alias("FilePath")]
        [string[]]$Path,

        [string]$Filter,

        [switch]$Recurse
    )
    
    Begin {

        #Converts relative path to absolute path
        $path = Convert-Path $path
    }

    Process {
        
        if (-not $PSBoundParameters.ContainsKey("Filter")) {
            
            if (-not $PSBoundParameters.ContainsKey("Recurse")) {
            
                Write-Verbose "Pulling folders in $path"
                $folders = [System.IO.Directory]::EnumerateDirectories($path, '*', 'TopDirectoryOnly')
            } else {
            
                Write-Verbose "Recursively pulling all folders in $path"
                $folders = [System.IO.Directory]::EnumerateDirectories($path, '*', 'AllDirectories')
            }
        } else {
            
            if (-not $PSBoundParameters.ContainsKey("Recurse")) {
            
                Write-Verbose "Pulling folders in $path"
                $folders = [System.IO.Directory]::EnumerateDirectories($path, "*$Filter*", 'TopDirectoryOnly')
            } else {
            
                Write-Verbose "Recursively pulling all folders in $path"
                $folders = [System.IO.Directory]::EnumerateDirectories($path, "*$Filter*", 'AllDirectories')
            }
        }

        foreach ($folder in $folders) {

            #more code
        }
    }

    End {

    }
}

November 12, 2017 at 4:31 am

Maybe using Switch instead of nested if else?

function Get-EnumeratedItem {

    [CmdletBinding()]
    Param (

        [Parameter(Mandatory=$true,
                   ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true)]
        [Alias("FilePath")]
        [string[]]$Path,

        [string]$Filter,

        [switch]$Recurse
    )
    
    Begin {

        #Converts relative path to absolute path
        $path = Convert-Path $path
    }

    Process {
        Switch ($PSBoundParameters) {
         {$_.ContainsKey("Filter") -and $_.ContainsKey("Recurse")}        {
                                                                           Write-Verbose "Recursively pulling folders in $path"
                                                                           #$folders = [System.IO.Directory]::EnumerateDirectories($path, "*$Filter*", 'AllDirectories')
                                                                          }
         {$_.ContainsKey("Filter") -and (-Not $_.ContainsKey("Recurse"))} {
                                                                           Write-Verbose "Pulling folders in $path"
                                                                           #$folders = [System.IO.Directory]::EnumerateDirectories($path, "*$Filter*", 'TopDirectoryOnly')
                                                                          }
         {(-Not $_.ContainsKey("Filter")) -and $_.ContainsKey("Recurse")} {
                                                                           Write-Verbose "Recursively pulling all folders in $path"
                                                                           #$folders = [System.IO.Directory]::EnumerateDirectories($path, '*', 'AllDirectories')
                                                                          }
         Default                                                          {
                                                                           Write-Verbose "Pulling all folders in $path"
                                                                           #$folders = [System.IO.Directory]::EnumerateDirectories($path, '*', 'TopDirectoryOnly')
                                                                          }
        }

        foreach ($folder in $folders) {
            #more code
        }
    }


    End {

    }
}