Skip to content

Instantly share code, notes, and snippets.

View manualbashing's full-sized avatar
:shipit:

Manuel Batsching manualbashing

:shipit:
View GitHub Profile
@manualbashing
manualbashing / SedExtractImagesFromHtml.sh
Created December 15, 2020 16:14
Use sed to extract all image paths from html file
# The assignment is neede to treat the return value of sed as array
IFS=$'\n' pictures=($(cat /home/mbatsching/git/abtis-cmp/deploy/powerbi/powerbi-update-datasource-credentials.template.html | grep img | sed -E 's/.*img src="([^"]+)".*/\1/g'))
@manualbashing
manualbashing / InstallRequiredModules.ps1
Created December 15, 2020 11:25
Install #powershell script dependencies
[CmdletBinding(SupportsShouldProcess)]
param (
# Path to the script with "requires module" statements
[Parameter(Mandatory)]
[string]
$Path
)
$dependencies = Get-ChildItem $Path |
Select-String -Pattern "^#Requires -Module (@{ ModuleName = '[^']+'; RequiredVersion = '[0-9.]+';? })" |
@manualbashing
manualbashing / blog.md
Last active December 14, 2020 09:40
Use ranger running in wsl from Windows PowerShell session
function Start-Ranger {
    
    [CmdletBinding()]
    param (
        [Parameter()]
        [string]
 $Path = $pwd.Path
@manualbashing
manualbashing / blog.md
Last active December 11, 2020 14:52
Bring the ChristBashTree to PowerShell (with a little help from wsl)

Haven't you always wondered how to bring Sergio Lepore's wonderful ChristBASHTree to PowerShell?

christbashtree

For this to work you need:

  • WSL2 running your favorite linux distribution
  • A PowerShell session (even 5.1 in the default console host if you feel nostalgic)
  • A cup of steaming hot mullet wine
  • The following function:
@manualbashing
manualbashing / blog.md
Last active December 9, 2020 23:20
Update fields in a table storage entity
az storage entity merge --entity PartitionKey=$user RowKey=$user Active="false" --table-name AcademyUser --account-name sajedoxlab8idgyu4918oj
@manualbashing
manualbashing / blog.md
Last active December 9, 2020 23:19
Loop over resources and add to terraform state file

Padding a Number with zeros:

j=12
Academy${(l:3::0:)j}
# ==> Academy012

Use arithmetic evaluation:

@manualbashing
manualbashing / blog.md
Last active March 5, 2023 21:05
A primitive az cli PowerShell wrapper for output parsing and error handling

Az CLI has many advantages over the Az PowerShell modules, but natively it does not integrate very nicely with PowerShell scripts. That is mainly because of two reasons:

  1. The output of AZ CLI needs to be parsed in order to chain commands together
  2. Exception handling within PowerShell's try-catch clauses is not possible

Both problems can be tackled with a very simple wrapper function:

function paz {
@manualbashing
manualbashing / blog.md
Last active December 9, 2020 09:22
Get internet answers in your shell

Add the following to your profile:

 how()
 {
        command="${@//[ ]/+}"
        curl cheat.sh/"$command"
        print -S "`curl -s cheat.sh/\"$command?QT\"`"
 }
@manualbashing
manualbashing / blog.md
Last active December 8, 2020 14:41
Get files from Azure file share recursively

Imagine you have a policy to not keep files on an azure file share that are older than a specified time, lets say 5 days.

The Az.Storage powershell module provides the cmdlet Get-AzStorageFile that can be used to retrive the files from an Azure file share, but there are two shortcomings of this cmdlet, that makes our task a bit difficult (at least in the module's current version when writing this article: 5.1.0):

  • Files are not retrieved recursively. If you hit a folder, you need to call Get-AzStorageFile on this folder, if you want to list it's content.
  • File properties like LastModified are not retrieved. You need to manually call the FetchAttributes() method to fill this attributes with value.

Fortunately PowerShell 7 makes it relatively easy to address both problems with a simple wrapper function:

Ranger Cheatsheet

General

Shortcut Description
ranger Start Ranger
Q Quit Ranger
R Reload current directory
? Ranger Manpages / Shortcuts