OUTPUT WRONG ; if you know another form to do the script tell me us…

Welcome Forums General PowerShell Q&A OUTPUT WRONG ; if you know another form to do the script tell me us…

Viewing 4 reply threads
  • Author
    Posts
    • #221478
      Participant
      Topics: 2
      Replies: 1
      Points: 26
      Rank: Member
      powershell 7.0 core

      my script:

      function date{
      
          (Get-Date).ToString("dd.MM.yyyy/HH:mm:ss"); 
      
      }
      
      function renew {
      
          date >>"LastAccessTime"
      
          Write-Host "file lastaccesstime renew!!!" -ForegroundColor Red
      
      }
      
      function view{
      
          Get-Content "LastAccessTime" -ErrorAction SilentlyContinue
      
      }
      
      function getstart{
      
          $date = date
          $view = view
          $viewlenth = (view).length
      
          ($view)[0..$viewlenth]       
      
      }
      getstart

      OUTPUT:

      • FIRST OUTPUT WRONG
      PS C:\Users\Administrador> getstart
      
      InvalidOperation: 
      Line |
        20 | ($view)[0..$viewlenth]
           |  ~~~~~~~~~~~~~~~~~~~~~~
           | Cannot index into a null array.
      • SECOND OUTPUT WRONG
      PS C:\Users\Administrador> getstart
      
      
      2
      2
      .
      0
      4
      .
      2
      0
      2
      0
      /
      2
      0
      :
      5
      7
      :
      5
      9
      2
      2
      .
      0
      4
      .
      2
      0
      2
      0
      /
      2
      0
      :
      5
      7
      :
      5
      9

      YOU CAN CHECK THE ERRORS DOWNLOADING THE SCIPT IN GITHUB : https://github.com/Krip4us/POWERSHELL/blob/master/Interactive%20Console.ps1

       

       

       

       

       

       

      • This topic was modified 4 weeks ago by kvprasoon. Reason: move to right forum
    • #223128
      Senior Moderator
      Topics: 8
      Replies: 1215
      Points: 4,334
      Helping Hand
      Rank: Community Hero

      Can you let us know what did you really intent by below expression.

      Get-Content "LastAccessTime"
      

      Above line will expect a file named “LastAccessTime” in the present working directory where the script runs. If the file doesn’t exists it wil throw exception, but you are ignoring the exception with ErrorAction set to SilentlyContinue

    • #223452
      Participant
      Topics: 3
      Replies: 324
      Points: 1,056
      Helping Hand
      Rank: Community Hero

      At what point do you call renew? It seems like you’re trying to output the data to a file with no extension named “LastAccessTime” using output redirect >>. If that’s the case, renew would need to be called before view – to create this file. I don’t understand why you’re outputting it to a file just to read it back in, versus just saving it to a variable. It seems you’re just missing the call to renew?

    • #227284
      Participant
      Topics: 2
      Replies: 1
      Points: 26
      Rank: Member
    • #227305
      Participant
      Topics: 3
      Replies: 324
      Points: 1,056
      Helping Hand
      Rank: Community Hero

      So you just needed to call renew?

Viewing 4 reply threads
  • You must be logged in to reply to this topic.