Skip to content

Instantly share code, notes, and snippets.

View JohnL4's full-sized avatar

John Lusk JohnL4

View GitHub Profile
@JohnL4
JohnL4 / init.sql
Created January 14, 2022 23:16
Sqlite shell settings to make pretty columns, etc.
.mode columns
.headers on
@JohnL4
JohnL4 / CatchLog-ExceptionWithScriptStackTrace.ps1
Created January 7, 2022 16:41
Catch and log an exception (for some value of "log") with a script stack trace for later debugging
# Wrap your entire script in a try-catch block:
try {
# Do stuff
}
catch {
Write-Error ("{0}`n{1}" -f $_, $_.ScriptStackTrace)
}
@JohnL4
JohnL4 / Get-FileNameParts.ps1
Created January 6, 2022 15:53
PowerShell filename parts
$f = ls foo/bar.txt
$f | select Directory,DirectoryName,BaseName,Extension
# More generic than just filesystem provider (e.g., registry keys)
$f | split-path -parent # Container
$f | split-path -leaf # Contained child
@JohnL4
JohnL4 / Mount-ManyDrives.ps1
Last active January 5, 2022 15:39
Mount a bunch of machine drives on drive letters
$machDrives = @(
# [PSCustomObject] @{ mach = "\\P8SisWeb02x\C$"; drive = "X:" },
# [PSCustomObject] @{ mach = "\\P8SisWeb01d\C$"; drive = "E:" },
# [PSCustomObject] @{ mach = "\\P8SisWeb01q\C$"; drive = "Q:" },
# [PSCustomObject] @{ mach = "\\P8SisMnBld01s\C$"; drive = "H:" },
# [PSCustomObject] @{ mach = "\\P8SisMnWeb01s\C$"; drive = "I:" },
# [PSCustomObject] @{ mach = "\\P8SisMnWeb02s\C$"; drive = "J:" },
# [PSCustomObject] @{ mach = "\\P8SisMnWeb03s\C$"; drive = "K:" },
# [PSCustomObject] @{ mach = "\\P8SisBld01t\C$"; drive = "L:" },
# [PSCustomObject] @{ mach = "\\P8SisWeb01t\C$"; drive = "M:" },
@JohnL4
JohnL4 / Get-FileInfo.ps1
Created December 1, 2021 20:38
Get file info including ownership in PowerShell
ls Collabor8*/*Collabor8*.ecube -rec | sel LastWriteTime,Length,@{N="Owner";E={$_.GetAccessControl().Owner}},FullName | ft -au -wr
# Note the nested {} on the expr value (code block?).
@JohnL4
JohnL4 / StupidEntityFrameworkTricks.cs
Last active November 24, 2021 15:37
Stupid Entity Framework tricks
// ---------------------------- Left outer join --------------------------------------------------------------------
var ecpNodes =
(from cp in _db.CustomerProduct
join ec in _db.EnvironmentCustomer on cp.CustomerId equals ec.CustomerId
join ecp in _db.EnvironmentCustomerProduct
on new { ec.EnvironmentId, cp.CustomerId, cp.ProductId }
equals new { ecp.EnvironmentId, ecp.CustomerId, ecp.ProductId }
into gj
// This idiom (group join, then 2nd "from" .DefaultIfEmpty()) results in a left outer join on ECP:
@JohnL4
JohnL4 / Check-Services.ps1
Last active April 18, 2024 21:02
Check on certain windows processes & services
get-service sisense* | select Status,Name,StartType | ft -au -wr
Get-CimInstance -ComputerName p8SisMnWeb01s,p8SisMnWeb02s,p8SisMnWeb03s -ClassName Win32_Service `
| ? {$_.Name -match 'sisense.*'} `
| select PSComputerName,State,ProcessID,Name,StartMode,StartName `
| sort PSComputerName,Name `
| ft -au -wr
ps *Sisense*,*Elasticube* | select ProcessName,StartTime,Path,Description | sort StartTime -desc | ft -au -wr
@JohnL4
JohnL4 / swap-capslock-ctrl.ps1
Last active October 21, 2021 02:38
PowerShell script to permanently swap caps-lock and control by registry hack
<# Writes a registry entry to swap caps lock and left control permanently. This affects all users and the login screen, and doesn't
require any other software to run after this (e.g., PowerToys). Requires a reboot after this script is run to take effect.
Courtesy of https://www.mavjs.org/post/swap-ctrl-and-capslock-on-windows/
#>
$hexified = "00,00,00,00,00,00,00,00,03,00,00,00,1d,00,3a,00,3a,00,1d,00,00,00,00,00".Split(",") | % { "0x$_"};
$kbLayout = 'HKLM:\SYSTEM\CurrentControlSet\Control\Keyboard Layout';
New-ItemProperty -Path $kbLayout -Name "Scancode Map" -PropertyType Binary -Value ([byte[]]$hexified)
@JohnL4
JohnL4 / awk-sort-uniq.ps1
Created May 26, 2021 15:21
awk | sort -unique in PowerShell
cat scheduled-task-2021-05-25.log `
| sls 'Probe foo_.*' `
| % {$_.Matches[0].Captures[0].Value} `
| sort -uniq
<#
Matches is array of matches in line (could be multiple, for repeated occurrences of the pattern).
Groups is regexp capture groups, [0] is entire matched string (I guess)
#>
@JohnL4
JohnL4 / Synthetic-Properties-Calculated-Properties.ps1
Created May 25, 2021 14:24
Synthetic name-value properties in powershell are CALCULATED properties
# Hard to search when you don't know the right keywords.
get-service `
| select @{name="DependentServiceName";expr={$_.Name}} -expand ServicesDependedOn `
| select DependentServiceName,Name `
| sort DependentServiceName,Name `
| ft -au -wr