JSON Adapter Feedback Provider

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.

Installing JSON Adapter Feedback Provider

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 -AllowPrerelease

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 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
JSON. Once the output is in a machine readable format like JSON, you can use ConvertFrom-JSON
cmdlet to make any native executable behave like a 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 -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

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 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.

Jim Truher and Steven Bucher

PowerShell Team

The post JSON Adapter Feedback Provider appeared first on PowerShell Team.

 

This article was originally published by Microsoft's ITOps Talk Blog. You can find the original article here.