vNugglets_SupportingFunctions.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
## helper functions internal to this module

function _New-RegExJoinedOrPattern {
<# .Description
    Helper function to take one or more strings and return a "joined" OR string (join values with "|"), and optionally RegEx escaping the input strings if specified
    .Outputs
    String
#>

    param (
        ## The string(s) for the regular expression pattern
        [parameter(Mandatory=$true)][String[]]$String,

        ## Switch: escape the string(s) and add expression start/end anchors ("^" and "$")?
        [switch]$EscapeAsLiteral
    ) ## end parameter

    process {
        ## if RegEx-escaping each string, do so and add line anchors "^" and "$" to each, then join all with "|"
        if ($EscapeAsLiteral) {($String | Foreach-Object {"^{0}$" -f [System.Text.RegularExpressions.Regex]::Escape($_)}) -join "|"}
        ## else, just join all as-is with "|"
        else {$String -join "|"}
    } ## end process
} ## end fn



function _Format-AsHexWWNString {
<# .Description
    Helper function for formatting WWN as hex string with colon-separators
    .Outputs
    String
#>

    param(
        ## the WWN in [long] format (not in hex, yet)
        [parameter(Mandatory=$true)][long]$WWN_long
    )
    ## convert to hex and then create a string with colons separating every two hex characters
    process {(("{0:x}" -f $WWN_long) -split "(\w{2})" | Where-Object {$_ -ne ""}) -join ":"}
} ## end function