115 lines
4.7 KiB
PowerShell
115 lines
4.7 KiB
PowerShell
function Show-CommandDefinition {
|
|
<#
|
|
.SYNOPSIS
|
|
Writes to screen the definition for a cmdlet, function, etc.
|
|
|
|
.PARAMETER Command
|
|
[string] Mandatory. Specifies what command's definition will be written.
|
|
|
|
.PARAMETER Clipboard
|
|
[switch] Optional. If used, also copy command information to clipboard.
|
|
|
|
.PARAMETER Detail
|
|
[switch] Optional. If used, provides additional info about command.
|
|
#>
|
|
[CmdletBinding()]
|
|
[OutputType([string[]])]
|
|
param (
|
|
[Parameter(Mandatory = $true)]
|
|
[ValidateNotNullOrEmpty()]
|
|
[ValidateCount(1,1)]
|
|
[ValidatePattern('[A-Za-z0-9\-\.\s\\:]')]
|
|
[string[]]$Command,
|
|
[Parameter(Mandatory = $false)]
|
|
[switch]$Clipboard,
|
|
[Parameter(Mandatory = $false)]
|
|
[switch]$Detail
|
|
)
|
|
|
|
|
|
$fullOutput="" # Full output
|
|
$outputHeader="" # Command details
|
|
$outputBody="" # Command text
|
|
|
|
# A basic check to make sure we're not getting more than one command somehow.
|
|
if ( (get-command $Command -ErrorAction SilentlyContinue).count -gt 1 ) {
|
|
# Passed in a wildcard or otherwise returning multiple commands. This will not work.
|
|
Write-Error "Error: This cmdlet only accepts single command arguments with no wildcards."
|
|
} else {
|
|
try {
|
|
$WarningPreference="SilentlyContinue"
|
|
# Get all command info
|
|
$commandInfo=(Get-Command $Command)
|
|
|
|
# Get the specific command info for header and command content
|
|
$outputHeader+="Command Name: $($commandInfo.Name)`n"
|
|
$outputHeader+="Command Type: $($commandInfo.CommandType)`n"
|
|
if ( $commandInfo.CommandType -eq "Alias" ) {
|
|
# Do other weird stuff
|
|
$outputHeader+="Resolved Command: $($commandInfo.ResolvedCommand)`n"
|
|
# Reset $commandInfo values to resolved command info
|
|
$commandInfo=(Get-Command $commandInfo.ResolvedCommand)
|
|
$outputHeader+="Command Source: $($commandInfo.Source)`n"
|
|
$outputHeader+="Version: $($commandInfo.Version)`n"
|
|
# Get the alias's command definition
|
|
$outputBody=$commandInfo.Definition
|
|
$fullOutput=$outputHeader,$outputBody
|
|
} elseif ( $commandInfo.CommandType -eq "ExternalScript" -or $commandInfo.CommandType -eq "Application" ) {
|
|
$outputHeader+="Command Path: $($commandInfo.Path)`n"
|
|
# Get the script/app definition
|
|
$outputBody=$commandInfo.Definition
|
|
if ( $commandInfo.CommandType -eq "Application" ) {
|
|
$outputHeader+="Product Name: $($commandInfo.FileVersionInfo.ProductName)`n"
|
|
$outputHeader+="Version: $($commandInfo.Version)`n"
|
|
$fullOutput=$outputHeader
|
|
} else {
|
|
$fullOutput=$outputHeader,$outputBody
|
|
}
|
|
} elseif ( $commandInfo.CommandType -eq "Function" -or $commandInfo.CommandType -eq "Cmdlet" ) {
|
|
$outputHeader+="Command Source: $($commandInfo.Source)`n"
|
|
$outputHeader+="Version: $($commandInfo.Version)`n"
|
|
# Get function/cmdlet definition
|
|
$outputBody=$commandInfo.Definition
|
|
$fullOutput=$outputHeader,$outputBody
|
|
} else {
|
|
# It's some other thing, a Filter, workflow, something.
|
|
if ( $null -ne $commandInfo.Path ) {
|
|
$outputHeader+="Command Path: $($commandInfo.Path)`n"
|
|
}
|
|
if ( $null -ne $commandInfo.Source) {
|
|
$outputHeader+="Command Source: $($commandInfo.Source)`n"
|
|
}
|
|
if ( $null -ne $commandInfo.Version ) {
|
|
$outputHeader+="Command Version: $($commandInfo.Version)`n"
|
|
}
|
|
# Get the definition
|
|
$outputBody=$commandInfo.Definition
|
|
$fullOutput=$outputHeader,$outputBody
|
|
}
|
|
|
|
if ( $Clipboard ) {
|
|
# Clear the clipboard, output info to clipboard
|
|
$null | Set-Clipboard
|
|
|
|
if ( $Detail ) {
|
|
$fullOutput | Set-Clipboard
|
|
$fullOutput
|
|
} else {
|
|
$outputBody | Set-Clipboard
|
|
$outputBody
|
|
}
|
|
} else {
|
|
# Just write to screen
|
|
if ( $Detail ) {
|
|
$fullOutput
|
|
} else {
|
|
$outputBody
|
|
}
|
|
}
|
|
} catch {
|
|
Write-Error "Error getting command definition: $($_.exception.message)"
|
|
} finally {
|
|
$WarningPreference="Continue"
|
|
}
|
|
}
|
|
} |