Out-File truncated to 79 chars. Need help outputting full text

Welcome Forums General PowerShell Q&A Out-File truncated to 79 chars. Need help outputting full text

Viewing 4 reply threads
  • Author
    Posts
    • #186431
      Participant
      Topics: 1
      Replies: 1
      Points: 15
      Rank: Member

      I have a script that is outputting only the first 79 characters of a table, but I'd like the log to contain the full table.  This blog shows how to output the entire table, but it's a little over my head, and when I tried to implement it, I'm getting an error.

      This snippet is the original code that outputs only 79 characters:

      # Output the log file
      $a = @{Expression={$_.PROCESS}; Label="PROCESS"; Width=15}, 
      @{Expression={$_.DESCRIPTION}; Label="DESCRIPTION"; Width=35}, 
      @{Expression={$_.'START TIME'}; Label="START TIME"; Width=23}, 
      @{Expression={$_.'END TIME'}; Label="END TIME"; Width=23}, 
      @{Expression={$_.'DURATION'}; Label="DURATION"; Width=12}, 
      @{Expression={$_.'STATUS'}; Label="STATUS"; Width=90}
      
      $logTable | Format-Table -Property $a | Out-File $logFile

      I replaced that last line with this code (based on the article linked above):

      Get-Alias -Definition Invoke-* `
      | Format-Table -Property $a -AutoSize `
      | Out-String -Width 4096 `
      | Out-File $logFile

      But I'm getting this error:

      format-default : The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
      + CategoryInfo : NotSpecified: (:) [format-default], CollectionNotInitializedException
      + FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException,Microsoft.PowerShell.Commands.FormatDefaultCommand

      Any help appreciated, I honestly don't even know where to start.  I am in a finance role supporting several financial systems.  An implementation partner created several powershell scripts to perform maintenance tasks, powershell is brand new to me, but I'm trying to get up to speed asap.

    • #186491
      Participant
      Topics: 9
      Replies: 1277
      Points: 1,051
      Helping Hand
      Rank: Community Hero

      Format-Table is really about making it formatted inside the Powershell. Think of $logTable as an Excel spreadsheet, when you export it to a text format there isn't really anything you can do with it except read it manually. Unless there is another part of the process that is consuming this file, you should use Export-CSV to save the $logTable into a more 'usable' format. Try something like this:

      $logTable | 
      Select-Object -Property Process, Description, 'Start Time', 'End Time',Duration, Status |
      Export-Csv -Path C:\MyFile.csv -NoTypeInformation
      

      You don't even need the Select-Object if those are the only properties in the $logTable object, you just place a # in front of that line to remark it out.

    • #186502
      Participant
      Topics: 1
      Replies: 1
      Points: 15
      Rank: Member

      Hi Rob,

      Yes, another process in the script actually does utilize the table, an email is sent with the table in the body (with some additional HTML tags added later).  The table in the email looks like this:  https://i.imgur.com/xTKwTtb.png

      And By the way, the log that I'm trying to expand looks like this:  https://i.imgur.com/9tKR3md.png

      And that incomplete log is a problem because this script detects processing errors by looking for the string, "EPMAT-", which is actually part of what's truncated off, so an error that occurred last night wasn't noticed.

      The entire script is here if you would like to review, I changed up specifics to my environment (names of databases, servers, tables):  https://gist.github.com/jmcahren/96e390a2120cbaafaf26a71caa693731.js

    • #186511
      Participant
      Topics: 0
      Replies: 115
      Points: 433
      Helping Hand
      Rank: Contributor

      Hello DWooderson,

      I've reviewed your original script and found the issue, because the line uses Format-Table it is limited to the default output line of 80 characters. You can change this by piping to Out-String cmdlet and specifying the width of each line. This is actually done in the script on the next output. Just copy paste the formatting and that will take care of it.

      Reference Lines 151 and 153 – 156 of the script you posted.

    • #186615
      Participant
      Topics: 9
      Replies: 1277
      Points: 1,051
      Helping Hand
      Rank: Community Hero

      Reviewing the GitHub code, the logic appears to dump the data to a logfile and then check to see if 'EMPAT-' is found in the log to trigger a failed job. @Jason.Robertson was on point with his answer to the cause, but there are some other ways to address the issue. You can also check $logTable variable (in memory) for the EMPAT information, so rather than...

      If (Select-String -Path $logFile -Pattern "EPMAT-" -Quiet) {
      

      you can do something like this:

      If ($logTable.Description -like "*EPMAT-*") {
      

      An example:

      $logTable = @()
      $logTable += [pscustomobject]@{Description = 'Login blah'}
      $logTable += [pscustomobject]@{Description = 'Something EMPAT-Something'}
      $logTable += [pscustomobject]@{Description = 'Logout blah'}
      
      if ($logTable.Description -like '*EMPAT-*') {
          'Failed'
      }
      else {
          'Success'
      }
      
Viewing 4 reply threads
  • You must be logged in to reply to this topic.