Need a more elegant use of $PSBoundParameters

Welcome Forums General PowerShell Q&A Need a more elegant use of $PSBoundParameters

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
1 year ago.

  • Author
    Posts
  • #84005

    Participant
    Points: 0
    Rank: Member

    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 {
    
        }
    }
    
  • #84011

    Participant
    Points: 0
    Rank: Member

    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 {
    
        }
    }

The topic ‘Need a more elegant use of $PSBoundParameters’ is closed to new replies.