首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用powershell将终端输出写入csv

使用powershell将终端输出写入csv
EN

Stack Overflow用户
提问于 2022-10-07 15:29:09
回答 1查看 40关注 0票数 0

下面的代码为我提供了文本文件中的输出

代码语言:javascript
复制
$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

文件的输出是

代码语言:javascript
复制
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>

但是我只需要获得行/列格式的值,并将数据写入csv文件中。

代码语言:javascript
复制
Index      Response     Status           Time
.......
.......

试着做下面这样的事情,但是只得到了标题部分

代码语言:javascript
复制
gc "D:\daily\2022-10-07\071020221715.txt" | ? {($_ -match '^Index')}

请告诉我如何修剪和写信给csv。

脚本将每4小时执行一次,因此数据将追加到同一个csv文件中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-07 16:14:24

这个判据可以解析您的表:

代码语言:javascript
复制
$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

如果我们试图将其转换为一个对象,则使用您正在向我们展示的内容:

代码语言:javascript
复制
$re.Matches($file) | ForEach-Object { $_.Value -replace '\s+', ',' } |
    ConvertFrom-Csv

我们得到以下输出:

代码语言:javascript
复制
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
....
....
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73989463

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档