Skip to content

Instantly share code, notes, and snippets.

The whole point of using ValueFromPipeline (and ValueFromPipelineByPropertyName) is to replace this syntax:

"c:\windows","c:\Program Files" | ForEach-Object {
  Get-DirectoryFileSize -Directory $_
}

With this synax:

@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
function New-Node {
[OutputType("Whatsupduck.Powershell.GraphViz.Node")]
param(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String]$Name,
[Parameter(Mandatory=$false)]
@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 / 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: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 / 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 / CarCdr.ps1
Created August 12, 2015 21:47
Powershell CAR and CDR
function car {
[CmdletBinding(DefaultParameterSetName='DefaultParameter', HelpUri='http://go.microsoft.com/fwlink/?LinkID=113387', RemotingCapability='None')]
param(
[Parameter(ValueFromPipeline=$true)]
[psobject]
${InputObject},
[Parameter(ParameterSetName='SkipLastParameter', Position=0)]
[Parameter(ParameterSetName='DefaultParameter', Position=0)]
[System.Object[]]
@gpduck
gpduck / StartTempRabbitMQ.ps1
Created July 13, 2015 20:33
Startup a temporary RabbitMQ Server
$TempPath = [IO.Path]::GetTempPath()
$SBin = "C:\Program Files (x86)\RabbitMQ Server\rabbitmq_server-3.5.3.60710\sbin"
$NodeNameBase = [IO.Path]::GetFileNameWithoutExtension([IO.Path]::GetRandomFileName())
$NodeName = "$NodeNameBase@$Env:COMPUTERNAME"
$RabbitBase = Join-Path $TempPath $NodeNameBase
$ErlangHome = "c:\Program Files\erl7.0"
$RabbitPort = 5672
$MgmtPort = 15672
@gpduck
gpduck / cCmsMessage.psm1
Last active August 29, 2015 14:21
Adds CmsMessage commands that should be mostly compatible with the ones provided in v5 to down-level PowerShell.
<#
Copyright 2015 Chris Duck
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software