Scott Hanselman

Keeping your WSL Linux instances up to date automatically within Windows 10

October 06, 2020 Comment on this post [9] Posted in Linux | Win10
Sponsored By

image[3]Hayden Barnes from Canonical, the folks that work on Ubuntu (lovely blog, check out it) had a great tweet where he recommended using the Windows Task Scheduler (think of it as a graphical cron job manager) to keep your WSL Linux instances up to date.

There's a few things to unpack here to get into the details.

First, if you run wsl --list -v you'll see all the WSL Linux Instances on your machine.

> wsl --list -v
NAME STATE VERSION
* Ubuntu-18.04 Running 2
kali-linux Stopped 1
Alpine Stopped 1
Ubuntu-20.04 Stopped 2
WLinux Running 2
docker-desktop-data Stopped 2
docker-desktop Stopped 2

You can I see I have a few. I spend most of my time in the Ubuntu instances, but I also occasionally drop into the kali-linux and WLinux instances. If I'm using LTS (long term support) distros then there's minimal risk (my opinion) in "apt get update" and "apt get upgrade"-ing them every week or so. I could even do it unattended.

I could set up a Task Scheduler and make an "on login" task or a weekly task that calls wsl.exe and passes in -d for distro, along with the name of the distro, run as root with -u and -e for the command. For example:

wsl -d "Wlinux" -u root -e apt update
wsl -d "Wlinux" -u root -e apt upgrade -y

Since I have several WSL instances, I could also make a "updateall.cmd" or .bat or .ps1 script and run them occasionally to keep them all updated on my own. Just change the -d and include the name of each distro. One could imagine a group policy as well for large enterprises to do the same thing for developers using a custom or managed WSL instance.

You would not want to update or mess with the docker- managed WSL instances above as they exist only to run your Docker Desktop-managed containers. Leave that to Docker to manage.

It's a whole new world out there, and I'm loving how I can move easily between multiple Linuxes on Windows 10. Check out my YouTube on WSL2 and please subscribe over there.


Sponsor: Never miss a beat with Seq. Live application logs and health checks. Download the Windows installer or pull the Docker image now.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service

How to use autocomplete at the command line for dotnet, git, winget, and more!

October 01, 2020 Comment on this post [19] Posted in Open Source
Sponsored By

Many years ago .NET Core added Command line "tab" completion for .NET Core CLI in PowerShell or bash but few folks have taken the moment it takes to set up.

I enjoy setting up and making my prompt/command line/shell/terminal experience as useful (and pretty) as possible. You have lots of command line shells to choose from on Windows!

Keep in mind these are SHELLs not terminals or alternative consoles. You can run all of these in the Windows Terminal.

  • Classic cmd.exe command prompt (fake DOS!) - Still useful and Clink can make it more bash-y without going full bash.
  • Yori from Malcolm Smith
  • Starship - more on this later, it's somewhat unique
  • Windows PowerShell - a classic because...
  • PowerShell 7 is out and runs literally anywhere, including ARM machines!

I tend to use PowerShell 7 (formerly PowerShell Core) as my main prompt because it's a cross-OS prompt. I can use the same prompt, same functions, same everything on Windows and Linux.

But it's command-line autocompletion that brings me the most joy!

  • git ch<TAB> -> git checkout st<TAB> -> git checkout staging
  • dotnet bu<TAB> -> dotnet build
  • dotnet --list-s<TAB> -> dotnet --list-sdks
  • winget in<TAB> -> winget install -> winget install WinDi<TAB> -> winget install WinDirStat

Once you have successfully tab'ed you way to glory it's hard to stop. With PowerShell and its cousins this is made possible with Register-ArgumentCompleter. Here's what it looks like for the dotnet CLI.

# PowerShell parameter completion shim for the dotnet CLI
Register-ArgumentCompleter -Native -CommandName dotnet -ScriptBlock {
param($commandName, $wordToComplete, $cursorPosition)
dotnet complete --position $cursorPosition "$wordToComplete" | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}

Looks like a lot, but the only part that matters is that when it sees the command "dotnet" and some partial text and the user presses TAB, it will call "dotnet complete" passing in the cursorPosition and the wordToComplete.

NOTE: If you understand how this works, you can easily make your own Argument Completer for those utilities that you use all the time at work! You can make them for the folks at work who use your utilities!

