One of the things that make PowerShell soo powerful is that you have access to .Net Framework and all the objects that it includes. Quite often I use the Get-Member (gm) CmdLet to find out what Methods and Properties that can be used, however one of the methods on the object isn't exposed, and that's the constructor.
The constructor(s) is the method that is used when creating the object. Information about the constructor is interresting when using the New-Object (new) CmdLet.
Lets say that you want to know what arguments you could/should/must use when creating a System.DateTime, then you can use the following line:
1# ([datetime]).GetConstructors() | foreach { $_.ToString() }
Void .ctor(Int64)
Void .ctor(Int64, System.DateTimeKind)
Void .ctor(Int32, Int32, Int32)
Void .ctor(Int32, Int32, Int32, System.Globalization.Calendar)
Void .ctor(Int32, Int32, Int32, Int32, Int32, Int32)
Void .ctor(Int32, Int32, Int32, Int32, Int32, Int32, System.DateTimeKind)
Void .ctor(Int32, Int32, Int32, Int32, Int32, Int32, System.Globalization.Calendar)
Void .ctor(Int32, Int32, Int32, Int32, Int32, Int32, Int32)
Void .ctor(Int32, Int32, Int32, Int32, Int32, Int32, Int32, System.DateTimeKind)
Void .ctor(Int32, Int32, Int32, Int32, Int32, Int32, Int32, System.Globalization.Calendar)
Void .ctor(Int32, Int32, Int32, Int32, Int32, Int32, Int32, System.Globalization.Calendar, System.DateTimeKind)
This gives you good information about the types you need to use, but it would be useful to see the names of the arguments too.
The following function shows the constructors with the parameternames. I've tried to make look a little more PowerShellish than the example above.
function Get-Constructor ([type]$type) {
$type.getconstructors() |
foreach -process {
$tmp = "[void] .ctor" +
($_.GetParameters() |
foreach -begin {$s=@()} `
-process {$s+="[$($_.ParameterType.ToString().ToLower())] `$$($_.Name)"} `
-end {"(" + [string]::Join(', ',$s) + ")"})
$tmp -replace "Boolean","bool" `
-replace "Int32", "int" `
-replace "System.IO." `
-replace "System.Text." `
-replace "System." `
-replace "System.String", "string"
}
}
And the result looks like this:
59# get-constructor datetime
[void] .ctor([int64] $ticks)
[void] .ctor([int64] $ticks, [datetimekind] $kind)
[void] .ctor([int] $year, [int] $month, [int] $day)
[void] .ctor([int] $year, [int] $month, [int] $day, [globalization.calendar] $calendar)
[void] .ctor([int] $year, [int] $month, [int] $day, [int] $hour, [int] $minute, [int] $second)
[void] .ctor([int] $year, [int] $month, [int] $day, [int] $hour, [int] $minute, [int] $second, [datetimekind] $kind)
[void] .ctor([int] $year, [int] $month, [int] $day, [int] $hour, [int] $minute, [int] $second, [globalization.calendar] $calendar)
[void] .ctor([int] $year, [int] $month, [int] $day, [int] $hour, [int] $minute, [int] $second, [int] $millisecond)
[void] .ctor([int] $year, [int] $month, [int] $day, [int] $hour, [int] $minute, [int] $second, [int] $millisecond, [datetimekind] $kind)
[void] .ctor([int] $year, [int] $month, [int] $day, [int] $hour, [int] $minute, [int] $second, [int] $millisecond, [globalization.calendar] $calendar)
[void] .ctor([int] $year, [int] $month, [int] $day, [int] $hour, [int] $minute, [int] $second, [int] $millisecond, [globalization.calendar] $calendar, [datetimekind] $kind)
Posted
07-24-2007 22:58
by
bjorn.osterman