Created
October 9, 2014 16:53
-
-
Save IISResetMe/cee36040106bdd4a7fbb to your computer and use it in GitHub Desktop.
Implementing map function in PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function map | |
{ | |
param( | |
[Parameter(Mandatory=$true,Position=0)] | |
[scriptblock] | |
$Function, | |
[Parameter(Mandatory=$true,Position=1)] | |
[Object[]] | |
$InputObject | |
) | |
if($Function.Ast.ParamBlock -eq $null) | |
{ | |
$Function = [scriptblock]::Create('param($_)'+$Function.ToString()) | |
} | |
elseif($Function.Ast.ParamBlock.Parameters.Count -eq 1) | |
{ | |
if($Function.Ast.ParamBlock.Parameters[0].Name.ToString() -ne '$_') | |
{ | |
Write-Error "Invalid ParamBlock in Function" | |
return $false | |
} | |
} | |
else | |
{ | |
Write-Error "Invalid ParamBlock in Function" | |
return $false | |
} | |
foreach($obj in $InputObject) | |
{ | |
&$Function $obj | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment