Skip to content

Instantly share code, notes, and snippets.

View JohnL4's full-sized avatar

John Lusk JohnL4

View GitHub Profile
@JohnL4
JohnL4 / Search my gists.md
Last active May 6, 2021 16:11 — forked from santisbon/Search my gists.md
How to search gists

Enter this in the search box along with your search terms:

Get all gists from the user santisbon.
user:santisbon or user:@me

Find all gists with a .yml extension.
extension:yml

@JohnL4
JohnL4 / Strip-Nulls.ps1
Created May 5, 2021 18:13
Strip null characters from a file
# From https://stackoverflow.com/a/9866769/370611
# (Obviously, make sure you're not dealing with a Unicode file.)
foreach ($f in (Get-ChildItem *.log)) {
(Get-Content $f) -replace "`0", "" | Set-Content $f
}
@JohnL4
JohnL4 / tabs-to-table.el
Created April 23, 2021 15:25
Emacs function that uses org-mode to convert a block of text containing hard tabs (such as copy-and-paste results from SQL Server) into an aligned table.
(defun tabs-to-table ()
"Turns a block of tab-delimited text (either the entire buffer or the region, if active) into an org-mode table.
Note that this function will operate on the lines the region touches, so it may stray outside the region."
(interactive)
(require 'org-table)
(save-excursion
(let* ((char-begin (if (use-region-p) (region-beginning) (point-min)))
(char-limit (if (use-region-p) (region-end) (point-max)))
end-marker
)
@JohnL4
JohnL4 / Schedule-PowerShell-Script.txt
Last active December 7, 2021 20:32
Notes on scheduling a PowerShell script with Windows Task Scheduler
The program to run is: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe (or wherever the PS executable is on your system)
The arguments are: -NoProfile -File Run-ScheduledScript.ps1
Run-ScheduledScript is where you redirect all your output streams to a log file using the '*>' PowerShell redirect operator
(or some variant thereof).
Maybe "*>&1" to merge everything to the SUCCESS stream, followed by "| Out-File -width 200 -encoding oem" or something equally creative.
@JohnL4
JohnL4 / Process-Pipeline.ps1
Last active May 8, 2025 18:20
awk-like BEGIN and END processing of a pipeline in PowerShell; adding -Verbose and -WhatIf support via CmdletBinding; Template for a whole script.
<#
.SYNOPSIS
Process pipeline junk
.DESCRIPTION
Longer description of script/cmdlet/function goes here.
.EXAMPLE
Sample usages, if helpful. Can be repeated for multiple examples.
.NOTES
[Stuff of interest not necessarily required as part of Description section. Could maybe put futures/todos here.
@JohnL4
JohnL4 / Set-RemoteSessionTimeoutOption.ps1
Created February 23, 2021 19:02
Change idle timeout for remote PowerShell sessions
# Six hours.
$PSSessionOption.IdleTimeout = New-Object TimeSpan 6,0,0
@JohnL4
JohnL4 / Make-SelfSignedCertificate.ps1
Last active February 18, 2021 15:00
Make, export and import a self-signed certificate for signing PowerShell scripts
# From https://medium.com/the-new-control-plane/generating-self-signed-certificates-on-windows-7812a600c2d8
# ----------------------------------------------------- Creation -----------------------------------------------------
$cert = New-SelfSignedCertificate `
-FriendlyName "John Lusk Self-signed" `
-KeyFriendlyName "John Lusk Self-signed Private Key" `
-NotAfter (get-date).AddYears(10) `
-Subject "John Lusk" `
-type CodeSigningCert `
@JohnL4
JohnL4 / Self-Elevate.ps1
Last active February 18, 2021 15:07
Snippet of PowerShell to self-elevate a script
# --------------------------------------- Self-elevate the script if required. ---------------------------------------
#
# Physically include this in the client script, dot-sourcing will not work.
# From https://blog.expta.com/2017/03/how-to-self-elevate-powershell-script.html, but I made a few modifications myself.
# TODO: try making this a function.
try {
if (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
if ([int](Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber) -ge 6000) {
Write-Verbose ("cmd type: {0}" -f $MyInvocation.MyCommand.CommandType)
@JohnL4
JohnL4 / Send-ArgumentsToInvokeCommand.ps1
Last active June 8, 2023 18:46
How to send arguments to Invoke-Command (using a parameterized script block) to invoke a command on a remote server (PS Remoting)
# The key is a parameterized script block.
$expected = Invoke-Command -ComputerName $Machine -script {
param($path) Get-ChildItem $path -rec | Measure-Object -prop length -sum } -args $CubePath
@JohnL4
JohnL4 / git-subtree.txt
Last active February 9, 2021 16:37
'git subtree' to merge separately-developed project into a bigger one.
(PowerShell)
> h | oss | sls subtree
316 git-subtree --help
317 git help subtree
326 git subtree add -P LoadTesting/k6 s:\JLusk-S\k6\.git
328 git subtree add -P LoadTesting/k6 s:\JLusk-S\k6\.git
329 git subtree add -P LoadTesting/k6 s:\JLusk-S\k6\.git HEAD