Skip to main content

Find all PowerShell Commands with Specific Parameters

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

Popular posts from this blog

With the AD Recycle Bin Turned on, What Happens when you Create a User Account with a Password that does not meet the Password Policy?

This was an interesting observation from one of my Windows Server 2012 classes.  While working with the AD Recycle bin in a lab, one of my students discovered some interesting accounts that were created. When he created user accounts that did not meet password complexity requirements, an account is temporarily made and then deleted.  When a new password is provided that meets the password requirements, then a new account is made. We discovered this in two places.  First off in the Active Directory Administrative Center.  This is what caused the initial confusion.  Take a look.  This is in the Deleted Objects OU. You can see multiple deleted accounts for Test2 and one for Test3.  Test3 is a valid, functioning user account.  Using the PowerShell command Get-ADObject –IncludeDeletedObjects –Filter * –Properties ObjectSID we can see that indeed, two accounts were created, with one of them deleted. Notice the RID portion of the SID is different. ...

Sticky Key problem between Windows Server 2012 and LogMeIn

This week I instructed my first class using Windows Server 2012 accessed via LogMeIn and discovered a Sticky Key problem every time you press the Shift key. Here is my solution to resolve this.  First off, in the Preferences of LogMeIn for the connection to the Windows Server, click General . Change the Keyboard and mouse priority to Host side user and click Apply at the bottom. On the Windows 2012 server, open the Control Panel – Ease of Access – Change how your keyboard works . Uncheck Turn on Sticky Keys . Click Set up Sticky Keys . Uncheck Turn on Sticky Keys when SHIFT is pressed five times . Click OK twice. If you are using Windows Server 2012 as a Hyper-V host, you will need to redo the Easy of Use settings on each guest operating system in order to avoid the Sticky Key Problem. Updated Information: March 20, 2013 If you continue to have problems, Uncheck Turn on Filter Keys .

Backup and Restore AD LDS with DSDBUTIL.exe

Active Directory Lightweight Directory Services allow you to create a directory service that allows applications to have access to user accounts, groups, and authentication similar to Active Directory Domain Services.  The big advantage here is that the schema of the directory service will not be bound by the rules of an Active Directory database.  Exchange 2007/2010, for example, use an instance of AD LDS on the Edge Transport Server to provide for user authentication from the internet.  Because your Active Directory database is not exposed to the internet, this is more secure. Applications will handle most of the dirty work should they require AD LDS.  You may want to make sure the database is being backed up and also have a restore plan in place.  Should the database become corrupt, the application that uses that database will fail.  This document will walk you through backing up and restoring an instance of AD LDS using the dsdbutil.exe command. Fi...