pyinfra-dev / pyinfra

pyinfra turns Python code into shell commands and runs them on your servers. Execute ad-hoc commands and write declarative operations. Target SSH servers, local machine and Docker containers. Fast and scales from one server to thousands.
https://pyinfra.com
MIT License
3.85k stars 374 forks source link

Add PowerShell 7 support #1052

Open ronan-lg opened 8 months ago

ronan-lg commented 8 months ago

Is your feature request related to a problem? Please describe

As Get-ItemProperty output is different → parsing fails

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

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

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

vojta001 commented 7 months ago

I think a much more future-proof, clean and safe (with respect to various kinds of injections when running commands on untrusted hosts) way is to use a proper serialization (JSON/XML) for transport. It is even much easier to handle the results on pyinfra's side while simply switching from Get-ItemProperty -Path C:\Windows to Get-ItemProperty -Path C:\Windows | ConvertTo-Json and possibly requesting only a subset of fields for efficiency.