Skip to content

Instantly share code, notes, and snippets.

@lzybkr
lzybkr / OneShotEnvVar.ps1
Created October 1, 2019 21:18
Hack to enable FOO=bar syntax in PowerShell
$ExecutionContext.InvokeCommand.CommandNotFoundAction =
{
param([string]$commandName,
[CommandLookupEventArgs]$eventArgs)
if ($commandName -match "(.*)=(.*)") {
$var = $matches[1]
if ($var.StartsWith("get-")) {
$var = $var.Substring(4)
}
@lzybkr
lzybkr / Get-AmIPwned.ps1
Last active August 21, 2023 08:16
Query haveibeenpwned.com
<#
.SYNOPSIS
Reports if your password is pwned by querying haveibeenpwned.com
.DESCRIPTION
Query haveibeenpwned.com to see if a password has appeared in a breach.
The query sends the first 5 characters of the SHA1 hash, so the query should be considered safe and anonymous.
@lzybkr
lzybkr / ClangAutocompletion.ps1
Created May 8, 2019 02:07
# Autocompletion for clang
# Autocompletion for clang - only works in pwsh
Register-ArgumentCompleter -CommandName clang -Native -ScriptBlock {
param($wordToComplete, $commandAst)
if ($wordToComplete.StartsWith('-')) {
$p,$a = $wordToComplete -split '=',2
if ($null -ne $a) {
$clangArg = "$p=,$a"
$type = [System.Management.Automation.CompletionResultType]::ParameterValue
@lzybkr
lzybkr / Invoke-WithEnvironment.ps1
Created October 3, 2019 19:09
Invoke a script block with a temporary environment
param(
[Parameter(Position=0, Mandatory)]
[hashtable]
$private:Environment,
[Parameter(Position=1, Mandatory)]
$private:ScriptBlock
)
end {
@lzybkr
lzybkr / Solve-KenKen.ps1
Last active January 18, 2022 16:41
Using Z3 w/ PowerShell to solve a KenKen puzzle
using namespace Microsoft.Z3
using namespace System.Collections.Generic
using namespace System.Management.Automation.Language
function GetZ3Ast([System.Management.Automation.Language.Ast]$ast, $ctx)
{
$typeName = $ast.GetType().Name
switch ($typeName)
@lzybkr
lzybkr / AnalyzeCurlUsage.ps1
Created August 21, 2016 00:43
Analyze curl in PowerShell scripts
using namespace System.Management.Automation.Language
function AnalyzeCurlUsage
{
param([Parameter(ValueFromPipeline)][Alias('PSPath')][string]$path)
process {
$err = $null
$tokens = $null
<Configuration>
<ViewDefinitions>
<View>
<Name>MatchInfo</Name>
<ViewSelectedBy>
<TypeName>Microsoft.PowerShell.Commands.MatchInfo</TypeName>
</ViewSelectedBy>
<CustomControl>
<CustomEntries>
<CustomEntry>
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class NativeConsoleMethods
{
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern IntPtr GetStdHandle(int handleId);
@lzybkr
lzybkr / ProxyStaticMembers.ps1
Created December 12, 2019 02:00
Create a proxy object for static members of a .Net type
param([type]$Type)
$result = [pscustomobject]@{}
foreach ($property in $type.GetProperties([System.Reflection.BindingFlags]::Public -bor [System.Reflection.BindingFlags]::Static)) {
$name = $property.Name
$params = @{
InputObject = $result
MemberType = 'ScriptProperty'
Name = $property.Name
}
@lzybkr
lzybkr / Out-Default.ps1
Created March 21, 2018 21:38
Quick and dirty timestamp on output
function Out-Default
{
[CmdletBinding(HelpUri='https://go.microsoft.com/fwlink/?LinkID=113362', RemotingCapability='None')]
param(
[switch]
${Transcript},
[Parameter(ValueFromPipeline=$true)]