Monday, May 20, 2024
HomePowershellJSON Adapter Suggestions Supplier - PowerShell Workforce

JSON Adapter Suggestions Supplier – PowerShell Workforce


We’re excited to announce the primary launch of our JSON Adapter Suggestions Supplier! If you’re
unfamiliar with what suggestions suppliers are, take a look at this weblog describing them, right here.

Notice


Suggestions Suppliers are an experimental characteristic of seven.4-preview2+ and so you can be required to make use of one of many 7.4 previews for JSON Adapters to work.

Putting in JSON Adapter Suggestions Supplier

First to get the newest PowerShell preview for this to work on you’ll be able to obtain them on our GitHub launch web page right here.

The discharge is on the market from the PowerShell Gallery.

Use the next command to put in JsonAdapter utilizing PowerShellGet v2.x:

Set up-Module -Identify Microsoft.PowerShell.JsonAdapter -AllowPrerelease 

If you’re utilizing PSResourceGet, you should use the next command:

Set up-PSResource -Identify Microsoft.PowerShell.JsonAdapter -PreRelease

To make use of it you have to to import the module into your session through:

Import-Module Microsoft.PowerShell.JsonAdapter

We encourage you to incorporate the import message in your $PROFILE so it may well persistently be loaded
in each PowerShell session you begin. When you have Visible Studio Code put in, kind
code $PROFILE to edit your profile or use your selection of editor.

What are JSON Adapters?

A JSON adapter is a script that may parse the textual content output of a local executable and convert it to
PowerShell Object. JSON adapters will be made for
any command, it’s simply required to make use of the precise identify of the command because the prefix to the script.
The script should be named like so <identify of command>-json.ps1 to be recognized by the JSON
adapter utility. This script’s file location should even be added to your $env:PATH variable to be
discovered.

Making a JSON Adapter

For instance, say you’re on a Mac and need to use the command vm_stat like a PowerShell object. If
you add the next to a file referred to as vm_stat-json.ps1 and add the placement of this file to your
$env:PATH variable, the JSON Adapter suggestions supplier will determine it as a attainable suggestion
for vm_stat.

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

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

END {
    [pscustomobject]$h
}

 

That is what the expertise appears like within the shell.

JC

JC or JSON Converter, is a command line utility that may convert textual content to JSON for number of command
line instruments. You could find directions on the right way to set up jc and a full record of supported instructions
on the repo of jc. It may be an excellent device to make use of to transform the outputs with out writing a JSON
Adapter your self. The JSON Adapter module helps utilizing jc as a JSON Adapter if the person has it
put in. This implies when you have the jc utility put in and use a command that’s supported by JC, the JSON
Adapter suggestions supplier will recommend utilizing JC piped to ConvertFrom-JSON.

It is very important notice that not all jc supported utilities are supported. The record of supported instructions 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"

Moreover, you have to to make use of the suitable parameters that jc requires to work correctly. For
instance, if you wish to use jc with uname, you have to to make use of uname -a as that’s what jc
requires to correctly convert the output to JSON.

Predictive IntelliSense Assist

We’ve additionally added predictive IntelliSense help for the JSON Adapter suggestions supplier. This
means after a JSON Adapter suggestions supplier is triggered, as you kind the command identify once more,
Predictive Intellisense will recommend the suggestions command to you. This can be a nice approach to simply strive
the suggestion after a JSON Adapter suggestions supplier is triggered.

screenshot showing predictive intellisense support

Suggestions

As that is our very first launch, we all know there could also be points that come up. We positively look
ahead to your suggestions and ideas! You may present suggestions on the repo for this mission
right here. Many issues are topic to vary as we’re in early growth of this. Give it a strive!

Jim Truher and Steven Bucher

PowerShell Workforce

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments