Skip to content

Instantly share code, notes, and snippets.

View JohnL4's full-sized avatar

John Lusk JohnL4

View GitHub Profile
@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",
@JohnL4
JohnL4 / Get-ProcessesWithCommandLines.ps1
Last active November 3, 2023 13:50
Get processes WITH COMMAND LINES (must run as admin)
get-process node `
| select id,starttime,name,
@{ Name="CommandLine";
Expr={ $filter = "ProcessId = {0}" -f $_.Id; (Get-CimInstance Win32_Process -filter $filter).CommandLine }} `
| ? {$_.CommandLine -match ' gulp\.js Build_'} `
| sort starttime `
| ft -au -wr
# Omit the filter on gulp.js for a fuller result.
@JohnL4
JohnL4 / Remove-OldSisenseLogs.ps1
Created December 12, 2022 14:56
Sisense log cleanup
# Old log file cleanup
cd C:\ProgramData\Sisense\PrismWeb\Logs
ls *.log.* `
| ? {$_.Extension -match '^\.[0-9]+$' } `
| ? {$_.LastWriteTime -lt (Get-Date).AddMonths(-1)} `
| rm -vb
@JohnL4
JohnL4 / Get-DistributionTime.ps1
Last active September 29, 2022 13:49
Get the time it takes to distribute cubes out of the log files generated by each distribution job
# Skip short files (assumed error outputs), files that contain "already begin built".
# Parse timestamp out of filename (YYYY-MM-DD-HHMMSS).
# Drop first component of "cube name" (the "Build_" part of "Build_Cube_Name")
ls *-2022-09-*.txt `
| ? {$_.Length -gt 1000} `
| ? {-not (sls -list 'already being built' $_)} `
| sls "Finished '(.*)' after (([0-9\.]+) [a-zA-Z]+)" `
| sel -fir 1000 `
| sel @{Name="Timestamp";Expr={if ($_.Filename -match "(?<timestamp>20\d+-\d+-\d+-\d+)") {$Matches.timestamp} else {""}}} `
@JohnL4
JohnL4 / Get-DbConnections.ps1
Created September 26, 2022 21:33
Get d/b connections from Sisense elasticubes
cd N:\SisenseBuilds\Elasticubes
ls */*.xml `
| % {[xml] $xml = cat $_;
# $xml | gm
$xml.Cloud["Cloud.Tables"].Tables.CloudTable `
| % {
# $_ | gm
[PSCustomObject] @{
Server = $_["CloudTable.ConnectionProperties"].SqlServerConnectionProperties.Server;
Database = $_["CloudTable.ConnectionProperties"].SqlServerConnectionProperties.Database