Add Fuzzy Search to PowerShell

I’ve tried many text editors over time. One of the features I really like in Sublime Text and Visual Studio Code is the fuzzy search they implement in many places.

Here’s Visual Studio Code’s file picker in action.

I’ve wanted this feature in PowerShell for a while and I came across this post How is the fuzzy search algorithm in Sublime Text designed? and ported it to PowerShell.

Fuzzy Search in PowerShell

Here’s fuzzy search in action, applied to a list of strings, countries. It’s not limited to lists and can be applied to many other things in PowerShell.

One example

Get-Command | sfs invokepr

Grab the Script

Here’s my GitHub repo Select-FuzzySearch applies fuzzy searching to text.

Try it out and do a PR to improve it.

Getting Started with Azure Functions in PowerShell

Azure Functions is an event driven, compute-on-demand experience that extends the existing Azure application platform with capabilities to implement code triggered by events occurring in virtually any Azure or 3rd party service as well as on-premises systems. Azure Functions allows developers to take action by connecting to data sources or messaging solutions, thus making it easy to process and react to events. Azure Functions scale based on demand and you pay only for the resources you consume.

You can create functions in the Azure portal, but manual intervention isn’t good for scaling continuous delivery.

Create an Azure Function App

This link walks you through creating an Azure Function App. Be sure to grab the Resource Group Name and the SiteName so you can plug it into the PowerShell script.

Automate it with PowerShell

Using the New-AzureRmResource Azure cmdlet, you’ll setup the required parameters. $props is a PowerShell hashtable that holds the config information for the function. Here you’ll create a timerTrigger and put it on a schedule. In the files hashtable, the key value pair are the name of the PowerShell file to be created and the value is valid PowerShell.

In this example. I’ll leverage the .NET framework, creating a Net.WebClient, so I can download finance data from the Yahoo Finance web service.

This is a quick tour. You can also upload JavaScript code to run as a function. Plus, you could read the PowerShell from a file rather then hard coding it in a script.

Lastly

This is just scratching the surface. For example, you can create functions in many different languages and there other Trigger types as well. Plus, we only outputted information to the log, there’s much more to explore.

Full-Text Search: PowerShell meet Lucene

Bruce Payette, co-founder of PowerShell, gave a talk on integrating full-text search using Lucene with PowerShell at the PowerShell Summit.

Refactoring

So I did a little refactoring and wrapped it in GUI, this is all PowerShell.

In Action

It indexed the content of 2500+ files in ~2 seconds.

Index and Search

Type in the name of the directory to be searched, including a filter, c:\posh\*.ps1, press enter and it will recursively search the directory for all ps1 files and index the contents, keeping the index in memory. You can also search multiple directories with different filters. E.g. c:\temp\*.cs,c:\test\*.ts,c:\arm\*.json

Then, you can search for a term across everything that was indexed by typing in the Query box and pressing enter.

What is Lucene?

Apache Lucene is a free and open-source information retrieval software library, originally written in Java by Doug Cutting. It is supported by the Apache Software Foundation and is released under the Apache Software License.

Who uses Lucene

  • Apache SOLR (Used by OMS Operational Insights)
  • Elastic Search (everybody uses this)
  • CIteSeerX
  • Apple
  • 7digital (digital media)
  • Comcast
  • Disney

On GitHub

Grab it all from my GitHub Repo.

Paste JSON as a PowerShell Class in Visual Studio Code

The PowerShell extension for Visual Studio Code just got way cool.

First, we got a $profile. It lets you customize your environment and add session-specific elements to your PowerShell Visual Studio Code session.

Second, we got a way to interact from PowerShell directly with the vs code editor.

You use Register-EditorCommand to register a PowerShell function|scriptblock. In the PowerShell script you can set it up to a take a “context” parameter that gets passed in. This parameter contains a reference to the vs code editor (in the future this reference will surface more features), you can now do things like InsertText into the active file.

ConvertTo-Class

I took my existing PowerShell module and added this handful of code to connect it to Visual Studio. ConvertTo-Class takes a JSON string, figures out it structure plus data types and code generates a PowerShell v5.0 Class.

In Action

If you have PowerShell v5.0 installed, you can grab the ConvertTo-Class module from the command line like this Install-Module ConvertToClass

Edit your PowerShell VS Code $profile and add Import-Module ConvertToClass.

Start VS Code. In VS Code you can edit both a JSON file and PowerShell file and have a rich editing experience for each.

Here, I’m copying a JSON snippet of an Azure Resource Management (ARM) Template, then switching to a PowerShell file and pasting it as a PowerShell Class.

That’s the quick tour. PowerShell module authors just got some really cool integration tools for Microsoft’s new modern editor, Visual Studio Code.

I can’t wait to see what the community starts to add to the ecosystem.

Resources

Check out these links for more info.

PowerShell is a Query Language for JSON

PowerShell, as is, has all the tools you need to query JSON and then reshape, re-purpose it to what you need.

Result

Who Moved the Cheese – Microsoft’s new editor “Visual Studio Code”

Microsoft VS Code reached the 1.0 milestone and announced Version 1.0 of Visual Studio Code.

