Working with PowerShell sometimes (or a lot of the time) involves using .Net types to create objects that can be used for more in depth scripting of various items. It is one thing to know the type that you need for the task at hand, but another all together to use that type and build an object that can be used for whatever purpose. Sometimes it is as simple as just doing the following:
New-Object Net.Sockets.TCPClient
But did you know that there are other parameters that can be used with this type? You can actually specify a system name and port number as an argument to the object creation that will actually perform a port query.
New-Object Net.Sockets.TCPClient -ArgumentList dc1.rivendell.com,389
You can see that not only did it create the TCPClient object, but it already attempted a port connection to the server. Pretty cool stuff!
So how do you figure out what to use when creating an object? Well, you can either go out to the MSDN site and search for that specific type and look at the various constructors available, or you can dig a little in the .Net object and find out yourself!
([type]"Net.Sockets.TCPClient").GetConstructors() | ForEach { $_.GetParameters() } | Select Name,ParameterType
Ok, so it still isn’t that clear at to what parameters are required for each available constructor for this type. But at least you now know more than before. Lets take it another step forward and actually see what each constructor has available.
([type]"Net.Sockets.TCPClient").GetConstructors() | ForEach { ($_.GetParameters() | ForEach {$_.ToString()}) -Join ", " }
That’s a little better. At least we can now more easily see the parameters and respective parameter types that are required for the constructor. The initial blank line means that this constructor also accepts no parameters to create the object.
To make this a little easier, I use a function I wrote called… Get-Constructor. Using this, I am able to quickly get all available constructors either written out as a simple string, or as an object if I wish to dive deeper into the .Net object. The download for this function is available below in the Script Repository.
A couple examples of Get-Constructor in action:
Display as a simple string
Get-Constructor -Type Net.Sockets.TCPClient
As an object
Get-Constructor -Type Net.Sockets.TCPClient -AsObject
A couple more just for fun
Get-Constructor -Type string
Get-Constructor -Type adsisearcher
That is all there is to it! Feel free to give it a download and let me know what you think!
Joel, your Get-Constructor code on poshcode is brilliant! I borrowed it a few years ago when putting together notes about how to use DLLs in Powershell. My notes are published here and do reference a poshcode link, although not 3993.
https://www.facebook.com/notes/allan-carhart/using-net-dlls-from-powershell/821898741197827
One of the best and collest PowerShell vs. .NET Tips!
Thank you for that Boe!
Peter Kriegel
http://www.admin-source.de
my reflection module (available on poshcode.org) has a get-constructor which even includes the PowerShell syntax for invoking the constructors (including how to cast arrays as parameters, etc.), you should check it out 😉
That is very cool! Yet another amazing thing that you have put together!
For anyone checking out this article, the most current version of Joel’s module is at http://poshcode.org/3993