Help with Crescendo

Hi All,

I am trying out the Crescendo module and I am getting an error when running the Export-CrescendoModule . The json looks good but I am getting this error “InvalidOperation: You cannot call a method on a null-valued expression.”

here is the code I am running

Import-Module  Microsoft.PowerShell.Crescendo -Verbose

 $tempJson = New-TemporaryFile

New-CrescendoCommand -Verb 'Get' -Noun 'ChocoPackage' | %{
    $_.OriginalName = 'choco'
    $_.OriginalCommandElements = @(
        'search',
        '--limit-output',
        '--local-only'
    )
    $_.Description = 'Get installed Chocolatey packages'
    $_.Parameters = @(
        @{
            Name = 'Package'
            ParameterType = 'string'
            Description = 'Package Name'
        }
    )
    $_.OutputHandlers = @(
        @{
            ParameterSetName = 'Default'
            Handler = {
                param ( $output )
                $output | ForEach-Object {
                    $name,$version = $_ -split '\|'
                    [pscustomobject]@{
                        Name = $name
                        Version = $version
                    }
                }
            }
        }
    )
    $_
} | ConvertTo-Json | Out-File $tempJson

Export-CrescendoModule -ConfigurationFile $tempJson -ModuleName .\Get-ChocoPackage.psm1 -Force 

any help would be greatly appreciated! Thx

Please share the complete error message (formatted as cdoe).

Thanks in advance.

 Export-CrescendoModule -ConfigurationFile $tempJson -ModuleName .\Get-ChocoPackage.psm1 -Force
InvalidOperation: You cannot call a method on a null-valued expression.```

here is the json file I am using

{
  "Verb": "Get",
  "Noun": "ChocoPackage2",
  "OriginalName": "choco",
  "OriginalCommandElements": [
    "search",
    "--limit-output",
    "--local-only"
  ],
  "Platform": [
    "Windows"
  ],
  "Elevation": null,
  "Aliases": null,
  "DefaultParameterSetName": null,
  "SupportsShouldProcess": false,
  "ConfirmImpact": null,
  "SupportsTransactions": false,
  "NoInvocation": false,
  "Description": "Get installed Chocolatey packages",
  "Usage": null,
  "Parameters": [
    {
      "ParameterType": "string",
      "Position": 2147483647,
      "Name": "Package",
      "OriginalName": null,
      "OriginalText": null,
      "Description": "Package Name",
      "DefaultValue": null,
      "DefaultMissingValue": null,
      "ApplyToExecutable": false,
      "AdditionalParameterAttributes": null,
      "Mandatory": false,
      "ParameterSetName": null,
      "Aliases": null,
      "OriginalPosition": 0,
      "ValueFromPipeline": false,
      "ValueFromPipelineByPropertyName": false,
      "ValueFromRemainingArguments": false,
      "NoGap": false
    }
  ],
  "Examples": [],
  "OriginalText": null,
  "HelpLinks": null,
  "OutputHandlers": [
    {
      "ParameterSetName": "Default",
      "Handler": "\n                param ( $output )\n                $output | ForEach-Object {\n                    $name,$version = $_ -split '\\|'\n                    [pscustomobject]@{\n                        Name = $name\n                        Version = $version\n                    }\n                }\n            ",
      "HandlerType": "Inline",
      "StreamOutput": false
    }
  ],
  "FunctionName": "Get-ChocoPackage2"
}

On line 35 you have a $_

I’m guessing it’s not supposed to be there because deleting it gets rid of the error.

    )
    $_
} | ConvertTo-Json | Out-File $tempJson

But then you get rid of the content as well :wink: … at least here in my test the JSON is empty.

Oops, you’re right. I was looking at the wrong temp file :blush:

yes but looks like we are making some head way! Thx guys

if $_ is on 35 the json looks good but the error happens with the export command .

Please don’t ask me to explain … I couldn’t … I just played around with it …

With the following JSON I could actually create the Crescendo module and run the Get-ChocoPackage cmdlet:

{
  "$schema": "https://aka.ms/Crescendo/Schema.json",
  "Commands": [
    {
      "Verb": "Get",
      "Noun": "ChocoPackage",
      "OriginalName": "choco",
      "OriginalCommandElements": [
        "search",
        "--limit-output",
        "--local-only"
      ],
      "Platform": [
        "Windows"
      ],
      "Description": "Get installed Chocolatey packages",
      "Parameters": [
        {
          "Name": "Package",
          "Description": "Package Name",
        }
      ],
      "OutputHandlers": [
        {
          "ParameterSetName": "Default",
          "Handler": "\r\n                param ( $outputLines )\r\n                foreach ($Line in $outputLines) {\r\n                        $name, $version = $Line -split '\\|'\r\n                        [pscustomobject]@{\r\n                            Name    = $name\r\n                            Version = $version\r\n                        }\r\n                }\r\n            ",
          "HandlerType": "Inline",
          "StreamOutput": false
        }
      ],
      "FunctionName": "Get-ChocoPackage"
    }
  ]
}
1 Like

New-CrescendoCommand creates only command object, its not the full Crescendo JSON.

{
   "$schema": "https://aka.ms/PowerShell/Crescendo/Schemas/2021-11",
   "Commands": [
     // Here comes the command object
    ]
}

Its not clear from the docs or not at all mentioned anywhere AFAIK.
You can do like


$CrescendoCommandObject = New-CrescendoCommand -Verb 'Get' -Noun 'ChocoPackage' | %{
    # Your code here
}

[PSCustomObject]@{
"`$Schema" = "https://aka.ms/PowerShell/Crescendo/Schemas/2021-11"
 Commands = @($CrescendoCommandObject)
} | ConvertTo-Json -Depth 10 | Out-File $tempJson

