Skip to content

Instantly share code, notes, and snippets.

@Jaykul
Created July 27, 2017 23:44
Show Gist options
  • Save Jaykul/23c2332e6a512f6ec9656be4d547015b to your computer and use it in GitHub Desktop.
Save Jaykul/23c2332e6a512f6ec9656be4d547015b to your computer and use it in GitHub Desktop.
Something I need to add to the Information Module
[CmdletBinding()]
param(
[System.Management.Automation.CommandMetadata]
$Command
)
begin {
$ProxyFactory = [System.Management.Automation.ProxyCommand]
<#
class TraceInformation {
[String]$Message
[Hashtable]$BoundParameters
TraceInformation([string]$Message, [Hashtable]$BoundParameters) {
$this.Message = $Message
$this.BoundParameters = $BoundParameters
}
[string] ToString() {
return $this.Message + " " + $($(
foreach($param in $this.BoundParameters.GetEnumerator()) {
"-{0}:{1}" -f $param.Key, $($param.Value -join ", ")
}
) -join " ")
}
}
#>
}
process {
$CommandName = $Command.Name
if($Help = $Command | Get-Help) {
$HelpComments = $ProxyFactory::GetHelpComments(@($Help)[0])
}
$CmdletBindingAttribute = $ProxyFactory::GetCmdletBindingAttribute($Command)
$ParamBlock = $ProxyFactory::GetParamBlock($Command)
$Begin = $ProxyFactory::GetBegin($Command)
$Process = $ProxyFactory::GetProcess($Command)
$End = $ProxyFactory::GetEnd($Command)
if($DynamicParam = $ProxyFactory::GetDynamicParam($Command)) {
$DynamicParam = "dynamicparam {`n$DynamicParam}"
}
Invoke-Expression @"
function global:$CommandName {
$CmdletBindingAttribute
param(
$ParamBlock
)
$DynamicParam
begin {
[TraceInformation]::new("BEGIN $CommandName", `$PSBoundParameters) | Write-Info -Tag Trace, Enter, Begin
$Begin
}
process {
[TraceInformation]::new("PROCESS $CommandName", `$PSBoundParameters) | Write-Info -Tag Trace, Enter, Process
$Process
}
end {
[TraceInformation]::new("END $CommandName", `$PSBoundParameters) | Write-Info -Tag Trace, Leave, End
$End
}
<#
.ForwardHelpTargetName $($Command.ModuleName)\$CommandName
.ForwardHelpCategory $($Command.Commandtime)
#>
}
"@
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment