Home > Blockchain >  Write terminal output to csv using powershell
Write terminal output to csv using powershell

Time:10-11

I am having below code which is giving me output in a text file

$FilePath  = "D:\daily\$((Get-Date).ToString('yyyy-MM-dd'))"

if([System.IO.File]::Exists($FilePath)){
    # file with path $path doesn't exist
    $Folder_Path = New-Item -ItemType Directory -Path "D:\daily\$((Get-Date).ToString('yyyy-MM-dd'))"
}
$Switch_IP = "12.68.15.11"

#Take Session
        $Session = Posh-SSH\New-SSHSession -ComputerName $Switch_IP -Credential $netcredential 
        $Strem = New-SSHShellStream -SSHSession $Session
#Execute command
        $ScreenLength = $Strem.WriteLine("screen-length disable")
        $Strem.read()
        $nqahistory = $Strem.WriteLine("display nqa history")
        sleep -Seconds 5
        $streamOut = $Strem.read()
        
        $streamOut | Out-File -FilePath "$FilePath\$((Get-Date).ToString("ddMMyyyyHHmm")).txt" -Force -Append

the output of the file is

screen-length disable
display nqa history




***********************************************************************

*                                                                     *

*                                                                     *

*                                                                     *

*    _____       _       _          _____       _           _         *

*   / ____|     (_)     | |        / ____|     | |         (_)        *

*  | |___   __ _ _ _ __ | |_  ___ | |  __  ___ | |__   __ _ _ _ __    *

*   \___ \ / _` | | '_ \| __||___|| | |_ |/ _ \| '_ \ / _` | | '_ \   *

*   ____) | (_| | | | | | |_      | |__| | (_) | |_) | (_| | | | | |  *

*  |_____/ \__,_|_|_| |_|\__|      \_____|\___/|_.__/ \__,_|_|_| |_|  *

*                                                                     *

*                                                                     *

*                                                                     *

*                              EQX                                    *

*                                                                     *

*---------------------------------------------------------------------*

*                                                                     *

*                             DIS01                                   *

*                                                                     *

*                                                                     *

* This network of computers is protected by a security system.        *

* Criminal law prohibits unauthorized use.                            *

* Violators will be prosecuted.                                       *

***********************************************************************





******************************************************************************

* Copyright (c) 2010-2019 Hewlett Packard Enterprise Development LP          *

* Without the owner's prior written consent,                                 *

* no decompiling or reverse-engineering shall be allowed.                    *

******************************************************************************



<DIS01>screen-length disable

<DIS01>display nqa history

NQA entry (admin cgadmin, tag isl_latency) history records:

Index      Response     Status           Time

2618       3            Succeeded        2022-10-07 17:11:35.9

2617       2            Succeeded        2022-10-07 17:06:35.9

2616       2            Succeeded        2022-10-07 17:01:35.9

2615       2            Succeeded        2022-10-07 16:56:35.9

2614       2            Succeeded        2022-10-07 16:51:35.9

2613       3            Succeeded        2022-10-07 16:46:35.9

2612       3            Succeeded        2022-10-07 16:41:35.9

2611       3            Succeeded        2022-10-07 16:36:35.9

2610       3            Succeeded        2022-10-07 16:31:35.9

2609       2            Succeeded        2022-10-07 16:26:35.9

2608       2            Succeeded        2022-10-07 16:21:35.9

2607       2            Succeeded        2022-10-07 16:16:35.9

2606       2            Succeeded        2022-10-07 16:11:35.9

2605       2            Succeeded        2022-10-07 16:06:35.9

2604       2            Succeeded        2022-10-07 16:01:35.9

2603       2            Succeeded        2022-10-07 15:56:35.9

2602       2            Succeeded        2022-10-07 15:51:35.9

2601       3            Succeeded        2022-10-07 15:46:35.9

2600       2            Succeeded        2022-10-07 15:41:35.9

2599       2            Succeeded        2022-10-07 15:36:35.9

2598       3            Succeeded        2022-10-07 15:31:35.9

2597       4            Succeeded        2022-10-07 15:26:35.9

2596       3            Succeeded        2022-10-07 15:21:35.9

2595       3            Succeeded        2022-10-07 15:16:35.9

2594       3            Succeeded        2022-10-07 15:11:35.9

2593       2            Succeeded        2022-10-07 15:06:35.9

2592       3            Succeeded        2022-10-07 15:01:35.9

2591       2            Succeeded        2022-10-07 14:56:35.9

2590       3            Succeeded        2022-10-07 14:51:35.9

2589       3            Succeeded        2022-10-07 14:46:35.9

2588       2            Succeeded        2022-10-07 14:41:35.9

2587       3            Succeeded        2022-10-07 14:36:35.9

2586       2            Succeeded        2022-10-07 14:31:35.9

2585       2            Succeeded        2022-10-07 14:26:35.9

2584       2            Succeeded        2022-10-07 14:21:35.9

2583       3            Succeeded        2022-10-07 14:16:35.9

2582       2            Succeeded        2022-10-07 14:11:35.9

2581       3            Succeeded        2022-10-07 14:06:35.9

2580       2            Succeeded        2022-10-07 14:01:35.9

2579       2            Succeeded        2022-10-07 13:56:35.9

2578       3            Succeeded        2022-10-07 13:51:35.9

2577       2            Succeeded        2022-10-07 13:46:35.9

2576       2            Succeeded        2022-10-07 13:41:35.9

2575       2            Succeeded        2022-10-07 13:36:35.9

2574       2            Succeeded        2022-10-07 13:31:35.9

2573       2            Succeeded        2022-10-07 13:26:35.9

2572       3            Succeeded        2022-10-07 13:21:35.9

2571       3            Succeeded        2022-10-07 13:16:35.9

<DIS01>

But I need to get only the values which is coming in row/column format and write the data in csv file.

Index      Response     Status           Time
.......
.......

was trying something like below, but only getting the header part

gc "D:\daily\2022-10-07\071020221715.txt" | ? {($_ -match '^Index')}

Please let me know how can I trim and write to csv.

The script will execute every 4 hours, so the data will be appended to same csv file

CodePudding user response:

This regex may work to parse your table:

$re   = [regex] '(?m)^\d . \d{4}(?:-\d{2}){2}\s(?:\d{2}:){2}[\d.] |^Index. Time'
$file = Get-Content "D:\daily\2022-10-07\071020221715.txt" -Raw
$re.Matches($file) | ForEach-Object { $_.Value -replace '\s ', ',' } |
    Set-Content path\to\export.csv

Using the content you're showing us in question if we attempt to convert it into an object:

$re.Matches($file) | ForEach-Object { $_.Value -replace '\s ', ',' } |
    ConvertFrom-Csv

We get the following output:

Index Response Status    Time
----- -------- ------    ----
2618  3        Succeeded 2022-10-07
2617  2        Succeeded 2022-10-07
2616  2        Succeeded 2022-10-07
2615  2        Succeeded 2022-10-07
2614  2        Succeeded 2022-10-07
2613  3        Succeeded 2022-10-07
2612  3        Succeeded 2022-10-07
2611  3        Succeeded 2022-10-07
....
....
  • Related