# Rest of the code

More samples Crescendo/Microsoft.PowerShell.Crescendo/Samples at master · PowerShell/Crescendo (github.com)

3 Likes

wow! that’s awesome Thx !!

Thank you I will give that a go!

I am running this and still getting the same error

Import-Module  Microsoft.PowerShell.Crescendo -Verbose

$tempJson = New-TemporaryFile


$CrescendoCommandObject = New-CrescendoCommand -Verb 'Get' -Noun 'ChocoPackage' | %{
    $_.OriginalName = 'choco'
    $_.OriginalCommandElements = @(
        'search',
        '--limit-output',
        '--local-only'
    )
    $_.Description = 'Get installed Chocolatey packages'
    $_.Parameters = @(
        @{
            Name          = 'Package'
            ParameterType = 'string'
            Description   = 'Package Name'
        }
    )
    $_.OutputHandlers = @(
        @{
            ParameterSetName = 'Default'
            Handler          = {
                param ( $output )
                $output | ForEach-Object {
                    $name, $version = $_ -split '\|'
                    [pscustomobject]@{
                        Name    = $name
                        Version = $version
                    }
                }
            }
        }
    )
    $_  
}

[PSCustomObject]@{
"`$Schema" = "https://aka.ms/PowerShell/Crescendo/Schemas/2021-11"
 Command = @($CrescendoCommandObject)
} | ConvertTo-Json -Depth 10 | Out-File $tempJson

Export-CrescendoModule -ConfigurationFile $tempJson -ModuleName .\Get-ChocoPackage.psm1 -Force 

here is the json

{
  "$Schema": "https://aka.ms/PowerShell/Crescendo/Schemas/2021-11",
  "Command": [
    {
      "Verb": "Get",
      "Noun": "ChocoPackage",
      "OriginalName": "choco",
      "OriginalCommandElements": [
        "search",
        "--limit-output",
        "--local-only"
      ],
      "Platform": [
        "Windows",
        "Linux",
        "MacOS"
      ],
      "Elevation": null,
      "Aliases": null,
      "DefaultParameterSetName": null,
      "SupportsShouldProcess": false,
      "ConfirmImpact": null,
      "SupportsTransactions": false,
      "NoInvocation": false,
      "Description": "Get installed Chocolatey packages",
      "Usage": null,
      "Parameters": [
        {
          "ParameterType": "string",
          "Position": 2147483647,
          "Name": "Package",
          "OriginalName": null,
          "OriginalText": null,
          "Description": "Package Name",
          "DefaultValue": null,
          "DefaultMissingValue": null,
          "ApplyToExecutable": false,
          "AdditionalParameterAttributes": null,
          "Mandatory": false,
          "ParameterSetName": null,
          "Aliases": null,
          "OriginalPosition": 0,
          "ValueFromPipeline": false,
          "ValueFromPipelineByPropertyName": false,
          "ValueFromRemainingArguments": false,
          "NoGap": false
        }
      ],
      "Examples": [],
      "OriginalText": null,
      "HelpLinks": null,
      "OutputHandlers": [
        {
          "ParameterSetName": "Default",
          "Handler": "\n                param ( $output )\n                $output | ForEach-Object {\n                    $name, $version = $_ -split '\\|'\n                    [pscustomobject]@{\n                        Name    = $name\n                        Version = $version\n                    }\n                }\n            ",
          "HandlerType": "Inline",
          "StreamOutput": false
        }
      ],
      "FunctionName": "Get-ChocoPackage"
    }
  ]
}

There was a typo in the code I shared :upside_down_face:, its commands not command

Commands = @($CrescendoCommandObject)

Worked!! Thank you sir … much appreciated

Do you know of a good resource to understand the scripting component ?

You may start here:

1 Like