PoshCode Archive  Artifact Content

Artifact f07d3bc2fdb3a1ab5d8e29a043ae24c08ed38775e1e47e1fc1c35d8ce88c80cd:

  • File ScriptTransforms-module.ps1 — part of check-in [0d8aca7497] at 2018-06-10 13:19:04 on branch trunk — PowerShell module that allows scripters to define argument transformation attributes in simple PowerShell syntax. This is an extension developed by beefarino of my ScriptTransform example at http://huddledmasses.org/more-custom-attributes-for-powershell-parameters/ and now supports re-defining transforms without restarting PowerShell. (user: Joel Bennett size: 12649)

# encoding: ascii
# api: csharp
# title: ScriptTransforms module
# description: PowerShell module that allows scripters to define argument transformation attributes in simple PowerShell syntax.  This is an extension developed by beefarino of my ScriptTransform example at http://huddledmasses.org/more-custom-attributes-for-powershell-parameters/ and now supports re-defining transforms without restarting PowerShell.
# version: 1.4
# type: function
# author: Joel Bennett
# license: CC0
# function: New-ParameterTransform
# x-poshcode-id: 3029
# x-archived: 2012-12-23T23:40:23
# x-published: 2012-10-27T21:56:00
#
# Note: It is now possible to redefine transforms by name by running the New-ParameterTransform command again, but type aliases that are generated are bound at function definition time, so any functions that are using the transform before it is redefined must also be redefined to take advantage of the new transform definition.
# Example usage:
# import-module ScriptTransforms;
# Transform NormalizeWhitespace {
# $string = $args[0].Trim()
# do { 
# $before = $string
# $string = $string -replace " + |`t"," "
# } while($before -ne $string)
# $string
# }
# function Test ( [NormalizeWhitespace()] $z ) { $z }
# Now, when you run:
# PS> Test " This is`tnot`tnormal.  At all.   "
# This is not normal. At all. 
# v 1.4 Fixes crashes when the transform output is null
# v 1.3 Fixes an ascii paste bug in the class name randomization
# v 1.2 Randomization of class names with type accelerators 
# v 1.1 Modularization by beefarino
# v 1.0 Original code from Joel’s blog
#
function New-ParameterTransform {
#.Synopsis
#	Generates Parameter Transformation Attributes in simple PowerShell syntax
#.Description
#	New-ParameterTransform allows the creation of .Net Attribute classes which can be applied to PowerShell parameters to transform or manipulate data as it's being passed in.
#.Example
#	Transform TrimBraces {
#		param($string) 
#		if($string -is [String] -and $string.Length -gt 2 -and $string[0] -eq '[' -and $string[-1] -eq ']') { 
#			$string.SubString(1, $string.Length-2) 
#		} else { 
#			$string
#		} 
#	}
#
#	function Where-Type {
#		param(
#			[Type][TrimBraces()][String]
#			[Parameter(Mandatory=$true, Position=0)]
#			$Type,
#			
#			[Parameter(ValueFromPipeline=$true)]
#			$InputObject
#		)
#		process {
#			if($InputObject -is $Type) { $InputObject }
#		}
#	}
#
#   Description
#   -----------
#	This example allows us to pass the "Type" to Where-Type with or without the [Braces] which would normally cause an exception when casting to a type:
#
#	Get-ChildItem | Where-Type IO.FileInfo
#
#	Get-ChildItem | Where-Type [IO.FileInfo]
#
#	Get-ChildItem | Where-Type ([IO.FileInfo])
#
#.Notes
#   It is possible to redefine transforms by name by running the New-ParameterTransform command again, but the type aliases that are generated are bound at function definition time, so any functions that are using the transform before it is redefined must also be redefined to take advantage of the new transform definition.
#
#   v 1.4 Fixes crashes when the transform output is null
#   v 1.3 Fixes an ascii paste bug in the class name randomization
#   v 1.2 Randomization of class names with type accelerators 
#   v 1.1 Modularization by "beefarino":http://poshcode.org/author/beefarino
#   v 1.0 Original code from Joel's blog
	[cmdletbinding()]
	param(
		# The name of the parameter transform to generate
		[Parameter(Mandatory=$true,Position=0)]
		[string] $name,
		# The script definition of the parameter transform
		[Parameter(Mandatory=$true, Position=1)]
		[scriptblock] $script
	)

end {
	$className = [Convert]::ToBase64String( ([Guid]::NewGuid().ToByteArray()) ).Replace('+',([char]241)).Replace('/',([char]223)).TrimEnd('=')

	$Type = Add-Type -Passthru -TypeDefinition @"
using System;
using System.ComponentModel;
using System.Management.Automation;
using System.Collections.ObjectModel;

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class ${name}Attribute${className} : ArgumentTransformationAttribute {
   private ScriptBlock _scriptblock;
   private string _noOutputMessage = "Transform Script had no output.";

   public ${name}Attribute${className}() {
      this.Script = ScriptBlock.Create( @"
	  	$($script.ToString() -replace '"','""' )
" );
   }
   public override string ToString() {
      return Script.ToString();
   }
   
   public override Object Transform( EngineIntrinsics engine, Object inputData) {
      Collection<PSObject> output;
      try {
         output = engine.InvokeCommand.InvokeScript( engine.SessionState, Script, inputData );
      } catch (ArgumentTransformationMetadataException) {
         throw;
      } catch (Exception e) {
         throw new ArgumentTransformationMetadataException(
            string.Format("Transform Script threw an exception ('{0}'). See `$Error[0].Exception.InnerException.InnerException for more details.", e.Message),
            e);
      }
      if(output == null || output.Count == 0 ) { 
         throw new ArgumentTransformationMetadataException(NoOutputMessage);
      }
      
      if(output.Count > 1) 
      {
         Object[] transformed = new Object[output.Count];
         for(int i =0; i < output.Count;i++) {
            if(output[i] == null) {
               transformed[i] = null;
            } else {
               transformed[i] = output[i].BaseObject;
            }
         }
         return transformed;
      }
      else // (output.Count == 1) 
      {
         if(output[0] == null) {
            return null;
         }
         return output[0].BaseObject;
      }
   }
   
   public ScriptBlock Script {
      get { return _scriptblock; }
      set { _scriptblock = value; }
   }
   
   public string NoOutputMessage {
      get { return _noOutputMessage; }
      set { _noOutputMessage = value; }
   }  
}
"@

	$xlr8r = [psobject].assembly.gettype("System.Management.Automation.TypeAccelerators")
	if($xlr8r::AddReplace) { 
		$xlr8r::AddReplace( ${name}, $Type) 
	} else {
		$null = $xlr8r::Remove( ${name} )
		$xlr8r::Add( ${name}, $Type)
	}
}}

New-Alias -Name Transform -Value New-ParameterTransform;
Export-ModuleMember -Alias Transform -Function *
# SIG # Begin signature block
# MIIRDAYJKoZIhvcNAQcCoIIQ/TCCEPkCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUiseSTw8ZDs4/WyAi4TlGsBiB
# yUWggg5CMIIHBjCCBO6gAwIBAgIBFTANBgkqhkiG9w0BAQUFADB9MQswCQYDVQQG
# EwJJTDEWMBQGA1UEChMNU3RhcnRDb20gTHRkL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