Scripts

General

How to find and delete duplicate files within the same directory?
I want to find duplicate files, within a directory, and then delete all but one, to reclaim space. How do I achieve this using a shell script? For example: pwdfolder Files in it are: log.bkplog

How to update WSL DNS settings when using a VPN

Problem: When connecting with a VPN, WSL's gateway setting does NOT get updated. "apt update" will stop working, for example. These instructions depend on your VPN utility executing a script as soon as the link is up.

  1. In WSL edit /etc/wsl.conf with the following, then restart it

[network]
generateResolvConf = false

  1. Install BurntToast from Powershell:

Install-Module BurntToast

  1. In WSL create setdns

echo "nameserver $1" >/etc/resolv.conf
Then make it executable
chmod u+x setdns

  1. In windows create setdns.ps1

# Get VPN Gateway
$gw = Get-WmiObject -Class Win32_IP4RouteTable |
where { $.destination -eq '0.0.0.0' -and $.mask -eq '128.0.0.0'} |
Sort-Object metric1 | select nexthop -ExpandProperty nexthop
# Run script that sets resolv.conf
wsl ~/setdns $gw
New-BurntToastNotification -Text "Set WSL Gateway to $gw"
exit

  1. In the VPN application, find the "VPN Up" event handler and set it to

powershell.exe -noexit "& ""C:\PATH\setdns.ps1"""

Where PATH is the directory containing setdns.ps1

Note: Typically the VPN app will display the current gateway being used. In step 4, if no gateway was found (or it found your local gateway instead of the VPN's gateway), the mask may need to be '0.0.0.0' instead of '128.0.0.0', depending on your VPN service.  You can double check by looking at the output of the following command (in Windows), and match to your current gateway, e.g. 1.2.3.4

netstat -rn | find "1.2.3.4"

Look at the line where the gateway is the third column - the mask is in the second column.

Now every time you connect, the gateway will be updated in WSL and a toast will display with the new value.

References

Using Toast notifications in Powershell
Notifications can be quite a bit more than just those annoying pop-ups you see down by your system clock from time to time. When created using Powershell, you can get an incredible amount of functionality out of them.
How to get the default gateway from powershell?
If a computer has multiple gateways, how can I determine which is the default gateway using the PowerShell?
Custom resolv.conf being replaced by generated file, even with comment removed. · Issue #1908 · microsoft/WSL
A brief description Custom /etc/resolv.conf overwritten, even when "# This file was automatically generated by WSL. To stop automatic generation of this file, remove this line." is remove...

Powershell

Unable to Install NuGet on newly installed Windows Server 2016 as Administrator?
I’m using Windows Server 2016 and ADFS 4.0.As part of the troubleshooting AD Federation service, which requires me to install the NuGet and the Modules as per https://adfshelp.microsoft.com/