Skip to content

Instantly share code, notes, and snippets.

@gaelcolas
Created April 20, 2016 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaelcolas/9667d56714f3254cb2ebd5dc76b1956d to your computer and use it in GitHub Desktop.
Save gaelcolas/9667d56714f3254cb2ebd5dc76b1956d to your computer and use it in GitHub Desktop.
. .\lib.common\MethodHelpers.ps1
. .\lib.common\PowerObject.ps1
function Resolve-DNSHost {
[cmdletBinding()]
Param ()
DynamicParam {
Get-DynamicParamForMethod -method ([System.Net.Dns]::Resolve)
}
process {
try
{
Invoke-MethodOverloadFromBoundParam -method ([System.Net.Dns]::Resolve) -parameterSet $PSCmdlet.ParameterSetName -Parameters $PSBoundParameters
}
catch
{
Write-Warning "Error occured: $_"
throw $_
}
}
}
@gaelcolas
Copy link
Author

This is an example to use my quick hack MethodHelpers.ps1 to wrap .Net methods into PowerShell commandlet with little code.
That also allows to extend the features by adding parameters or processing in the param block or begin/process/end blocks.

There are limitations with some constructors or methods when they have similar constructors and/or use references of objects instead of variable.
The main problem with this implementation (in my opinion, there might be others) is that it relies on the [type]::method.OverloadDefinitions which is a string, that I parse using regex (quite simple and not really validating anything, as I did that for log4net which was pretty consistent).

Feel free to point me to flaws or issues and I may try to improve the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment