PowerShell Text File

I get the following output from a PowerShell request. I do not have access to the server to execute the request, so I have no way to influence the output format.

Example:

Name : folderC FullName : D:\folderA\folderB\folderC Length : CreationTime : 2/8/2014 11:12:58 AM LastAccessTime: 2/8/2014 11:12:58 AM Name : filename.txt FullName : D:\folderA\folderB\filename.txt Length : 71560192 CreationTime : 11/25/2015 3:10:43 PM LastAccessTime: 11/25/2015 3:10:43 PM 

How can I format the contents above to get something more convenient, for example, in the form of a table, for example like this:

 Name|FullName|Length|CreationTime|LastAccessTime 
+5
source share
2 answers

As @alroc notes in the commentary on the question, it is possible that the objects are accessible by the OP, given that they claim that the output is β€œfrom the Powershell request” - if so, simply reformatting the array of objects using regular cmdlets is an option.
On the contrary, this answer assumes that only the textual representation printed in the question is available.

Dave Sexton's answer is a simpler and more elegant choice if:

  • input has no empty values ​​(sample input OP is used).
  • the input file is small enough to be read into memory as a whole.

Consider the approach below to avoid the problems above and / or if you want more control over how input is converted to user objects , especially with respect to creating properties with types other than [string] : extend the toObj() function below (as written, that's it properties are also just strings).


 Get-Content File | % ` -begin { function toObj([string[]] $lines) { $keysAndValues = $lines -split '(?<=^[^ :]+)\s*: ' $htProps = @{} for ($i = 0; $i -lt $keysAndValues.Count; $i += 2) { $htProps.($keysAndValues[$i]) = $keysAndValues[$i+1] } return [PSCustomObject] $htProps } $lines = @() } ` -process { if ($_.trim() -ne '') { $lines += $_ } else { if ($lines) { toObj $lines } $lines = @() } } ` -end { if ($lines) { toObj $lines } } | Format-Table 

Explanation:

  • Uses ForEach-Object ( % ) with separate begin , process and end blocks.

  • -begin executed once at the beginning:

    • Defines a helper function toObj() that converts a block of adjacent non-empty input strings into a single user object.

      • toObj() splits the array of strings into an array of adjacent key values, converts this array into a hash table, which is then converted to a user object.
    • Initializes an array of $lines , which will store the lines of one block of adjacent non-empty input lines

  • The -process block executed for each line of input:

    • If the input line is not empty: adds it to the current block of adjacent non-empty input lines stored in the $lines array.

    • Otherwise: sends the current block to toObj() to convert to a custom object, and then flushes the $lines array to start the next block. Essentially, toObj() (starting non-empty lines) is called for each paragraph.

  • The -end block executed once at the end:

    • Sends the last paragraph to toObj() for conversion to a custom object.
  • Finally, the resulting array of user objects is passed to the Format-Table .

+3
source

I think you need to split the text into records, replace the colons with equal ones, so that you can use ConvertFrom-StringData to turn each record into a hash, which can then be passed to New-Object for conversion to an object. The output of the object to the data, divided into pipes, can be performed using ConvertTo-Csv. Something like that:

 $x = @" Name : folderC FullName : D:\folderA\folderB\folderC Length : 0 CreationTime : 2/8/2014 11:12:58 AM LastAccessTime : 2/8/2014 11:12:58 AM Name : filename.txt FullName : D:\folderA\folderB\filename.txt Length : 71560192 CreationTime : 11/25/2015 3:10:43 PM LastAccessTime : 11/25/2015 3:10:43 PM "@ ($x -split '[\r\n]+(?=Name)') | % { $_ -replace '\s+:\s+', '=' } | % { $_ | ConvertFrom-StringData } | % { New-Object psobject -Property $_ } | ConvertTo-Csv -Delimiter '|' -NoTypeInformation 
+4
source

Source: https://habr.com/ru/post/1241021/


All Articles