Skip to content

Commit fb2e80e

Browse files
author
Bastien Perez - ITPro-Tips.com
committed
add function for fast search in pshell
1 parent 445ab7b commit fb2e80e

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

Convert-ListToString.ps1

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function Convert-ListToString {
2+
[CmdletBinding()]
3+
param (
4+
[Parameter()]
5+
$InputArray
6+
)
7+
8+
$string = New-Object -TypeName System.Text.StringBuilder
9+
foreach ($object in $InputArray) {
10+
[void]$string.Append($object)
11+
}
12+
13+
return $string.ToString()
14+
}

Get-ObjectUnique.ps1

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function Get-ObjectUnique {
2+
[CmdletBinding()]
3+
param (
4+
[Parameter()]
5+
$InputArray
6+
)
7+
8+
return [System.Collections.Generic.HashSet[string]]$InputArray
9+
}

Get-ObjectsInTwoArrays.ps1

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
function Get-ObjectsInTwoArrays {
2+
[CmdletBinding()]
3+
param (
4+
[Parameter()]
5+
$Array,
6+
[Parameter()]
7+
$ArrayToCompare,
8+
[Parameter(Mandatory)]
9+
[ValidateSet('In', 'NotIn')]
10+
$ComparisonMethod,
11+
[Parameter(Mandatory)]
12+
[ValidateSet('String', 'int','PSObject')]
13+
$ObjectType
14+
)
15+
16+
if($ObjectType -eq 'String'){
17+
$arrayIndex = [System.Collections.Generic.HashSet[String]]$Array
18+
}
19+
elseif($ObjectType -eq 'int'){
20+
$arrayIndex = [System.Collections.Generic.HashSet[int]]$Array
21+
}
22+
elseif($ObjectType -eq 'PSObject'){
23+
$arrayIndex = [System.Collections.Generic.HashSet[PSObject]]$Array
24+
}
25+
26+
[System.Collections.Generic.List[PSObject]]$res = @()
27+
28+
if ($ComparisonMethod -eq 'In') {
29+
foreach ($object in $ArrayToCompare) {
30+
if ($arrayIndex.Contains($object)) {
31+
$res.Add($object)
32+
}
33+
}
34+
}
35+
elseif ($ComparisonMethod -eq 'NotIn') {
36+
foreach ($object in $ArrayToCompare) {
37+
if (-not($arrayIndex.Contains($object))) {
38+
$res.Add($object)
39+
}
40+
}
41+
}
42+
43+
Write-Host "$($res.count) $ComparisonMethod two arrays"
44+
return $res
45+
}

0 commit comments

Comments
 (0)