Categories
geek microsoft programming software tips tools

home made progress bars and indicators in PowerShell

Sometimes you write this fancy batch script that does a bunch of stuff, and you want to have it print out some status information as it’s doing its thing.

Sometimes this fancy script is doing a lot and there’s going to be a lot of stuff printed, so it would be nice if you could overwrite the previous bit of text. Basically, you want a progress bar or progress indicator of some sort.

There are a few ways of doing this, and one involves manipulating the $Host.UI.RawUI.CursorPosition values. That needs “a lot” of code for something that you really don’t want to write a lot of code for.
There are also the oldskool typewrite control characters, however. Like the Carriage Return, `r in PowerShell, which does pretty much the same thing.

So this bit of code, for example, prints everything out on a single line, even though it’s doing that a hundred times:

100..0 | % { write-host "`r- Items to process: $($_)".PadRight(25) -nonewline; sleep -milliseconds 20 }

The magic is in this line:

write-host "`r- Items to process: $($_)".PadRight(25)

Note the “`r” at the beginning of the line. This will reset the cursor to the beginning of the current line, printing the text behind it over any text already present on that line.
Do this in a loop, and you keep writing over the previously printed text.

This also explains the PadRight() statement, which makes sure that there are spaces added to the end of the line to erase any characters left over if the previous line was longer than the current one.
This happens a number of times in this case, as we’re counting from 100 to 0. I know there are smarter ways to fix this, but this works just fine right here (KISS).

Here’s another example using the CR trick. An actual character based progress bar. Just copy-paste and run it in a shell to see the effect:

1..20 | % { write-host ("#"*$_ + "|" * (20-$_) + "`r") -nonewline ; sleep -Milliseconds 200 }; ""

The following example is a bit more complex. It displays a spinner for longer running operations using a set of characters.

# Animation object to keep state.
$global:animation = @{ Chars = "-\|/"; Ix = 0; Counter = 0 }

# Animate one step every 500 calls. Lower the number for a faster animation.
function Animate() {
    $a = $global:animation

    if (($a.Counter % 500) -eq 0) {
        Write-Host " $($a.Chars[$a.Ix])`r" -NoNewline
        $a.Ix = ($a.Ix + 1) % $a.Chars.Length
    }
    $a.Counter++ 
}

# Usage example. Call the animation in a loop. 
$largeImages = ls *.jpg -r | where { $_.length -gt 100000; animate }

There’s also the official Write-Progress PowerShell commandlet to show a progress bar on the screen. You might want to check that out too. I’m not a fan of it myself, because it tends to act strange when you scroll in your shell window, but for more complex status updates it can be really handy.

I hope this helps to make your scripts a bit more informative (or fun) when running long jobs.

Categories
geek microsoft programming software tips

asp.net cache profile location attribute

Here’s something that confused me recently. I was debugging some caching issue, and it looked like the ASP.NET (framework) site wasn’t actually using the output cache settings.

The problem was that I was looking at the response headers, and it kept showing the Cache-Control no-cache header value. When I debugged the controller however, I noticed that it didn’t always hit the breakpoint, so cache was working.

Turns out the location of the cache profile was set to server.

<add name="somepage" duration="60" location="Server" />

Setting the location attribute to server makes ASP.NET cache the output in memory of your server, and it sets the response header to no-cache. That way, proxies like Cloudflare won’t cache the response, and you can do fancy stuff in your ASP.NET app using internal data to vary your cached responses (see VaryByCustom), and have them cached in memory on your server.

The upside is you can vary your output cache using information Cloudflare or Varnish don’t know. The downside is that this means your servers will get more hits and the more servers you are using in a load balancer setup, the less effective your cache will be.

If you drop the location attribute, the Cache-Control header will be set to whatever time you have set in your cache profile, and proxies can start caching the results.

Categories
geek microsoft programming software tips tools

fix cascadia mono missing in visual studio without rebooting

Here’s a dumb problem I keep having on my work laptop. For some reason, Visual Studio 2022 shows me a notification that it can’t find the Cascadia fonts, and that a reboot will probably fix the problem.

That’s great and all, but I’m like in the middle of something and have a ton of other apps open and really don’t feel like rebooting right now (do we ever?). But being stuck looking at code in an ugly ass Courier font, isn’t what a self-respecting developer feels like doing either, right?

Last time I ran into this, I figured I might as well find the font and see if I couldn’t just reinstall it. VS should pick it up again after a restart. Turns out I was right. No reboot needed, here’s how you reinstall the Cascadia fonts on your machine:

  • Open the path C:\Windows\Fonts
  • Lookup the Cascadia fonts. There should be 2, CascadiaCode.ttf and CascadiaMono.ttf.
  • For each font file, double click it.
    A window will open, previewing the font. In the top toolbar, click Install.
  • Now restart Visual Studio. You’ll see your code represented in a pretty font once again.
Categories
geek linux microsoft programming software tips tools ubuntu windows

WSL setup, tips and tricks

WSL or Windows Subsystem for Linux allows you to run a fully functional Linux environment, inside your Windows 10 system, without having to set up a virtual machine. It’s a lot more lightweight too, and it integrates nicely with the Windows OS, so it’s a great way to explore Linux, or use the two systems side by side for cross-platform development. This also skips the need for setting up Cygwin and whatever hacky way to access those Linux command line tools on Windows.

I’ve been using it for a while, so I’ll list a short setup guide here, and some tips & tricks to help you along.

Installing WSL

Installing WSL is really easy now. Just open an administrator PowerShell or a Command window and type:

wsl --install

That will install WSL with the default Ubuntu distribution. If you prefer something else, like Debian, you can use this command:

wsl --install -d Debian

The username and password for your WSL system is specific to the Linux distribution. So make sure you don’t forget that password. :)

There are 2 versions of WSL. If you can, run on v2. It’s faster, better and euhm, harder? Anyway, it’s faster, so that’s what you want. But sometimes, you might want to run things on v1 anyway. For example, some VPN clients break with the WSL v2 networking. The easiest way is to set your WSL distro to v1, and see if that works. It did the trick in my case with the CheckPoint VPN on my work machine.

To check your WSL version, run:

wsl -l -v

It should show something like this, if you’re on Debian and v2

  NAME                   STATE           VERSION
* Debian                 Stopped         2

To switch the Debian distro to v1, you can run this:

wsl --set-version Debian 1

Makes sense right? But you just have to know.
Switching versions can take a while as it’s being converted, so do this when you have the time for it.

Accessing your files in WSL

Ok, now let’s do some work in WSL, by typing wsl in a PowerShell window.

That’s it, you now have a shell in your local WSL system.
You’ll see something like this on your prompt:

n3wjack@Mjolnir:/mnt/c/Users/n3wjack$

That’s a mount of your Windows C-drive into the Linux system. So you can access any file from your Windows system in your Linux shell. The other way around also works.

Open up an Explorer window and enter \\wsl$ in the address bar and hit return.
You’ll see a folder pop up for each WSL distribution you have installed. So if you’ve installed Debian, you’ll see a Debian folder. From there you can access any file on your WSL system.

Windows Explorere windows showing the Debian distribution folder under the \\wsl$ network drive.

Keep in mind that this cross OS file access is pretty slow, certainly for lots of small files. So if you are planning on working on files, it’s better to choose your OS and stick with it. But it’s super handy that you can easily copy and access files from any system.

Installing software in WSL

Well, this is easy. If you’ve installed Debian or Ubuntu, so you probably know you can install more software using apt or apt-get.
WSL pretty much behaves as it should, and you can just install whatever you like using known tools.
The account you created when setting up your distro is an administrator account, so you can use sudo commands.

Moving your WSL distro to a new PC

So you have your Linux distro all set up the way you want to, and now you’ve got yourself a brand new shiny piece of hardware to work and play on. How do you move that WSL distro over to the new machine?
Luckily, it’s as simple as backup and restore. Really. It’s actually easier than moving your Windows files over.

Creating a backup of WSL works like this:

wsl --export <distribution> <filename.tar>

So if you see that your distro is called Debian after running wsl -l -v, you do this:

wsl --export Debian debian.tar

This takes a while. After it’s done, you copy the tar file over to your new shiny machine and run the following command:

wsl --import <distribution name> <install path> <tar file path>

Or actually:


wsl --import Debian c:\users\YourName\AppData\Local\Packages\Debian c:\temp\debian.tar

That’s it! You now have your full distro moved to a new machine, including all settings and files.

For more info on WSL, see the excellent official Microsoft documentation.

Categories
microsoft software tips

use a single mouse and keyboard to control all your computers

Photo by SJ . on Unsplash

Working from home created a unique set of problems. For example, sharing your mouse and keyboard between your private laptop and your work laptop. Having two keyboards and mice around isn’t an option. It would clutter the desk massively, and I hate clutter.
I don’t like messing with cables either, so my setup is wireless. But having to reconfigure Bluetooth settings every time I want to switch between laptops is equally annoying. I just want to use the keyboard and mouse I always use at home, on both computers, without any special hardware. Some keyboards and mice have buttons to switch between multiple devices, but frankly, even that is quite annoying if you end up having to do that too often.
I just want to use the same keyboard and mouse I always use at home, on both computers, without any special hardware.

I came across a few software solutions to do this, some free and some not, and found this great side project from a Microsoft hackathon called Mouse without Borders. It’s awesome.

All you need to do is install the client software on all your computers (it handles up to 4) configure them as described in the setup guide, and you are set. You can from then on move your mouse from one machine’s screen to the other, as if they were the same computer. You can also copy-paste across machines, or even transfer files if you want.
There are plenty of small tweaks in the options panel to tweak it to work the way you want it to.

I used it with 3 laptops at some point, and it worked great. If you can see all the screens, this is a lot handier than using remote desktop software like TeamViewer or the Windows RDP client. It even handles (re)connecting through VPN’s and works on the login screen (if you hibernated the OS).
Give it a try. It could make your working-from-home life a lot smoother.