Crescendo
Crescendo copied to clipboard
Reference parameters in the OutputHandler
In my OutputHandler I'm trying to include some basic error handling. I want to be able to include a command in the handler like
Write-Warning "Failed to find a matching entry for $list"
In my Parameters section, the Name is set to "List" but this doesn't seem to work. Is there some other way to reference parameters or does this functionality not exist yet?
we know that the output handlers are going to get worked over a bit, but I'm trouble with what you're seeing. could you provide more of the output handler?
Part of the challenge is that there is little documentation on how all of this works. For example, I'm not sure what gets passed to an output handler or how to reference it. Here's a config I've been testing with.
{
"$schema": "../../Microsoft.PowerShell.Crescendo.Schema.json",
"Verb": "Get",
"Noun": "CmdKey",
"Description": "Get Cmdkey entries from the localhost.",
"OriginalName": "cmdkey",
"Usage": {
"Synopsis": "Get entries from CmdKey"
},
"Aliases": ["gck"],
"Parameters": [
{
"Name": "List",
"OriginalName": "/List:",
"Description": "List cmdkey entries for the specified computer. Wildcards are permitted.",
"ParameterType": "string",
"DefaultValue": "*",
"Aliases": ["cn"],
"ValueFromPipeline": true,
"AdditionalParameterAttributes": [
"[ValidateNotNullorEmpty()]"
]
}
],
"Examples": [
{
"Command": "Get-Cmdkey -list SRV1
Computername Target Type User
------------ ------ ---- ----
PROSPERO srv1 Domain Password company\\artd
",
"Description": "Get the cmdkey entry for SRV1",
"OriginalCommand": "cmdkey /list:srv1"
}
],
"HelpLinks": [
"cmdkey",
"Get-Credential"
],
"OutputHandlers": [
{
"ParameterSetName": "Default",
"Handler" : "Param($item)
$raw = $item | Select-Object -skip 3 | where-object {$_ -match ':'}
if ($raw.count -ge 3) {
For ($i = 0; $i -lt $raw.count;$i+=3) {
$raw[$i..($i+2)] | foreach-object -begin { $h = [ordered]@{PSTypeName='cmdKeyEntry';Computername = [system.environment]::MachineName}} -Process {
$line = $_.split(':')
$h.Add($line[0].Trim(),$line[1].trim())
} -end { New-Object -typename PSObject -property $h }
}
}
else {
Write-Warning 'Failed to find any matching cmdkey entries.'
}"
}
]
}
I'd like to include error handling in the output handler that could use the value from the $List parameter.
On a similar note, I'm also interested i this functionality. I have a command that returns an xml containing a number of lists (which can't be filtered by its own), and I'd really like the opportunity to be able to filter the output directly via the Crescendo command. I imagine this would be done by adding another -Filter parameter (with ApplyToExecutable = $false), but currently I have no idea on how to pass this value to the output handler.
I think this would be good. I believe there's a little bit of inconsistency here. Currently the output of the native command is passed to the handler, but no additional parameters that are available in a handler are used. As an example, here's a handler I have for parsing vagrant box list:
function parseVagrantBox {
[CmdletBinding()]
Param(
[Parameter(Mandatory)]
[Object[]]
$Lines,
[Parameter()]
[String]
$Name
)
process {
$Boxes = [System.Collections.Generic.List[pscustomobject]]::new()
$Lines | Foreach-Object {
$null = $_ -match '(?<name>([\w\/]+))\s+\((?<provider>(\w+)),\s(?<version>(\d.+))\)'
$b = [pscustomObject]@{
Name = $matches.name
Provider = $matches.provider
Version = [version]$matches.version
}
$Boxes.Add($b)
}
if(-not $Name){
return $Boxes
}
else {
$r = $Boxes | Where-Object { $_.Name -eq $Name}
if(-not $r){
Write-Warning "Box not found!"
}
else {
return $r
}
}
}
}
On a command-line outside of my module I can see that things work as expected:
$lines = vagrant box list
. ./parseVagrantBox.ps1
parseVagrantBox -Lines $lines
Name Provider Version
---- -------- -------
StefanScherer/windows_2019 virtualbox 2021.5.15
ubuntu/trusty64 virtualbox 20190514.0.0
ubuntu/xenial64 virtualbox 20211001.0.0
Additionally, I can filter this:
parseVagrantBox -Lines $lines -Name 'ubuntu/xenial64'
Name Provider Version
---- -------- -------
ubuntu/xenial64 virtualbox 20211001.0.0
My error handling works as well:
parseVagrantBox -Lines $lines -Name 'ubuntu/wefwefewf'
WARNING: Box not found!
However, when I run this in the context of the generated Crescendo module, my filter/error logic falls apart:
Get-VagrantBox
Name Provider Version
---- -------- -------
StefanScherer/windows_2019 virtualbox 2021.5.15
ubuntu/trusty64 virtualbox 20190514.0.0
ubuntu/xenial64 virtualbox 20211001.0.0
Get-VagrantBox -Name 'ubuntu/xenial64'
Name Provider Version
---- -------- -------
StefanScherer/windows_2019 virtualbox 2021.5.15
ubuntu/trusty64 virtualbox 20190514.0.0
ubuntu/xenial64 virtualbox 20211001.0.0
Get-VagrantBox -Name 'uowejfi'
Name Provider Version
---- -------- -------
StefanScherer/windows_2019 virtualbox 2021.5.15
ubuntu/trusty64 virtualbox 20190514.0.0
ubuntu/xenial64 virtualbox 20211001.0.0
I've put the full Crescendo module code in a gist if anyone finds it useful to see exactly how I've configured things.
@jdhitsolutions - We improved and added error handling for the next release, please take a look and see if #170 fixes this for you.