POwershell script giving 0 bytes OUt-file

Welcome Forums General PowerShell Q&A POwershell script giving 0 bytes OUt-file

Viewing 5 reply threads
  • Author
    Posts
    • #212526
      Participant
      Topics: 1
      Replies: 2
      Points: 16
      Rank: Member

      Hi,

      I have a file like below

      2020-02-29 00:22:40,ACE001P,QL.IN.XCENTER.ERECEIPT.FACADE, 314,
      2020-03-01 00:52:40,ACE001P,QL.IN.XCENTER.ERECEIPT.FACADE, 264,
      2020-03-01 01:22:40,ACE001P,QL.IN.XCENTER.ERECEIPT.FACADE, 29,
      2020-03-01 01:52:40,ACE001P,QL.IN.XCENTER.ERECEIPT.FACADE, 11,

      I am using below script to get last 4 hours data, every 4 hours. I was working until last week, but its not now. I am not sure what the issue is.

      $Data = Import-CSV -path “E:\Chayan\QL.IN.XCENTER.ERECEIPT.FACADE_ACE001P_1.csv” -Header “Date”,”Col2″,”Col3″,”Col4″
      $CutoffDate = (Get-Date).AddHours(-4)
      $Data | Where-Object {$_.Date -as [datetime] -gt $CutoffDate} |
      Export-Csv -Path “.\QL.IN.XCENTER.ERECEIPT.FACADE_ACE001P_2.csv” -NoTypeInformation

       

    • #212529
      Participant
      Topics: 12
      Replies: 519
      Points: 1,194
      Helping Hand
      Rank: Community Hero

      It’s working. None of the dates above is from the last 4 hours.

    • #212547
      Participant
      Topics: 1
      Replies: 2
      Points: 16
      Rank: Member

      Yes, sorry for posting such a silly question. I should have looked at the source stack, one more issue though. how do I remove the column headers from this file. I am using -skip 1, but it doesn’t seem to be working.

       

      col1 col3 col4
      —- —- —-
      2020-03-01 00:52:40,ACE001P,QL.IN…. 264,
      2020-03-01 01:22:40,ACE001P,QL.IN…. 29,

    • #212634
      Participant
      Topics: 4
      Replies: 82
      Points: 251
      Helping Hand
      Rank: Contributor

      Export-Csv puts column headers in automatically.  You could remove them after the file is written or not use export-csv and use Add-Content to write plain text to the file.  Here is an example of how you could remove the header row after the file is written:

      $Data = Get-Content -Path .\QL.IN.XCENTER.ERECEIPT.FACADE_ACE001P_2.csv 
      $Data[1..($Data.Count)] | Set-Content .\QL.IN.XCENTER.ERECEIPT.FACADE_ACE001P_2.csv

      Here’s an example of how you could change from Export-Csv to using Set-Content

      $Data | ForEach-Object {
      $Value = $_.Date, $_.Col2, $_.Col3, $_.Col4
      Add-Content -Path .\QL.IN.XCENTER.ERECEIPT.FACADE_ACE001P_3.csv -Value ($Value -join ",") }

       

    • #212706
      Participant
      Topics: 1
      Replies: 2
      Points: 16
      Rank: Member

      This is from a different command I am using to extract specific columns from a source stack. Here is the command.

      Import-Csv .\QL.IN.XCENTER.ERECEIPT.FACADE_ACE002P.csv -Header col1,col2,col3,col4,col5,col6,col7,col8,col9,col10 -Delimiter ‘ ‘ | Select-Object col1,col3,col4|Out-File .\QL.IN.XCENTER.ERECEIPT.FACADE_ACE002P_2_1.csv

       

      2020-02-29 23:52:40,2020-03-01 00:22:40,ACE001P,QL.IN.XCENTER.ERECEIPT.FACADE, 314, 314, 0, 0, 2

      2020-03-01 00:22:40,2020-03-01 00:52:40,ACE001P,QL.IN.XCENTER.ERECEIPT.FACADE, 264, 264, 0, 0, 1

      2020-03-01 00:52:40,2020-03-01 01:22:40,ACE001P,QL.IN.XCENTER.ERECEIPT.FACADE, 29, 29, 0, 0, 1

      2020-03-01 01:22:40,2020-03-01 01:52:40,ACE001P,QL.IN.XCENTER.ERECEIPT.FACADE, 11, 11, 0, 0, 1

      2020-03-01 01:52:40,2020-03-01 02:22:40,ACE001P,QL.IN.XCENTER.ERECEIPT.FACADE, 14, 14, 0, 0, 1

      2020-03-01 02:22:40,2020-03-01 02:52:40,ACE001P,QL.IN.XCENTER.ERECEIPT.FACADE, 8, 8, 0, 0, 1

      2020-03-01 02:52:40,2020-03-01 03:22:40,ACE001P,QL.IN.XCENTER.ERECEIPT.FACADE, 1, 1, 0, 0, 1

      2020-03-01 09:22:40,2020-03-01 09:52:40,ACE001P,QL.IN.XCENTER.ERECEIPT.FACADE, 4, 4, 0, 0, 1

      2020-03-01 09:52:40,2020-03-01 10:22:40,ACE001P,QL.IN.XCENTER.ERECEIPT.FACADE, 76, 76, 0, 0, 1

      2020-03-01 10:22:40,2020-03-01 10:52:40,ACE001P,QL.IN.XCENTER.ERECEIPT.FACADE, 324, 324, 0, 0, 2

      2020-03-01 10:52:40,2020-03-01 11:22:40,ACE001P,QL.IN.XCENTER.ERECEIPT.FACADE, 692, 692, 0, 0, 2

       

      I want this to output without the following column headers.

      col1                       col3                       col4

      —-                       —-                       —-

      2020-02-29                 00:22:40,ACE001P,QL.IN…. 314,

      2020-03-01                 00:52:40,ACE001P,QL.IN…. 264,

      2020-03-01                 01:22:40,ACE001P,QL.IN…. 29,

    • #212712
      Participant
      Topics: 12
      Replies: 1610
      Points: 2,508
      Helping Hand
      Rank: Community Hero

      If you look at @MikeR’s response, it explains how to do this

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