1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
<#
.SYNOPSIS
Writes verbose information about the invocation being entered.
.DESCRIPTION
Used to trace verbose information when entering a function/script. Writes an entering message followed by a short description of the invocation. Additionally each bound parameter and unbound argument is also traced.
.PARAMETER Parameter
Wildcard pattern to control which bound parameters are traced.
#>
function Trace-EnteringInvocation {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[System.Management.Automation.InvocationInfo]$InvocationInfo,
[string[]]$Parameter = '*')
Write-Verbose "Entering $(Get-InvocationDescription $InvocationInfo)."
$OFS = ", "
if ($InvocationInfo.BoundParameters.Count -and $Parameter.Count) {
if ($Parameter.Count -eq 1 -and $Parameter[0] -eq '*') {
# Trace all parameters.
foreach ($key in $InvocationInfo.BoundParameters.Keys) {
Write-Verbose " $($key): '$($InvocationInfo.BoundParameters[$key])'"
}
} else {
# Trace matching parameters.
foreach ($key in $InvocationInfo.BoundParameters.Keys) {
foreach ($p in $Parameter) {
if ($key -like $p) {
Write-Verbose " $($key): '$($InvocationInfo.BoundParameters[$key])'"
break
}
}
}
}
}
# Trace all unbound arguments.
if (@($InvocationInfo.UnboundArguments).Count) {
for ($i = 0 ; $i -lt $InvocationInfo.UnboundArguments.Count ; $i++) {
Write-Verbose " args[$i]: '$($InvocationInfo.UnboundArguments[$i])'"
}
}
}
<#
.SYNOPSIS
Writes verbose information about the invocation being left.
.DESCRIPTION
Used to trace verbose information when leaving a function/script. Writes a leaving message followed by a short description of the invocation.
#>
function Trace-LeavingInvocation {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[System.Management.Automation.InvocationInfo]$InvocationInfo)
Write-Verbose "Leaving $(Get-InvocationDescription $InvocationInfo)."
}
<#
.SYNOPSIS
Writes verbose information about paths.
.DESCRIPTION
Writes verbose information about the paths. The paths are sorted and a the common root is written only once, followed by each relative path.
.PARAMETER PassThru
Indicates whether to return the sorted paths.
#>
function Trace-Path {
[CmdletBinding()]
param(
[string[]]$Path,
[switch]$PassThru)
if ($Path.Count -eq 0) {
Write-Verbose "No paths."
if ($PassThru) {
$Path
}
} elseif ($Path.Count -eq 1) {
Write-Verbose "Path: $($Path[0])"
if ($PassThru) {
$Path
}
} else {
# Find the greatest common root.
$sorted = $Path | Sort-Object
$firstPath = $sorted[0].ToCharArray()
$lastPath = $sorted[-1].ToCharArray()
$commonEndIndex = 0
$j = if ($firstPath.Length -lt $lastPath.Length) { $firstPath.Length } else { $lastPath.Length }
for ($i = 0 ; $i -lt $j ; $i++) {
if ($firstPath[$i] -eq $lastPath[$i]) {
if ($firstPath[$i] -eq '\') {
$commonEndIndex = $i
}
} else {
break
}
}
if ($commonEndIndex -eq 0) {
# No common root.
Write-Verbose "Paths:"
foreach ($p in $sorted) {
Write-Verbose " $p"
}
} else {
Write-Verbose "Paths: $($Path[0].Substring(0, $commonEndIndex + 1))"
foreach ($p in $sorted) {
Write-Verbose " $($p.Substring($commonEndIndex + 1))"
}
}
if ($PassThru) {
$sorted
}
}
}
########################################
# Private functions.
########################################
function Get-InvocationDescription {
[CmdletBinding()]
param([System.Management.Automation.InvocationInfo]$InvocationInfo)
if ($InvocationInfo.MyCommand.Path) {
$InvocationInfo.MyCommand.Path
} elseif ($InvocationInfo.MyCommand.Name) {
$InvocationInfo.MyCommand.Name
} else {
$InvocationInfo.MyCommand.CommandType
}
}
|