Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Merge And Sort Function with issues in certain pester versions?
Function MergeAndSort {
<#
.SYNOPSIS
MergeAndSort
.DESCRIPTION
Merges arrays and sorts, extracted for refactoring and adding unit tests
.PARAMETER VMNumbers
VMware Number array
.PARAMETER ADNumbers
ActiveDirectory Number array
.PARAMETER DNSNumbers
DNS Number array
.PARAMETER SCCM2012Numbers
ConfigMgr Number array
.PARAMETER SCOM2007Numbers
SCOM Number array
.EXAMPLE
PS C:\> MergeAndSort `
-VMNumbers $VMNumbers `
-ADNumbers $ADnumbers `
-DNSNumbers $DNSNumbers `
-SCCM2012Numbers $SCCM2012Numbers`
-SCOM2007Numbers $SCOM2007Numbers
.NOTES
For additonal information please contact ffff@ffff.com
.LINK
https://fffff
#>
[CmdletBinding()]
Param(
[string[]]$VMNumbers,
[string[]]$ADNumbers,
[string[]]$DNSNumbers,
[string[]]$SCCM2012Numbers,
[string[]]$SCOM2007Numbers
)
# Merge arrays
$mergedList = New-Object System.Collections.ArrayList
[void]$mergedList.Add(0)
if ($VMNumbers) { [void]$mergedList.AddRange($VMNumbers) }
if ($ADnumbers) { [void]$mergedList.AddRange($ADnumbers) }
if ($DNSNumbers) { [void]$mergedList.AddRange($DNSNumbers) }
if ($SCCM2012Numbers) { [void]$mergedList.AddRange($SCCM2012Numbers) }
if ($SCOM2007Numbers) { [void]$mergedList.AddRange($SCOM2007Numbers) }
[System.Collections.ArrayList]$mergedList = $mergedList | Sort-Object -Unique
Return $mergedList
}
# This test would work on windows 10.. (pester 4.0.3) but not on windows 7 same pester version, wmf 5.1
It "sorts numbers and removes duplicates" {
MergeAndSort `
-VMNumbers @(200, 100, 300) `
-ADNumbers @(200, 100, 300) `
-DNSNumbers @(200, 100, 300) `
-SCCM2012Numbers @(200, 100, 300) `
-SCOM2007Numbers @(200, 100, 300) | Should Be @(0,100,200,300)
}
# modifying to this doesn't help
It "sorts numbers and removes duplicates" {
$results = MergeAndSort `
-VMNumbers @(200, 100, 300) `
-ADNumbers @(200, 100, 300) `
-DNSNumbers @(200, 100, 300) `
-SCCM2012Numbers @(200, 100, 300) `
-SCOM2007Numbers @(200, 100, 300)
,$results | Should Be @(0,100,200,300)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.