My PowerShell class this week had a great idea. While we were utilizing Get-Command to find commands, someone asked if it was possible to find commands based on their parameters. I thought that that would be a good idea. The original code started really simple.
1 2 3 4 5 6 7 8 | Function Find-Parameters { Param( [String]$Parameter ) Get-Command | Where-Object {$_.Parameters.Keys -eq $Parameter} } |
This code was designed to be placed in their profiles for easy use. They simple needed to type Find-Parameters and then the parameter name that they were interested in. Out popped all the commands that contained that parameter.
I decided to have a bit more fun with it. First I turned it into a filtering function that allowed you to search for commands with multiple parameters through the pipeline. The –ExactMatch switch was added to filter out all but the commands that contained all the parameters in the search.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| <# .SYNOPSIS Finds all cmdlets with the specified parameters. .DESCRIPTION Allows you to search all of the cmdlets on a client for cmdlets that contain those parameters .PARAMETER Parameter A list of parameters you want in the returned commands. .PARAMETER ExactMatch Only returns commands that contain all of the provided parameters .EXAMPLE "ComputerName","PassThru" | Find-Parameters -Verbose -ExactMatch
Command ------- Add-ADComputer Remove-Computer Rename-Computer Set-Service
Returns only commands that have "ComputerName" and "PassThru" as parameters. #> Function Find-Parameters { [CmdletBinding()] Param( [parameter(Mandatory=$true, ValueFromPipeline=$true)] [String[]]$Parameter,
[Switch]$ExactMatch ) BEGIN { $Result = $null $FinalResult = $null } # END: BEGIN BLOCK
PROCESS {
# Loop through each command and determine if # it contains one of the parameters. ForEach ($P in $Parameter) { $Result = Get-Command | Where-Object {$_.Parameters.Keys -eq $P} }
If ($ExactMatch -and ($FinalResult -ne $null)) { # If "ExactMatch" is used and "$FinalResult" is not $null, # then Compare what is in $Result with $Final result and # save all instances where there are identical data into # $FinalResult. $CompareHash = @{ 'ReferenceObject' = $Result 'DifferenceObject' = $FinalResult 'ExcludeDifferent' = $True 'IncludeEqual' = $True } $FinalResult = Compare-Object @CompareHash | Select-Object -Property @{'n'= "Command"; 'e'={$_.InputObject}} } ElseIf (($FinalResult -ne $null) -and ($ExactMatch -eq $False)) { # If -ExactMatch is not $true then add the new # results to $FinalResult, and then filter # so all commands are unique to prevent repeated # data. $FinalResult = $FinalResult += $Result | Select-Object -Unique } Else { # If this is the first parameter queried or only one # is provided, save the results in $FinalResult $FinalResult = $Result }
} # END: PROCESS BLOCK
END { # Sort the data and send it to the pipeline. Write-Output ($FinalResult | Sort-Object -Property Name) } # END: END BLOCK
} |
Lines 1-24 is the help file.
Lines 35-39 is the BEGIN block. It initializes our two variables. $Result is the temporary storage and $FinalResult is what will be sent to the pipeline.
Lines 46 – 50 look very similar to the original code. This time multiple parameters can be searched for. The IF Statement from lines 52-81 is where the magic happens.
Let’s start in the ELSE block. It simply writes the results to $FinalResult. This is ran only if 1 parameter is given or the –ExactMatch switch is not used.
The ELSEIF block is used if multiple parameters are being search for, but the user does not care if all of the parameters are present in each command.
The IF block takes each result, and compares it to the first. It allows only commands that are present in both results to continue on. This allows –ExactMatch to work.
Lines 85 – 90 Sorts the data and then sends it to the pipeline.
I like this one so much, I’ve added it to my module of helper functions.
Comments