Skip to content

Instantly share code, notes, and snippets.

View glombard's full-sized avatar

Gert Lombard glombard

View GitHub Profile
@glombard
glombard / Get-ScriptPath.ps1
Created May 2, 2014 10:45
Use different techniques to determine a PowerShell script's directory: try `$PSScriptRoot`, `$MyInvocation.MyCommand.Path`, `$ExecutionContext.SessionState.Module.Path` and `$PWD`.
function Get-ScriptPath {
$scritDir = Get-Variable PSScriptRoot -ErrorAction SilentlyContinue | ForEach-Object { $_.Value }
if (!$scriptDir) {
if ($MyInvocation.MyCommand.Path) {
$scriptDir = Split-Path $MyInvocation.MyCommand.Path -Parent
}
}
if (!$scriptDir) {
if ($ExecutionContext.SessionState.Module.Path) {
$scriptDir = Split-Path (Split-Path $ExecutionContext.SessionState.Module.Path)
@glombard
glombard / ClipboardNotification.cs
Created December 16, 2013 12:34
Monitor Clipboard changes in C# using AddClipboardFormatListener / WM_CLIPBOARDUPDATE. See Clipboard class: http://msdn.microsoft.com/en-us/library/system.windows.clipboard(v=vs.110).aspx
// from: http://stackoverflow.com/questions/2226920/how-to-monitor-clipboard-content-changes-in-c
/// <summary>
/// Provides notifications when the contents of the clipboard is updated.
/// </summary>
public sealed class ClipboardNotification
{
/// <summary>
/// Occurs when the contents of the clipboard is updated.
/// </summary>
@glombard
glombard / add-credential.bat
Created May 1, 2014 10:59
Use cmdkey to add a Git credential to the Windows Credential Manager
# to add a new Git credential to the Credential Manager:
cmdkey /generic:LegacyGeneric:target=git:https://github.com /user:username /pass:"mypassword"
# to list all credentials:
cmdkey /list
@glombard
glombard / reference-markdown-metadata-from-jinja-template.py
Last active May 30, 2023 09:19
How to use Markdown as a filter in a Jinja2 template, and then extract the Markdown Meta property directly from the template. Assuming you want to use the Meta-data value before rendering the converted Markdown content (e.g. in the html head), the trick is to render the markdown first, save it to a variable (html_content in this example) using a…
from pprint import pprint
import jinja2
import markdown
HTML_TEMPLATE = """{% macro get_html() %}
{{ content | markdown }}
{% endmacro %}
{% set html_content = get_html() %}
Title from Markdown meta-data: {{ get_title() }}
@glombard
glombard / setup-wifi.sh
Created July 22, 2014 18:57
Install wifi drivers for Ubuntu / Lubuntu 14.04 on Acer Aspire 5755G (Broadcom BCM43227 network controller)
# Determine wireless device model (manufacturer 14e4 for Broadcom):
lspci -vvnn | grep 14e4
# Install Broadcom STA driver for BCM43227:
sudo apt-get update
sudo apt-get install --reinstall linux-headers-generic build-essential dkms bcmwl-kernel-source
sudo modprobe -r b43 ssb wl brcmfmac brcmsmac bcma
sudo modprobe wl
# Connect (press Fn+F3 to enable wifi if necessary first):
@glombard
glombard / create-WinXP-vm.ps1
Last active January 11, 2023 03:14
Create Windows XP image on Hyper-V
# Enable RDP on host:
Set-ItemProperty -Path "HKLM:\System\CurrentControlSet\Control\Terminal Server" -Name fDenyTSConnections -Type DWord -Value 0
netsh advfirewall firewall set rule group="remote desktop" new enable=Yes
# Get sublime text editor:
$c=new-object Net.WebClient
$c.DownloadFile('http://c758482.r82.cf2.rackcdn.com/Sublime%20Text%20Build%203047%20x64%20Setup.exe',"$env:TEMP\sublime.exe")
cd $env:TEMP
.\sublime.exe /SILENT
@glombard
glombard / combine.py
Created November 24, 2014 00:22
Merging 4 images into one with Python and PIL/Pillow
# Combine multiple images into one.
#
# To install the Pillow module on Mac OS X:
#
# $ xcode-select --install
# $ brew install libtiff libjpeg webp little-cms2
# $ pip install Pillow
#
from __future__ import print_function
@glombard
glombard / new_obj.py
Created December 9, 2014 19:57
Python anonymous object
# Sometimes it's handy to create small anonymous objects instead of explicitly defining a class for it, especially while prototyping.
def new(name, data):
return type(name, (object,), data)
person = new('Person', { 'name': 'Joe', 'age': 30 })
print(person.name)
@glombard
glombard / jenkins-build-duration-graph.py
Last active July 5, 2022 02:04
Plot a graph of Jenkins job build durations over time
import logging
import datetime
import time
from dateutil import tz
import matplotlib.pyplot as plt
import pytz
import requests
logging.captureWarnings(True)
@glombard
glombard / curl_exit_codes.json
Created January 13, 2016 02:24
Python script to generate json file and shell script with curl exit codes
[
{
"description": "All fine",
"code": 0,
"name": "CURLE_OK",
"long_description": "All fine. Proceed as usual.\n"
},
{
"description": "The URL you passed to libcurl used a protocol that this libcurl does not support",
"code": 1,