You never actually see this call to "dotnet complete." You just see yourself typing dotnet bui<TAB> and getting a series of choices to tab through!

Here's what happens behind the scenes:

>dotnet complete --position 3 bui
build
build-server
msbuild

You can add these to your $profile. Usually I run 'notepad $profile" at the command line and it will autocreate the correct file in the correct location.

This is a super powerful pattern! You can get autocomplete in Git in PowerShell with PoshGit as well as in WinGet!

What are some more obscure autocompletes that you have added to your PowerShell profile?

ACTION: Finally, please take a moment and subscribe to my YouTube or head over to http://computerstufftheydidntteachyou.com and explore! I'd love to hit 100k subs over there. I heard they give snacks.


Sponsor: Suffering from a lack of clarity around software bugs? Give your customers the experience they deserve and expect with error monitoring from Raygun.com. Installs in minutes, try it today!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service

How to use, open, resize, and split Panes in the Windows Terminal

September 29, 2020 Comment on this post [4] Posted in Win10
Sponsored By

My love and appreciate for the new open-source Windows Terminal is well-documented. I enjoy customizing the Windows Terminal with a nice prompt.

The Terminal of course has Tabs so you can open many different shells at once within a terminal instance, often I want to do things like Split Screen/Split Pane. "Use Tmux!" you might shout, and that's a valid thing to yell if I was only living in Linux (using WSL2). There are several multi-pane options to choose from within a shell using something like tmux.

However, the Windows Terminal supports a multi-pane view at the Terminal-level, regardless of shell!

There's great docs on setting up hotkeys for this, and you should.

The best way to get started with ZERO setup is to click the main Dropdown in Windows Terminal and hold down the ALT key while you click on a shell!

Below you can see Ubuntu/WSL2 on the left running htop, while on the right I'm running PowerShell 7 (powered by .NET Core) and sitting in my podcast's source code directory.

MultiPane Windows Terminal

I'll then click the dropdown, hold ALT, and click on the Visual Studio Developer Command Prompt that I've added to the menu. I'm doing this while Ubuntu is the focused pane.

My Windows Terminal Menu

And the result:

Three panes in Windows Terminal

Now you can see the VS2019 prompt in the lower left corner. With hotkeys I can control where panes open.

I can even navigate between pans with the ALT key and my arrow keys! Even better, SHIFT+ALT and the arrow keys will resize them!

Resized my Terminal Panes

Go spend some time learning about Panes in Windows Terminal and let me know how it goes for you! It's gonna make your command line life so much better!

ACTION: Finally, please take a moment and subscribe to my YouTube or head over to http://computerstufftheydidntteachyou.com and explore! I'd love to hit 100k subs over there. I heard they give snacks.


Sponsor: Suffering from a lack of clarity around software bugs? Give your customers the experience they deserve and expect with error monitoring from Raygun.com. Installs in minutes, try it today!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service

Exploring your .NET applications with dotnet-monitor

September 24, 2020 Comment on this post [6] Posted in
Sponsored By

I talked about Cross-platform diagnostic tools for .NET Core and dotnet-trace for .NET Core tracing but I would be remiss if I didn't show and mention "dotnet monitor."

dotnet monitor is an experimental tool that makes it easier to get access to diagnostics information in a dotnet process. If you're running .NET Core within a container - or mostly likely in Kubernetes - this tool offers an insight into your running microservice. Basically it creates a microservice of its own that you can interrogate to better understand what's happening.

HELP! Confused about Kubernetes? Check out http://computerstufftheydidntteachyou.com and my most recent video on Kubernetes and Container Orchestration.

Assuming you have .NET Core installed, you can install dotnet monitor as a global tool:

dotnet tool install -g dotnet-monitor --add-source https://dnceng.pkgs.visualstudio.com/public/_packaging/dotnet5-transport/nuget/v3/index.json --version 5.0.0-preview.*

You then just run it along side your project or running process.

dotnet monitor collect

The developer blog on dotnet monitor shows you how you can share a volume mount between your application container and a container running dotnet monitor if you like. If you're in k8s (Kubernetes) you should run dotnet monitor as a sidecar to your container within the same pod.

