Search value into CSV and put all to variable

Hi people,

I get a log file from an equipment to view a specific information. I already read the csv, but I don’t put de all lines of log to a foreach. The script is below:

[pre]

$DGI = Import-Csv c:\DGI.CSV -Header @(“device”,“port”,“alarm”,“description”,“time”,“status”,“severity”)

foreach ($line in $DGI) {

$port = $line.port

$time = $line.time

$column1 = @($port)

$column2 = @($time)

$i=0

$alarmdgi = ($column1,$column2)[0]|foreach {

new-object psobject -property @{

ONU = $Column1[$i]

TIME = $column2[$i++]

}

} |where ONU -ge 0 |%{write-output “[ONU: $($.ONU) TIME: $($.TIME)]”} | sort} [/pre]

When I run the script the result is:

[pre]DGI: [ONU: gpon-1/1/5/37 TIME: 2019-08-14 10:32:29 UTC-3] [/pre]

But, I know that have 2 informations to appear. Something like:

[pre]DGI: [ONU: gpon-1/1/5/37 TIME: 2019-08-14 10:32:29 UTC-3][ONU: gpon-1/1/7/45 TIME: 2019-08-14 10:00:00 UTC-3] [/pre]

 

Thank you for any help.

 

 

 

I’m struggling to recognize what exactly you like to do with your code. You might describe that a little more detailed. You may post a few lines of your CSV files
(formatted as code as well please)

Regardless of that: Could you please try to avoid to post a lot of unnecessary white space? Thanks.

Hi,

Here the original CSV:

[pre]onu,gpon-1/1/5/25,GPON_DGi,Receive dying gasp of ONU,2019-09-01 05:46:42 UTC-3,false,CRITICAL
onu,gpon-1/1/5/37,GPON_DGi,Receive dying gasp of ONU,2019-08-14 10:32:29 UTC-3,false,CRITICAL[/pre]
I need to get the second and fifth columns: customer and time event respectively. Into Powershell code I am putting a label of columns and trying to create a foreach to put into a variable.

Thanks and sorry about white spaces.

Hmmm … with this description this code snippet should be enough:

$DGI  = @’
onu,gpon-1/1/5/25,GPON_DGi,Receive dying gasp of ONU,2019-09-01 05:46:42 UTC-3,false,CRITICAL
onu,gpon-1/1/5/37,GPON_DGi,Receive dying gasp of ONU,2019-08-14 10:32:29 UTC-3,false,CRITICAL
'@ |
ConvertFrom-Csv -Delimiter ‘,’ -Header device, port, alarm, description, time, status, severity |
Select-Object -Property port,time
$DGI

Instead of the ConvertFrom-CSV you should use your Import-CSV cmdlet of course.

Hello Olaf, thanks for your time and kindness,

I didn’t understand this part of your code:
[pre] $ DGI = @ ’
un, gpon-1/1/5/25, GPON_DGi, receives a dying breath from the UN, 2019-09-01 05:46:42 UTC-3, false, CRITICAL
UN, gpon-1/1/5/37, GPON_DGi, receives a dying breath from the UN, 2019-08-14 10:32:29 UTC-3, false, CRITICAL
'@ | [/pre]
Do I need to put log content in plain text? Or is Working with a variable in which I import CSV?
Thank you in advance.

Rodrigo,

That’s just a way to deliver the data together with the code and make sure it works as intended. Of course you should use the Import-Csv you posted initially … like this:

$DGI = Import-Csv c:\DGI.CSV -Delimiter ‘,’ -Header device, port, alarm, description, time, status, severity |
Select-Object -Property port, time
$DGI

Boa noite :wink:

Thank you Olaf! we are almost there!!

[pre]
<text>Clientes DGI:
gpon-1/1/2/39 2019-09-02 18:07:18 UTC-3
gpon-1/1/2/41 2019-09-02 16:48:25 UTC-3
gpon-1/1/5/22 2019-09-02 18:31:42 UTC-3
gpon-1/1/5/25 2019-09-01 05:46:42 UTC-3
gpon-1/1/5/37 2019-08-14 10:32:29 UTC-3
[/pre]
Now, I would like two things: remove the " UTC-3" and separate each group information with a [ ] Like that:
[pre]
[gpon-1/1/2/39 2019-09-02 18:07:18]
[gpon-1/1/2/41 2019-09-02 16:48:25]
[gpon-1/1/5/22 2019-09-02 18:31:42]
[gpon-1/1/5/25 2019-09-01 05:46:42]
[gpon-1/1/5/37 2019-08-14 10:32:29]
[/pre]

Again, thank you so much!!

As usual you have several options …

Either with a calculated property …

DGI = Import-Csv c:\DGI.CSV -Delimiter ',' -Header device, port, alarm, description, time, status, severity |
Select-Object -Property @{Name = 'Rodrigos Output'; Expression = { "[{0} {1}]" -f .port, (.time -replace ‘\s+UTC-3$’) } }
$DGI

… or with a PSCustomObject …

DGI = Import-Csv c:\DGI.CSV -Delimiter ',' -Header device, port, alarm, description, time, status, severity |
ForEach-Object {
    [PSCustomObject]@{
        'Rodrigos Output' = "[{0} {1}]" -f .port, (.time -replace ‘\s+UTC-3$’)
}
}
$DGI

… or … :wink:

Olaf, thank you so much!!! worked exactly how I wished!!

many thanks!!!