Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reference parameters in the OutputHandler #54

Open
jdhitsolutions opened this issue Jan 29, 2021 · 5 comments
Open

Reference parameters in the OutputHandler #54

jdhitsolutions opened this issue Jan 29, 2021 · 5 comments
Assignees
Labels
Issue-Enhancement New feature or request Issue-Triaged issue was read and triaged Needs-Author Feedback waiting for author feedback

Comments

@jdhitsolutions
Copy link

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?

@theJasonHelmick theJasonHelmick added the Issue-Triaged issue was read and triaged label Feb 2, 2021
@JamesWTruher
Copy link
Member

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?

@jdhitsolutions
Copy link
Author

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.

@tbergstedt
Copy link

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.

@steviecoaster
Copy link

steviecoaster commented Mar 25, 2022

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.

@theJasonHelmick theJasonHelmick added the Issue-Enhancement New feature or request label Jul 11, 2022
@theJasonHelmick theJasonHelmick added this to the Investigating milestone Jul 11, 2022
@theJasonHelmick theJasonHelmick self-assigned this Jul 20, 2022
@theJasonHelmick
Copy link
Collaborator

@jdhitsolutions - We improved and added error handling for the next release, please take a look and see if #170 fixes this for you.

@theJasonHelmick theJasonHelmick modified the milestones: Investigating, v.Next Aug 23, 2022
@theJasonHelmick theJasonHelmick added the Needs-Author Feedback waiting for author feedback label Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Enhancement New feature or request Issue-Triaged issue was read and triaged Needs-Author Feedback waiting for author feedback
Projects
None yet
Development

No branches or pull requests

5 participants