1

Wildcard Matching

I am writing a function in an attempt to enumerate through the environment variables and find the least common denominator when returning wildcards.Right below is the output that I recieve and I honestly have no idea where these digits are coming from. It's def the indexed positions, but I did not instruct it to happen. This some weird behavior of the array list?

I think since it it processing it backwards its is also grabbing environment variables matching "?" and resolving to that, but any error handling ive tried to avoid that has been unsuccessful

0
1
2
3
4
5
6
7
8
${env:userdomai?}
${env:userdoma??}
${env:userdom???}
${env:userdo????}
${env:userd?????}
${env:user??????}
${env:use???????}
${env:us????????}
${env:u?????????}
function wildCard-Replace {
    param (
        [string]$string 
    )
    
    $modifiedString = $string
    $options = New-Object System.Collections.ArrayList
    $yup = New-Object System.Collections.ArrayList

    # Iterate through each character in the string in reverse order
    for ($i = $string.Length - 1; $i -ge 0; $i--) {
        
        # Replace the character with a question mark
        $modifiedString = $modifiedString.Substring(0, $i) + "?" + $modifiedString.Substring($i + 1)
        $ModStringValue = (get-item "env:$modifiedString").value
        $constant = ('${'+"env:$string}").Value
        $validated = ('${'+"env:$modifiedString}")
        if (($modifiedString -match "[^?]") -and ($ModStringValue -like "*$constant*")){
            $yup.Add($validated) #| Out-Null
        }
    }
    return $yup
}
I am Jakoby
  • 577
  • 4
  • 19
  • 1
    In short: any output - be it from a PowerShell command, an operator-based expression, or a .NET method call - that is neither captured in a variable nor redirected (sent through the pipeline or to a file) is _implicitly output_ from a script or function. To simply _discard_ such output, use `$null = ...`. If you don't discard such output, it becomes part of a script or function's "return value" (stream of output objects). See the linked duplicate for more information. – mklement0 Mar 21 '23 at 13:01
  • 1
    Specifically, it is the `ArrayList`'s `.Add()` method call that produces the undesired output. -> `$null = $yup.Add($validated)` – mklement0 Mar 21 '23 at 13:03
  • 1
    thank you yea so i just piped the output to $null and it works just fine now thank you $yup.Add($validated) | Out-Null – I am Jakoby Mar 21 '23 at 14:16
  • 1
    Glad to hear it. Yes, `... | Out-Null` works too, as do `> $null` (equivalent) and `[void] (...)`. However `$null = ...` is syntactically easier than a `[void]` cast and avoids potential performance pitfalls with `... | Out-Null` / `> $null` - see [this answer](https://stackoverflow.com/a/55665963/45375) for a juxtaposition of these techniques. – mklement0 Mar 21 '23 at 14:26

0 Answers0