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! PS /home/marco> (powershell.
[Read More]
Local User Management Polyfill
Older Versions of Windows 10 and all Windows Server 2012 systems have no local user management commandlets.
This is a working polyfill to allow scripts using local user management to run on old systems.
Linked: Powershell PSDefaultParameterValues
A deep dive into PowerShell parameter defaults
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 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]
Linked: Powershell Aliasing
A deep dive into aliasing with PowerShell
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]