Last active
August 29, 2015 14:11
-
-
Save guitarrapc/d76791ca0c058544d6a4 to your computer and use it in GitHub Desktop.
LINQ SelectMany for 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
SelectMany -Source (1,2),(3,4) {$Source | %{"OU={0}" -f $_}} -Verbose | |
<# | |
詳細: 1 2 | |
詳細: OU=1 OU=2 | |
OU=1 | |
OU=2 | |
詳細: 3 4 | |
詳細: OU=3 OU=4 | |
OU=3 | |
OU=4 | |
#> | |
SelectMany -Source (1,2),(3,4) | |
<# | |
1 | |
2 | |
3 | |
4 | |
#> | |
SelectMany -Source (1,2),(3,4) | measure | |
<# | |
Count : 4 | |
#> |
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
SelectMany -Source 1,2,(3,4) {$Source | %{"OU={0}" -f $_}} -Verbose | |
<# | |
詳細: 1 | |
詳細: OU=1 | |
OU=1 | |
詳細: 2 | |
詳細: OU=2 | |
OU=2 | |
詳細: 3 4 | |
詳細: OU=3 OU=4 | |
OU=3 | |
OU=4 | |
#> |
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
1,2,(3,4) | SelectMany -ScriptBlock {$Source | %{"OU={0}" -f $_}} -Verbose | |
<# | |
詳細: 1 | |
詳細: OU=1 | |
OU=1 | |
詳細: 2 | |
詳細: OU=2 | |
OU=2 | |
詳細: 3 | |
詳細: OU=3 | |
OU=3 | |
詳細: 4 | |
詳細: OU=4 | |
OU=4 | |
#> |
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
#Requires -Version 4.0 | |
function SelectMany | |
{ | |
[CmdletBinding()] | |
param | |
( | |
[parameter(Mandatory = 1, Position = 0, ValueFromPipeline = 1)] | |
[PSObject[]]$Source, | |
[parameter(Mandatory = 0, Position = 1)] | |
[ScriptBlock]$ScriptBlock = {$Source} | |
) | |
process | |
{ | |
foreach ($x in $Source) | |
{ | |
$output = $ScriptBlock.GetNewClosure().InvokeWithContext( | |
$null, | |
(New-Object "System.Management.Automation.PSVariable" ("Source", $x)) | |
) | |
foreach ($o in $output) | |
{ | |
$o | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment