The WSL Onion

Calling powershell.exe from PowerShell inside WSL

The other day I was playing around in WSL with a colleague of mine and we did this: # We start out in WSL Debian and enter PowerShell Core marco@box:~$ pwsh PowerShell 7.2.2 Copyright (c) Microsoft Corporation. https://aka.ms/powershell Type 'help' to get help. # Then we get the major version of the active PowerShell session PS /home/marco> ($PSVersionTable.PSVersion).Major 7 # Then we call powershell.exe and get a completely different version! [Read More]

The Null-Coalescing Operator

A closer look at the null-coalescing operator and how it can help us in writing smarter, more concise scripts.

Today we take a closer look at the null-coalescing operator and how it can help us in writing smarter, more concise scripts. PS> $userDate ?? (Get-Date -Format "yyyy-MM-dd") 2021-02-14 PS> $userDate = Get-Date -Year 2020 -Format "yyyy-MM-dd" PS> $userDate ?? (Get-Date -Format "yyyy-MM-dd") 2020-02-14 How it works We can write some simple code to visualize what the null-coalescing operator is doing function nullCoalescingOperator ($leftValue, $rightValue) { if ($null -eq $leftValue) { return $rightValue } else { return $leftValue } } Usually we will want to assign the resulting value to a variable. [Read More]

Delete Your Old VMware Snapshots

For the love of Pete, please delete your old snapshots regularly! Old snapshots have caused incidents and even outages more than once in my career and it is really easy to preemptively look for them and get them removed before anything happens. Why To put it plainly, they can cause issues - like 03:00 in the morning pager alert issues and additionally eat up storage space like crazy. Degraded performance of the VM having the snapshot Degraded performance to full outages for other VMs on the same data store due to rapidly increasing snapshot sizes VMware recommends a series of steps to reduce risk when using snapshots: [Read More]

How To Find With PowerShell

Finding files, do I even need to describe how important this is?

Finding files, do I even need to describe how important this is? From finding README files in a project $ find . -name README* ./app/README.md ./README.md ./ipsum.md/README.md to locating that one config file you never can remember the location of. $ find . -name dnsconf.yaml ./app/module1/dnsconf.yaml .notice{padding:18px;line-height:24px;margin-bottom:24px;border-radius:4px;color:#444;background:#e7f2fa}.notice p:last-child{margin-bottom:0}.notice-title{margin:-18px -18px 12px;padding:4px 18px;border-radius:4px 4px 0 0;font-weight:700;color:#fff;background:#6ab0de}.notice.warning .notice-title{background:rgba(217,83,79,.9)}.notice.warning{background:#fae2e2}.notice.info .notice-title{background:#f0b37e}.notice.info{background:#fff2db}.notice.note .notice-title{background:#6ab0de}.notice.note{background:#e7f2fA}.notice.tip .notice-title{background:rgba(92,184,92,.8)}.notice.tip{background:#e6f9e6}.icon-notice{display:inline-flex;align-self:center;margin-right:8px}.icon-notice img,.icon-notice svg{height:1em;width:1em;fill:currentColor}.icon-notice img,.icon-notice.baseline svg{top:0.125em;position:relative} Info All commands in this post are run against a open source repository to allow you to reproduce output & play with the commands. [Read More]

Super-Charged Cmdlet Aliases

It works like magic, but it works - Bring your aliasing to a whole other level!

Backstory I recently wrote a post about PowerShell Aliasing for the folks over at ScriptRunner. The one issue I had while researching for this post was this: You can not create a alias for a function and overwrite one of it’s parameters at the same time while keeping nice features like tab-complete. For a concrete example of this visit please look at the original post. At the end of the post I asked if anyone had a solution to this problem - and the internet delivered! [Read More]

Why You Want To Install PowerShell On Windows

Why installing PowerShell on Windows may actually be a good idea

Q: Wait a minute! Don’t I already have PowerShell? A: Yes. However you may want to continue reading because things are never as simple as they appear. The PowerShell version you have installed on Windows 10 is 5.X, Microsoft even says so in their own documentation. On the initial release of Windows 10, with automatic updates enabled, PowerShell gets updated from version 5.0 to 5.1. If the original version of Windows 10 is not updated through Windows Updates, the version of PowerShell is 5. [Read More]

Collections and Randomization

Randomize Collections in PowerShell

Randomizing a collection is useful in a variety of situations and in most languages it is fairly straight forward. For example Python my_list = range(1,20) random.shuffle(my_list) PHP $numbers = range(1, 20); shuffle($numbers); In PowerShell this works a bit differently. Before 7.1 PS> @(1,2,3) | Sort-Object {Get-Random} 3 1 2 PS> "abc".ToCharArray() | Sort-Object {Get-Random} a c b Well, how does that work? Sort-Object allows us to sort the incoming collection based on a script block. [Read More]

How To Grep With PowerShell

It's just like Bash, but more powerful!

Filtering data, from log or config files to data returned by an api, is an important operation to remove noise from it and make further analysis possible. In Bash most would gravitate towards grep, egrep or awk. Classic examples would be looking at your history $ history | grep ssh ssh marco@0.0.0.0 ssh deploy@0.0.0.0 ssh admin@0.0.0.0 looking for errors in a log $ cat /var/log/app.log | grep error 2020-09-30 09:01:17 error: twitter api responded with 403 2020-09-30 09:16:05 error: secret plan to take over the world failed grep $ grep api app. [Read More]