jdhitsolutions / PSScriptTools

:wrench: :hammer: A set of PowerShell functions you might use to enhance your own functions and scripts or to facilitate working in the console. Most should work in both Windows PowerShell and PowerShell 7, even cross-platform. Any operating system limitations should be handled on a per command basis. The Samples folder contains demonstration script files

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Convert-HashtableToCode does not work with array of hash tables

pluma9 opened this issue · comments

Describe the bug
When passing a hash table, whose one of the values contains an array of hash tables, to Convert-HashtableToCode, the output is not as expected.

To Reproduce

C:\> $department = @{DepartmentName="IT"; Managers = @(@{Name="John"; Age=33}, @{Name="David";  Age = 31})}
C:\> Convert-HashtableToCode $department
@{
        Managers = 'System.Collections.Hashtable','System.Collections.Hashtable'
        DepartmentName = 'IT'
}

Expected behavior
The output should be:

@{
        Managers = @(@{Name="John"; Age=33}, @{Name="David";  Age = 31})
        DepartmentName = 'IT'
}

Desktop (please complete the following information):

  • Operating System Version: Windows 10
  • Module Version: 2.29.0
  • PowerShell Version: 5.1.19041.1
  • Powershell Edition: Desktop

Sure looks that way. A single hashtable is just fine. I didn't plan for an array. Working on it.

Update to v2.30.0 and try again.

The issue still persists in v2.30.0.

C:\> $department = @{DepartmentName="IT"; Managers = @(@{Name="John"; Age=33}, @{Name="David";  Age = 31})}
C:\> Convert-HashtableToCode $department
@{
        Managers = @{
        Age = 33
        Name = 'John'
},
@{
        Age = 31
        Name = 'David'
}
        DepartmentName = 'IT'
}

The expected output is:

@{
        Managers = @(
        @{
            Age = 33
            Name = 'John'
        },
        @{
            Age = 31
            Name = 'David'
        })
        DepartmentName = 'IT'
}

So your issue is the format or alignment? There was in fact a problem if there was an array of hashtables which I've fixed. The result is still usable and valid. Putting the array inside the @() isn't technically required. The result from the function and your expected result are functionally identical.

I was talking about the structure of the hash table. However, it's my mistake thinking the actual output and the expected output are different. Confirm that the issue has been fixed. Thanks @jdhitsolutions.

I made some additional changes in v2.31.0 to explicitly add @() for arrays. The output may still not be perfect but it should only take a moment to edit the result to meet your preferences or requirements.

It looks good to me. Thanks @jdhitsolutions.