It is code editing redefined. Plus, PowerShell support for syntax highlighting ships in the box, and you need to install the extension (see VS Code Extensions to see how to) to get the rest, intellisense, debugger and much more.

I’ve been lucky to be involved with the PowerShell Editor Service, I’ve made contributions to it and have be around many great conversations about features now and to come.

Here is the Expand Alias feature I contributed.

VS Code

VS Code launched a year ago, there have been 2 million installs of VS Code. There are more than 500,000 people actively using VS Code each month.

  • Intelligent editing
  • Powerful debugging
  • Built-in Git support
  • Hundreds of extensions

What started as an experiment to build a production quality editor using modern web technologies has blossomed into a new kind of cross-platform development tool, one that focuses on […] productivity by centering the product on rich code editing and debugging experiences.

Really Cool

Besides the fact that I can a huge amount of work for PowerShell in VS Code, I found this next part really cool.

I’ve been doing more work in Azure and after I did a PaaS deployment of an App Service, I exported the configuration from the Azure Portal to a zip file.

The zip had five files, deploy.ps1, deploy.sh, DeploymentHelper.cs, parameters.json, and template.json. I extracted the files and then typed code . to launch vs code telling it use the current directory as my “project”.

Here’s where it gets cool, there are 4 different file types in that Azure export. A PowerShell file, Linux Shell file, a C# file and JSON files.

NOTE: I have these two Visual Studio Code extensions installed C# and Azure Resource Manager Tools.

From VS Code, I clicked on each file and VS Code provided me syntax highlighting, and intellisense for the PS1 and C# files and for the JSON files, because they are ARM templates, the Azure Tools extension gave me intellisense based on the ARM schemas defined.

Check it out

Editing four different file types, supporting syntax highlighting and more.

All in one place I could easily edit each file and in the case of the PowerShell script, I could run it from vs code, debug it and get all the intellisense I expect from other environment.

Fantastic!

VS Code Extensions

It is super easy to get these extensions in your environment too. After installing vs code, run it. For example, from a PowerShell prompt type code and press enter. After it launches, press F1. This will give you a dropdown at the top, it’s called the command palette, something you’ll use a lot. Next, type ext inst, press enter. This will query the Visual Studio Code Marketplace for published extensions.

When the list is returned, you can start typing the name of the extension you want, the list will update with matches. Type powersh. Next to the highlighted selection, you should see some blue boxes, you can click on the ReadME, License to read about these details or the Cloud and Arrow to download and install the extension. After it is installed, VS Code needs to restart and will prompt you if you want to do it now or defer to later.

Once installed and restarted the extension, when you edit a PowerShell file you’ll have even richer features at your fingertips. Repeat this to install the C# and Azure Manager Tools extension. Also, browse through the list to see what other extensions meet your needs.

VS Code Pandoc

This is a vs code extension that I wrote and published. I work a lot with markdown files. Visual Studio Code not only supports editing these files, it also has a live preview of them.

I ran across a free tool called Pandoc it renders markdown into amazing number of formats. The ones I’m interested in, PDF, Word Docs and HTML

NOTE: If you want to render to PDF you need to install MiKTeX for pandoc to generate it.

Install Follow the instruction in VS Code Extensions, search for pandoc and install.

In action

Here’s what the interaction looks like when rendering markdown to other formats in VS Code.

Check out vscode-pandoc in the market place.

More Productive

Visual Studio Code is a very impressive editor. For PowerShell, I’ve opted to do more of my PowerShell scripting in vs code. Yes, there are pieces missing, but not for long. Plus, if you are editing more than one type of file in your day, it’s worth checking out vs code. The sooner you do, the sooner you’ll find the moved cheese and be more productive.

NOTE: We haven’t even talked about how easy it is to create vs code extensions to fit your workflow.

Books about modern cloud products, companies and cultures

From this tweet to links and pictures.

Domain Driven Design

In Search of Certainty: The science of our information infrastructure

Release It!

Drift into Failure: From Hunting Broken Components to Understanding Complex Systems

Systems Thinking, Third Edition: Managing Chaos and Complexity: A Platform for Designing Business Architecture

The Principles of Product Development Flow: Second Generation Lean Product Development

Antifragile: Things That Gain from Disorder

Irresistible APIs: Designing web APIs that developers will love

Cloudonomics, + Website: The Business Value of Cloud Computing

Continuous Delivery: Reliable Software Releases through Build, Test, and Deployment Automation

Images from Microsoft Bing API with PowerShell Get-BingPics

PowerShell script for searching and downloading images from the Bing API. Inspired by this Python script Images from the iTunes/App/Mac App Stores.

Grab it

I put it up on on this GitHub repository.

Note: You need a Bing API Key to make it work.

You can pipe or pass a query to Get-BingPics:

Get-BingPics powershell or echo lions tigers bears liger | Get-BingPics

The script will look for your Bing API key in $env:BingPicKey, build the url with the query (line 17) and then use Invoke-RestMethod passing the api key in the header using basic authentication (line 19).

Lines 21-23 loops through each of the found images, creating an html anchor tag, with the image. Lines 25-32 create and html snippet and {1} (line 29) is replaced with anchor tags. This string is sent to a file and then in line 34, Invoke-Item launches it in your browser.