It'll start up and you can talk to dotnet monitor with curl, wget, and pipe through jq and hit localhost:52323/processes to get a list of .NET Core processes it can think about.

NOTE: If you are running this locally and get auto redirected to HTTPS then you may have a cached HSTS policy for localhost from other work. Head over to edge://net-internals/#hsts (or chrome://) and scroll to the bottom and delete the Domain Security Policies for localhost.

Now I can curl and see the output. I have my podcast on the left pane in Windows Terminal, dotnet monitor collect in the upper right, and the output lower right.

dotnet monitor

Once I figure out my process id (PID) - which will be automatic within a container as there will only be one - I can explore any of these local endpoints:

  • /processes
  • /dump/{pid?}
  • /gcdump/{pid?}
  • /trace/{pid?}
  • /logs/{pid?}
  • /metrics

If you are getting the logs, you'll get a never ending text/event-stream in your browser. I'd recommend you "curl" to see this at the command line.

text/eventstream in your browser

You can also get momentary traces, collect a nettrace file and analyze it in Visual Studio, PerfView or other tools.

The dotnet monitor is experimental, but if you're digging it, head over to the dotnet/diagnostics GitHub and show some support.

SOS and Other Diagnostic Tools

  • SOS - About the SOS debugger extension.
  • dotnet-dump - Dump collection and analysis utility.
  • dotnet-gcdump - Heap analysis tool that collects gcdumps of live .NET processes.
  • dotnet-trace - Enable the collection of events for a running .NET Core Application to a local trace file.
  • dotnet-counters - Monitor performance counters of a .NET Core application in real time.

Hope this helps you!


Sponsor: Upgrade from file systems and SQLite to Actian Zen Edge Data Management. Higher Performance, Scalable, Secure, Embeddable in most any programming language, OS, on 64-bit ARM/Intel Platform.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service

Blazor WebAssembly on Azure Static Web Apps

September 23, 2020 Comment on this post [17] Posted in ASP.NET | DotNetCore
Sponsored By

Blazing Pizza Blazor AppMany apps today are just static files on the front end - HTML and JavaScript - with something powerful on the server side. They aren't "static apps" as they have very dynamic front end experiences, but they are static in that their HTML isn't dynamically generated.

As such, you don't need to spend the money on an Azure Web App when an Azure Static Web App will do! These apps get free SSL certs, custom domains, web hosting for static content, and fit into a natural git-based workflow for publishing. You can build modern web applications with JavaScript frameworks and libraries like Angular, React, Svelte, Vue, or using Blazor to create WebAssembly applications, with an Azure Functions back-end or publish static sites with frameworks like Gatsby, Hugo, VuePress.

But there's big news out of Ignite this week, with Azure Static Web Apps now supporting Blazor applications. You can develop and deploy a frontend and a serverless API written entirely in .NET.

To get started "hello world style" there is a GitHub repository template that's a starting point. It's a basic web app with a client that uses Blazor and .NET that is run on the client-side in your browser using WebAssembly.

Called it! It's almost a decade later and yes, JavaScript (and WebAssembly) is the assembly language for the web!

So the client runs in the browser written in C#, the server runs as a serverless Azure Function (meaning no identifiable VM, and it just scales as needed) also written in C#, and this client and server share a data model between Blazor and Functions also written in...wait for it...C#.

An app like this can basically scale forever, cheaply. It can put the browser to work (which was basically hanging out waiting for anglebrackets anyway) and when it needs data, it can call back to Functions, or even Azure CosmosDB.

Be sure to check out this complete Tutorial: Building a static web app with Blazor in Azure Static Web Apps! All you need is a GitHub account and any free Azure Account.

If you want more guided learning, check out the 12 unit module on Microsoft Learn. It shouldn't take more than an hour and you'll learn how to Publish a Blazor WebAssembly app and .NET API with Azure Static Web Apps.

Resources

Also be sure to check out the Day 2 Microsoft Ignite Keynote by yours truly! The app I made and demo in the keynote? Made with Blazor and Azure Static Web Apps, natch! The keynote is happening in three time zones so you can see it at a time that works for you...or on-demand!


Sponsor: Upgrade from file systems and SQLite to Actian Zen Edge Data Management. Higher Performance, Scalable, Secure, Embeddable in most any programming language, OS, on 64-bit ARM/Intel Platform.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.