Skip to content

Instantly share code, notes, and snippets.

View codepersononline's full-sized avatar

codepersononline

View GitHub Profile
@codepersononline
codepersononline / powershellLoggingIdea.ps1
Created March 27, 2017 10:53
powershell error logging with multiple destinations.
#1. in your logging module, you could have a function like this that takes a dependency called Destination.
function Write-ToLog{
param(
[Parameter(Mandatory=$true)] $Destination,
[Parameter(Mandatory=$true)] [System.String] $Message
)
try {
$Destination.Write()
}
catch {
@codepersononline
codepersononline / Example - ModulePatternViaCallback.ps1
Last active August 29, 2015 14:21
Example - load and run a Module Pattern from a callback
<# Author: Steve Rathbone
May 2015
#>
$sb = {
[string]$myPrivateVariable = "Hello World!"
function Say-HelloWorld([string]$s) {
write-output $s
}
@codepersononline
codepersononline / gist:f2856c3c50f410099e30
Created August 31, 2014 04:22
invoke a powershell script from IronPython (basic)
#this is in no way production ready code that should be used in a live system,
#however it explains the basic elements that make the connection possible.
import clr, System
clr.AddReference("System.Management.Automation")
from System.Management.Automation import Runspaces
myrunspace = Runspaces.RunspaceFactory.CreateRunspace()
myrunspace.Open()
pipeline = myrunspace.CreatePipeline()
@codepersononline
codepersononline / addScriptMethodToObject
Last active August 29, 2015 14:02
Example - bind a ScriptMethod object type to an object recieved via the pipeline
#The string "foo" below explicitly states that $obj is instantiated with a real .NET concrete type (String) underneath,
#and that the object $obj is subsequently turned into a new type of PSObject and is not a new extension method applied to the
#string class.
$obj = "foo" | Add-Member -MemberType ScriptMethod -Value {
param( $first, $second)
"first is $first, second is $second"
}
-Name DoStuff -PassThru; $obj.DoStuff('abc', '123')
@codepersononline
codepersononline / iseextensionmethods
Created March 23, 2014 01:02
add very simple extension method to Powershell host object to make it easier to develop ISE add-ons
$psise.CurrentFile | Add-Member ScriptMethod "GetFileExtension" {return [System.IO.Path]::GetExtension($psISE.CurrentFile.DisplayName)}