Skip to content

Instantly share code, notes, and snippets.

View JohnL4's full-sized avatar

John Lusk JohnL4

View GitHub Profile
@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 / parse-xml.ps1
Last active May 18, 2022 15:50
Extract data from multiple XML files, composing into PSCustomObjects which can then be sorted uniquely.
# ls *.xml | % {[xml] $xml = cat $_; $xml.Cloud["Cloud.Tabls"].Tables.CloudTable | % { [PSCustomObject] @{ Server = $_["CloudTable.ConnectionProperties"].SqlServerConnectionProperties.Server; Database = $_["CloudTable.ConnectionProperties"].SqlServerConnectionProperties.Database}}}
ls .\*.xml `
| % {[xml] $xml = cat $_; # Cast to [xml] magically parses XML text into an XmlElement (document).
# $xml | gm # Then you just have to navigate the various children and attributes.
$xml.Cloud["Cloud.Tables"].Tables.CloudTable `
| % {
# $_ | gm
[PSCustomObject] @{ # Cast to [PSCustomObject] "magically" turns a hash into an object w/hash key-value pairs as properties.
Server = $_["Clou
@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 / 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 / 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.
ls $src | ? {$_.LastWriteTime -ge (Get-Date).AddMinutes( -5)} | cp -dest $dest -for -pass
@JohnL4
JohnL4 / literals.ps1
Last active May 26, 2022 18:51
PowerShell syntax for complex literals (array, hash), custom objects and calculated properties
# Hash table syntax is @{ name = value; name = value; ... } -- Curly braces and semicolons
$map = @{ key1 = "value1"; key2 = 3.14 }
echo $map.key1 # "value1"
echo $map["key2"] # 3.14
# Coerce it to a new object
[PSCustomObject] $map
# Btw, you can SELECT a "calculated property" (or synthetic property, if that's how your brain works):
@kingspp
kingspp / logging.py
Created April 22, 2017 07:14
Python Comprehensive Logging using YAML Configuration
import os
import yaml
import logging.config
import logging
import coloredlogs
def setup_logging(default_path='logging.yaml', default_level=logging.INFO, env_key='LOG_CFG'):
"""
| **@author:** Prathyush SP
| Logging Setup
@JohnL4
JohnL4 / Get-WindowsServicesAndTasksWithHardcodedPasswords.ps1
Last active August 23, 2022 18:05
PowerShell command to find logon account of Windows services; also cmd line to dump attributes of scheduled tasks to CSV file
<#
This might help in finding that pesky windows service that's always locking you out when you change your password and reboot.
#>
# ft is Format-Table; -auto is auto column width; -wrap is wrap text if necessary
Get-WmiObject win32_service | sort startname,startmode,state,displayname | select StartMode,State,StartName,DisplayName | ft -auto -wrap
# Or you can select only certain services.
# '?' is 'where' alias; -match uses regular expressions; -not is (obviously) a 'not' operator.
Get-WmiObject win32_service | ? {-not ($_.state -match 'running')} | sort startname,displayname | select StartMode,State,StartName,DisplayName | ft -auto -wrap
@JohnL4
JohnL4 / Make-Zips.ps1
Last active February 7, 2020 16:31
PowerShell cmd to zip up a directory while excluding multiple subdirectories
ls -rec | ? {-not ($_.FullName -match '\\(NUnitConsole|TestRun)\\')} | write-zip -output "$(datefn).zip"
#### If you want to get fancier...
# (1) Make a list of files touched in the last hour.
$fs = ls c:\work\sxa\18.4cu2\Projects\VisitRecord\VRDotNet\Portal\SXA.VR.Portal\bin `
| ? {$_.LastWriteTime -ge (Get-Date).AddMinutes(-60)}
# (2) Add to it. The '+=' operator actually makes a new Array by copying the old Array + the entry(ies).