Powershell Tip: get all Constructor overloads and their signatures

In continuation to the previous post on getting all overload signature of an object’s method, you would notice that we do not get any help regarding the number and overload for an type’s constructor. To view the overloads of a type constructor, below script can be used.

Const1

The above can also be re-written more succinctly using pipelines as shown below..

Const2

the results should look like below..

Const result

This would help you in knowing which constructor to use while creating new objects of a type.

Hope this helps!!

Cheers!!

Advertisements

Powershell Tip: Get all overloaded method signatures

Powershell ISE is great for authoring of Powershell scripts, however it does not provide intellisense for object method parameters.
For example, if you type the below code snippet on ISE, you do not get any help regarding the parameters GetDirectories can take as input. It also does not indicate if there are overloads for the same method with varying parameters and types.
Method Signature

For invoking these method, we browse through MSDN documentation to figure out the type and number of parameters. However, in powershell we can use the below method technique to view all the method signatures.

The idea is to execute the method without the parenthesis. When a method is invoked without parenthesis, powershell would use reflection and output all the method signatures.

Method Signature I

Method results

This would help you in knowing which types and overloads to use while invoking the method.

Hope this helps!!

Cheers!!

POSH Concept : Understanding WildCards

The word wildcard is quite intriguing and potentially comes from the world of Games and gambling where most of the results are unknown or unpredictable. In the game of cards, there are few cards termed as ‘Jokers’ and they represent ‘WildCard’ meaning that they can assume any value. They are amorphous and can assume values depending on the context being used.

Wildcard in programming world is typically used to find and search sequence of characters within a text for which it is not sure or known whether the search sequence of characters exists in target text, if the sequence of characters exists, then how many? Also, It might not be a single exact sequence of characters to find. There could be multiple variations of sequence of characters that are to be found – all at one time. Some of the characters to be searched might not even be known at all while searching.

Wildcards helps in finding solutions to all the above mentioned situations. It helps in searching sequence of characters is almost fuzzy manner.

Every programming language designates few characters in its lexicon as Wildcard characters depending on its grammar. Powershell has its grammar and also supports Wildcard characters. Based on its grammar, Powershell has the following characters that can be used as wildcard characters.

  • ?
  • *
  • []
  • [-]

‘?’ means ‘any single character can be replaced with it’. ‘?’ signifies that any one character is a valid character wherever it appears. For example: in characters ‘W3w?’, ‘?’ can be replaced with any legitimate character. W3wa, w3wp, w3wz, w3w0 would match to the above mentioned wildcard pattern.

‘*’ means ‘zero or more character can be replaced with it’. ‘*’ signifies that any number and any character is a valid to replace wherever it appears. For example: in characters ‘W3w*’, ‘*’ can be replaced with any legitimate character. W3wa, w3wpi, w3wz123 would match to the above mentioned wildcard pattern.

‘[]’ means ‘any one of the specified characters within these brackets can be replaced with it’. ‘[]’ signifies that any one character is a valid to replace wherever it appears. For example: in characters ‘W3w[mnopq]’, ‘[]’ can be replaced with any legitimate character ‘m’, ‘n’, ‘o’, ‘p’, ‘q’. W3wm, w3wn, w3wo, w3wp, w3wq would match to the above mentioned wildcard pattern.

‘[-]’ means ‘any one of the specified characters within the range available in the brackets can be replaced with it’. ‘[-]’ signifies that any one character is a valid to replace wherever it appears from the range. For example: in characters ‘W3w[m-q]’, ‘[-]’ can be replaced with any legitimate character ‘m’, ‘n’, ‘o’, ‘p’, ‘q’. W3wm, w3wn, w3wo, w3wp, w3wq would match to the above mentioned wildcard pattern.

one of the important character in the Powershell grammar for wildcards is the ‘`’ character. This acts as an escape character and is of immense importance while searching for strings that might contain one of the wildcard characters. for example if the search characters include ‘*’ to be searched verbatim, there is no other way to let Powershell know that ‘*’ is not a wildcard character without the usage of ‘`’. ‘`*’ would negate the ‘*’ being as wildcard character and would be searched as a normal character.

One important thing to remember is that not all properties belonging to cmdlets are wildcard savvy meaning that only some properties can be used along with wildcards and this information about which properties can be used for searching using wildcards is available as part of cmdlet parameters description on MSDN.

Lets understand the usage of Wildcards in powershell using example..

We will use a simple cmdlet ‘Get-Service’ for all the examples. Get-Service cmdlet when run, returns all Windows Services available within the operating system.

Example 1 : find all services that start with letters ‘win’

Get-Service -name ‘win*’
The result is all services whose name starts with ‘win’ on my machine

  • WinDefend
  • WinMgmt
  • WinRM
Example 1 : find all services that ends with letters ‘win’

Get-Service -name ‘*svc’
The result is all services whose name ends with ‘svc’ on my machine

  • W3SVC
  • Wcmsvc
  • wcncsvc
  • WdNisSvc
  • Wecsvc
  • WEPHOSTSVC
  • WerSvc
  • WinHttpAutoProxySvc
  • WlanSvc
  • wlidsvc
  • WMPNetworkSvc
  • workfolderssvc
  • WPCSvc
  • wscsvc
  • wudfsvc
  • WwanSvc

I will post some more examples in future posts!! stay tuned..

Cheers!!

DiD you know : You can find all values belonging to an ENUM in powershell

POSH TIP: List all values belonging to an ENUM in powershell

Enum is a value type available in .NET framework. Use GetNames static method of this class to list down all values belonging to and ENUM as shown below

[Enum]::GetNames([System.Management.Automation.ActionPreference])

In the above command System.Management.Automation.ActionPreference is the Enum whose values we are interested in..

The above code return a string array and it looks like below

    SilentlyContinue
    Stop
    Continue
    Inquire
    Ignore
    Suspend

The above code can be used with an .NET Enumeration type.

Another example are below..

[Enum]::GetNames([System.Windows.Forms.MessageBoxButtons])

    OK
    OKCancel
    AbortRetryIgnore
    YesNoCancel
    YesNo
    RetryCancel

DiD you know : You can find the class that implements a particular cmdlet

POSH TIP: Get .Net Class Name that implements a particular cmdlet

Use the Get-Command cmdlet along with the name of the target cmdlet for getting the implementing class like below..

(Get-Command -name Get-Process).ImplementingType.Name

Put the get-command along with name parameter within parenthesis and then use the ‘Name’ property of ‘ImplementingType’ property

The ImplementingType property returns the runtimeType object that actually contains the name of class implementing the cmdlet.

The output looks like below..GetProcessCommand is the class implementing the Get-Process cmdlet

GetProcessCommand