12

JSON Adapter Feedback Provider

 1 year ago
source link: https://devblogs.microsoft.com/powershell/json-adapter-feedback-provider/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

JSON Adapter Feedback Provider

Screenshot-2022-11-10-at-8.04.28-AM-96x96.png

Steven Bucher

ChristmasConduct-150x150.jpg

Jim Truher

July 10th, 20235 1

JSON Adapter Feedback Provider Release

We are excited to announce the first release of our JSON Adapter Feedback Provider! If you are unfamiliar with what feedback providers are, check out this blog describing them, here.

Note


Feedback Providers are an experimental feature of 7.4-preview2+ and so you will be required to use one of the 7.4 previews for JSON Adapters to work.

Installing JSON Adapter Feedback Provider

First to get the latest PowerShell preview for this to work on you can download them on our GitHub release page here.

The release is available from the PowerShell Gallery.

Use the following command to install JsonAdapter using PowerShellGet v2.x:

Install-Module -Name Microsoft.PowerShell.JsonAdapter -AllowPrerelease 

If you are using PSResourceGet, you can use the following command:

Install-PSResource -Name Microsoft.PowerShell.JsonAdapter -PreRelease

To use it you will need to import the module into your session via:

Import-Module Microsoft.PowerShell.JsonAdapter

We encourage you to include the import message in your $PROFILE so it can persistently be loaded in every PowerShell session you start. If you have Visual Studio Code installed, type code $PROFILE to edit your profile or use your choice of editor.

What are JSON Adapters?

A JSON adapter is a script that can parse the text output of a native executable and convert it to PowerShell Object. JSON adapters can be made for any command, it is just required to use the exact name of the command as the prefix to the script. The script will have to be named like so <name of command>-json.ps1 to be identified by the JSON adapter utility. This script’s file location must also be added to your $env:PATH variable to be found.

Creating a JSON Adapter

For example, say you are on a Mac and want to use the command vm_stat like a PowerShell object. If you add the following to a file called vm_stat-json.ps1 and add the location of this file to your $env:PATH variable, the JSON Adapter feedback provider will identify it as a possible suggestion for vm_stat.

[CmdletBinding()]
param ( [Parameter(ValueFromPipeline=$true)][string]$inputObject )
BEGIN {
    $h = @{}
}

PROCESS {
    if ( $inputObject -match "^Mach Virtual") {
        if ($inputObject -match "page size of (\d+) ") {
            $h['PageSize'] = [int]$matches[1]
        }
    }
    else {
        $k,$v = $inputObject -split ":"
        $AdjustedK = ($k -replace "[ -]","_").trim() -replace '"'
        $AdjustedV = "$v".Trim() -replace "\.$"
        $h[$AdjustedK] = [int64]$AdjustedV
    }
}

END {
    [pscustomobject]$h
}

This is what the experience looks like in the shell.

VM stat screenshot

JC or JSON Converter, is a command line utility that can convert text to JSON for variety of command line tools. You can find instructions on how to install jc and a full list of supported commands on the repo of jc. It can be a great tool to use to convert the outputs without writing a JSON Adapter yourself. The JSON Adapter module supports using jc as a JSON Adapter if the user has it installed. This means if you have the jc utility installed and use a command that is supported by JC, the JSON Adapter feedback provider will suggest using JC piped to ConvertFrom-JSON.

It is important to note that not all jc supported utilities are supported. The list of supported commands is:

"arp", "cksum", "crontab", "date", "df", "dig", "dir", "du", "file", "finger",
"free", "hash", "id", "ifconfig", "iostat", "jobs", "lsof", "mount", "mpstat",
"netstat", "route", "stat", "sysctl", "traceroute", "uname", "uptime", "w", "wc",
"who", "zipinfo"

Additionally, you will need to use the appropriate parameters that jc requires to work properly. For example, if you want to use jc with uname, you will need to use uname -a as that is what jc requires to properly convert the output to JSON.

Predictive IntelliSense Support

We have also added predictive IntelliSense support for the JSON Adapter feedback provider. This means after a JSON Adapter feedback provider is triggered, as you type the command name again, Predictive Intellisense will suggest the feedback command to you. This is a great way to easily try the suggestion after a JSON Adapter feedback provider is triggered.

screenshot showing predictive intellisense support

Feedback

As this is our very first release, we know there may be issues that arise. We definitely look forward to your feedback and suggestions! You can provide feedback on the repo for this project here. Many things are subject to change as we are in early development of this. Give it a try!

Jim Truher and Steven Bucher

PowerShell Team

Steven Bucher Product Manager

Follow

Jim Truher Senior Software Engineer, PowerShell

Follow


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK