Skip to content

Instantly share code, notes, and snippets.

@gpduck
gpduck / Get-HexValue.ps1
Last active December 21, 2015 12:49
Returns the hex value of any character using the specified encoding (default is Unicode).
function Get-HexValue {
param(
[Parameter(Mandatory=$true)]
[Char]$Character,
[ValidateSet("ASCII","BigEndianUnicode","Default","Unicode","UTF32","UTF7","UTF8")]
[String]$Encoding = "Unicode"
)
$E = [Text.Encoding]::$Encoding
$Bytes = $E.GetBytes($Character)
@gpduck
gpduck / gist:6437000
Created September 4, 2013 13:32
This allows plain text passwords to be supplied in DSC configurations. Be aware it puts the password in plain text in c:\windows\system32\configuration\current.mof (and possibly other places)
$CfgData =@{
AllNodes =@(
@{
NodeName = $Env:Computername;
PSDscAllowPlainTextPassword = $true;
}
)
}
configuration TestFile {
@gpduck
gpduck / gist:6546064
Created September 13, 2013 01:59
Start a process and manipulate stdin in PowerShell
try {
$MyProcess = New-Object System.Diagnostics.Process
$MyProcess.StartInfo.FileName = "c:\MyProcess.exe"
$MyProcess.StartInfo.Arguments = "arguments for process"
$MyProcess.StartInfo.UseShellExecute = $false
$MyProcess.StartInfo.RedirectStandardInput = $true
$MyProcess.Start()
$StdIn = $MyProcess.StandardInput
@gpduck
gpduck / gist:7035635
Last active December 25, 2015 20:29
Sits in a loop listening for keyword actions (defined in the switch from line 72 to 108). You must have Windows Speech Recognition configured and running before you run this script.
[Reflection.Assembly]::LoadWithPartialName("System.Speech") > $null
function InitGrammar {
param(
$Attention,
$CommandArray
)
$choices = new-object System.Speech.Recognition.Choices($CommandArray)
@gpduck
gpduck / gist:7329707
Created November 6, 2013 02:01
Test functions for missing help and missing process blocks if they take pipeline input. Get-Command -Module Whatever | Test-Function
function Test-Function {
param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[ValidateNotNull()]
[System.Management.Automation.FunctionInfo]$Function,
[switch]$IgnoreWarnings,
[switch]$IgnoreMissingHelp
)
@gpduck
gpduck / Start-Webserver.ps1
Last active January 12, 2017 12:59
PowerShell Web Server Framework
<#
.SYNOPSIS
Start a web server that will route requests to a series of script blocks as defined by the -Routes parameter.
.DESCRIPTION
Starts a single-threaded web server and responds to requests by executing the script blocks that are
defined as routes on the command line.
.NOTES
Copyright 2013 Chris Duck
@gpduck
gpduck / Get-ClipboardAsCsv.ps1
Created January 31, 2014 03:56
A function that pulls your clipboard data as CSV and converts it to objects (try copying a block from Excel).
function Get-ClipboardAsCsv {
param(
[Parameter(Mandatory=$false)]
[char]$Delimiter,
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string[]]$Header
)
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") > $null
$ms = [Windows.Forms.clipboard]::getdata([Windows.Forms.DataFormats]::CommaSeparatedValue)
@gpduck
gpduck / ConvertTo-AsciiArt.ps1
Created May 2, 2014 03:30
ConvertTo-AsciiArt
<#
.DESCRIPTION
Converts an image to ASCII art.
.PARAMETER Path
The path to an image file to convert to ASCII. This can be any image format supported by System.Drawing.Bitmap.
.PARAMETER Uri
The URI for an image to download and convert to ASCII. This can be any image format supported by System.Drawing.Bitmap.
@gpduck
gpduck / IseFileChangeNotifications.ps1
Created May 6, 2014 05:05
A crude attempt at providing file change notifications for ISE.
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.VisualBasic") > $Null
$Watchers = @{}
$TabCollectionChangedEvent = {
$EventArgs.NewItems | %{
Register-ObjectEvent -InputObject $_.Files -EventName CollectionChanged -Action $FileCollectionChangedEvent
}
}
$FileCollectionChangedEvent = {
@gpduck
gpduck / UnpackAb.groovy
Created May 13, 2014 07:51
Unpack an Android adb backup using groovy
import java.util.zip.*
String backupFile = "c:\\backup.ab";
String outFile = "c:\\backup.tar";
int skip = 0;
System.setProperty("line.separator", (String)(char)10);
File f = new File(backupFile);
FileInputStream fis = new FileInputStream(f);
InputStreamReader sr = new InputStreamReader(fis);