2

By default hash tables created by PowerShell (e.g. $hashtable = @{}) are case insensitive.

For my script I want to explicitly create a case sensitive hash table.

This can be done by:

$hashtable = [hashtable]::new()

or:

$hashtable = New-Object hashtable

But I want to have my script also compliant with the default PSScriptAnalyzer rules. For the above case sensitive hash table examples, the UseLiteralInitializerForHashtable rule cases a warning:

Use literal initializer, @{{}}, for creating a hashtable as they are case-insensitive by default

I would expect to be able to work arround this by specifying the StringComparer, like:

[HashTable]::New(0, [StringComparer]::Ordinal)

But this still generates an error (although [HashTable]::New(0, [StringComparer]::OrdinalIgnoreCase) doesn't).
AFAIK, there is not something like: [StringComparer]::OrdinalMatchCase, or?

How to create a case sensitive Hashtable without generating a PSScriptAnalyzer warning?

PSScriptAnalyzer version: 1.18.3
Tested both Windows PowerShell (5.1) and PowerShell Core (6.2.3)

Steps to reproduce the warning:

Invoke-ScriptAnalyzer -ScriptDefinition '[HashTable]::New(0, [StringComparer]::Ordinal)'
iRon
  • 20,463
  • 10
  • 53
  • 79
  • 1
    [HashTable]::New(0, [StringComparer]::Ordinal) is working here without any error. Which PS Version are you using? – f6a4 Dec 16 '19 at 14:00
  • For info, the PS plugin for VScode doesn't complain about any of those formats. – Scepticalist Dec 16 '19 at 14:13
  • I have added some version information and steps to reproduce the warning to the question. – iRon Dec 16 '19 at 14:22
  • i _vaguely_ recall reading that you can add PSSA instructions in your file. that would let you tell it to ignore the specified rule for that one file. i suspect you can do the same for the workspace ... but i have not been able to find any docs on the idea. [*blush*] – Lee_Dailey Dec 16 '19 at 18:10
  • Thanks for all the suggestions, I guess I was actually for a confirmation that I didn't overlook something obvious in this. I have created a PSScriptAnalyser issue enhancement request: [#1858](https://github.com/PowerShell/PSScriptAnalyzer/issues/1385) – iRon Dec 17 '19 at 08:42

1 Answers1

0

Have you tried to encapsulate the hashtable using inline c# as a custom static class?

Add-Type -typedef @"
    using System;
    using System.Collections;

    namespace myCsharp 
    {
        //-----------------------------------------
        public class myHashtable
        //-----------------------------------------
        {

            //-------------------------------------
            public static Hashtable GetHashtable()
            //-------------------------------------
            {
                Hashtable ht = new Hashtable( 0, StringComparer.Ordinal);
                return ht;
            }

        }
    }
"@

$x = [myCsharp.myHashtable]::GetHashtable()
f6a4
  • 1,684
  • 1
  • 10
  • 13
  • 1
    Thanks for the suggestion. Although this technically works, it is far off from the actual PSScriptAnalyzer goal to create lean PowerShell scripts... – iRon Dec 17 '19 at 07:47