Skip to content

Instantly share code, notes, and snippets.

View JohnL4's full-sized avatar

John Lusk JohnL4

View GitHub Profile
@JohnL4
JohnL4 / parse-remote-log-file.ps1
Last active February 25, 2025 18:14
Use a remote session to pull info out of a remote file
# Casting hashtable to PSCustomObject must be on one line, afaict.
# -split uses regexp, so '|' must be escaped
icm { cd f:\DataPump/logs; cat ImportData2Snowflake_20250224.log, ImportData2Snowflake_20250225.log | sls '\| Destination:' | % {
$arr = $_ -split ' \| ';
[PSCustomObject] @{TimeStamp = get-date $arr[0]; Note = $arr[5]; FieldCount = $arr.Count; FullText=$_ }
} | ? {$_.TimeStamp -ge (get-date '2025-02-24T22:00+00:00')} } -session $ses | select TimeStamp,Note | ft -au -wr
# Could also use just 'cat -tail 200 -wait' to tail the file.
@JohnL4
JohnL4 / Get-CurrentScriptPath.ps1
Created January 23, 2025 15:04
Figure out the path to the currently-executing PowerShell script
<#
.SYNOPSIS
Figure out the path to the currently-executing script so we can do stuff like dot-source other PS snippets or invoke other scripts
in the same directory.
.NOTES
Can use one of:
- $MyInvocation -- Looks like this is really NOT the way to go. Seems a bit complicated to get the right info out of here.
Could be useful for a variety of info, but not the path to the current script.
- $PSCommandPath
@JohnL4
JohnL4 / org-todo-sort.el
Last active January 6, 2025 20:08
Sort org-mode Todo items better (so high-priority, more-recently-scheduled items float to the top).
(defun my-org-agenda-todo-sort (a b)
"Function should only sort TODO items; since I can't return ``unsortable'' for things that don't compare, I just
return 0 and hope for the best. Seems to be working so far. Higher-priority and more-recently-scheduled items
have higher urgency."
(if (string-match "\\(Sched\\.\\s-*\\([0-9]+\\)x\\|Scheduled\\):\\s-+\\S-+ \\[#\\([ABC]\\)\\]" a)
(let ((a-sched-days (string-to-number (if (null (match-string 2 a)) "0" (match-string 2 a))))
(a-priority (match-string 3 a)))
(if (string-match "\\(Sched\\.\\s-*\\([0-9]+\\)x\\|Scheduled\\):\\s-+\\S-+ \\[#\\([ABC]\\)\\]" b)
(let ((b-sched-days (string-to-number (if (null (match-string 2 b)) "0" (match-string 2 b))))
(b-priority (match-string 3 b)))
@JohnL4
JohnL4 / stdin-as-plain-text-in-powershell.txt
Created November 20, 2024 20:54
How to get a PowerShell pipeline to emit plain text, not objects
When invoking from PowerShell, you can pass a stream of file names in a pipeline as follows:
ls VP_CLIENT/*.sql -rec | select -expand FullName | py ../InternalTools/Export-VP_DDL/fix-anonymous-sequences.py
(The ``select -expand`` is key -- The list of filenames will appear on this script's stdin as lines of text.)
@JohnL4
JohnL4 / dynamic-interpolated-sql-string.sql
Created May 31, 2024 14:41
A cute way to build dynamic sql by interpolating a string (#interpolation)
DECLARE @message VARCHAR(MAX) = 'Some {item} with {count} {collection}.';
-- Via https://stackoverflow.com/a/77054331/370611
-- Recursive expression, frowned upon by https://learn.microsoft.com/en-us/sql/t-sql/language-elements/select-local-variable-transact-sql?view=sql-server-ver16#c-antipattern-use-of-recursive-variable-assignment
-- But... it does seem to work.
SELECT @message = REPLACE(@message, SearchText, ReplaceText)
FROM ( VALUES -- Table constructor (literal), anonymous table, essentially.
('{item}', 'text string'),
('{count}', '3'),
('{collection}', 'variables')
@JohnL4
JohnL4 / flip-executable-bit-on-script-in-git.ps1
Last active February 7, 2024 15:22
Flip the executable bit on a shell script with git command line
git update-index --chmod=+x myscript.sh
# May be required to get your bash script to run in a Linux Azure pipeline agent; watch for "not executable" errors.
# (This is Powershell, but who cares, it's a command line.)
# From https://www.aligrant.com/web/blog/2022-04-20_execute_permission_on_chained_shell_scripts_through_azure_devops
@JohnL4
JohnL4 / __tsql-hex-conversion-README.md
Last active August 16, 2023 13:47
In MS SQL Server 2016 T-SQL, convert large hexadecimal to decimal (numeric(38)) and back

This is some code I wrote in a doomed attempt to deal with some long hex strings in some of our database objects.

The effort was doomed, but this code seems to work fine.

The strings I was working with were fixed 24-hex-digit strings, but you could probably make this variable length (so long as the values fit in numeric(38) values).

This was for Sql Server 2016, so I didn't have string_split() or string_agg(). (String_split doesn't take empty string or null for separator.)

@JohnL4
JohnL4 / DataReader2DataSet.cs
Created May 31, 2023 20:09
Create a DataSet from a multi-result d/b return value
var reader = cmd.ExecuteReader();
var ds = new DataSet();
while (! reader.IsClosed)
{
var dt = ds.Tables.Add();
dt.Load( reader );
}
@JohnL4
JohnL4 / Get-ServicePid.ps1
Last active May 10, 2023 13:21
Find the process id (pid) of a Windows service
$ServicePID = (get-wmiobject win32_service | where { $_.name -eq 'service name'}).processID
# https://social.technet.microsoft.com/Forums/en-US/43fa0ba5-f5b6-43b7-a10b-4957bc4a5f19/how-to-search-a-pid-with-service-name-and-then-task-kill-it?forum=winserverpowershell
@JohnL4
JohnL4 / rust-analyzer-inlay-settings.jsonc
Last active April 4, 2023 21:00
How to fix rust-analyzer inlay hints. (Actually, all inlay hints.)
// Edit your settings.json, probably located at C:\Users\j6l\AppData\Roaming\Code\User\settings.json
// The following is a snippet. Note the background is completely transparent (trailing "0").
// (Note the actual VS Code settings can't have comments.)
"workbench.colorTheme": "Default Light+",
"breadcrumbs.enabled": true,
"workbench.colorCustomizations": {
"[Default Light+]": {
"editorBracketMatch.border": "#00ffff",
"editorBracketMatch.background": "#00ffff80",