Skip to content

Add PowerShell 7 support #14

@ronan-lg

Description

@ronan-lg

Is your feature request related to a problem? Please describe

As Get-ItemProperty output is different → parsing fails

  • Number of lines
  • Number of digits

Windows PowerShell 5.1

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.22621.2506
PSEdition                      Desktop
> Get-ItemProperty -Path C:\Windows
0|
1|
2|    Répertoire : C:\
3|
4|
5|Mode                 LastWriteTime         Length Name
6|----                 -------------         ------ ----
7|d-----        29/12/2023   6:01 PM                Windows
  < 6  >

PowerShell 7

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.3.10
PSEdition                      Core
> Get-ItemProperty -Path C:\Windows
0|
1|    Directory: C:\
2|
3|Mode                 LastWriteTime         Length Name
4|----                 -------------         ------ ----
5|d----          29/12/2023  6:01 PM                Windows
  < 5 >

In addition the output is colored:

b'd----          29/12/2023  6:01 PM                \x1b[44;1mWindows\x1b[0m'

Describe the solution you'd like

facts/windows_files.pyclass File(FactBase):process method

  • from
      def process(self, output):
          if len(output) < 7:
              return None
    
          # Note: The first 7 lines are header lines
          return parse_win_ls_output(output[7], self.type)
  • to
      def process(self, output):
          if len(output) < 5:
              return None
    
          # Note: The first lines are header lines
          return parse_win_ls_output(output[-2], self.type)

facts/util/win_files.pyWIN_LS_REGEX definition, relax the filetype and mode pattern

  • from
            # filetype and mode
            r"^([darhsl\-]{6})\s+"
  • to
            # filetype and mode
            r"^([darhsl\-]{5,6})\s+"

facts/util/win_files.pyparse_win_ls_output function, remove ansi sequences

  • from
      if output:
          matches = re.match(WIN_LS_REGEX, output)
  • to
      if output:
    
          # Patch to remove ansi sequences from PowerShell 7
          # @see https://stackoverflow.com/questions/14693701/how-can-i-remove-the-ansi-escape-sequences-from-a-string-in-python
          ansi_escape = re.compile(r"(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]", flags=re.IGNORECASE)
          output = ansi_escape.sub('', output)
    
          matches = re.match(WIN_LS_REGEX, output)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions