【问题标题】:Powershell script to extract data from multiple text files into an excel spreadsheet用于将多个文本文件中的数据提取到 Excel 电子表格中的 Powershell 脚本
【发布时间】:2020-11-17 00:14:46
【问题描述】:

我对 PS 还很陌生,并且已经苦苦挣扎了几天。 我在一个文件夹中有多个文本文件,其中包含我想提取到 Excel 电子表格中的特定数据。 每个文件如下所示:

Client n° : xxx Client name : xxx

                Computer status

pc group 1 : 

n°1 OK                   n°2 Disconnected               n°3 Unresponsive
n°4 Unreachable host     n°5 Unresponsive

Data read 11/11/20 12:50:07

Version: x.x.x 

我想要一个如下所示的输出文件:

 Client name and n°     OK       Disconnected      Unresponsive    Unreachable host   version       
     xxx/xxx            1             1                2                 1             x.x.x

对于状态列,它是具有该状态的 pc 总数,而不是我要显示的 pc n°。

目前我正在处理多个 .bat 文件,这些文件搜索状态并为每个状态输出一个文件

find /c "Disconnected" *.* > disconnected.txt
find /c "Unresponsive" *.* > unresponsive.txt

然后我在 Excel 中对每个输出进行排序,这花费了我太多时间,我想知道是否可以使用脚本自动执行此任务。

我真的没有任何PS知识,只会基本的批处理命令。

【问题讨论】:

  • 请编辑您的问题并添加您已经尝试过的代码。解释什么不起作用,如果您收到错误消息,也将它们添加到问题中。没有表现出自己的努力,这只是要求有人为您编写代码..

标签: excel powershell


【解决方案1】:

假设您的文件都在一个文件夹中,并且所有文件都具有.txt 扩展名。

然后你需要遍历这些文件并从中解析出你需要的数据:

# create a Hashtable to add the different status values in
$status = @{'OK' = 0; 'Disconnected'= 0; 'Unresponsive' = 0; 'Unreachable host'= 0}

# loop through the files in your path and parse the information out
$result = Get-ChildItem -Path 'D:\Test' -Filter '*.txt' -File | ForEach-Object {
    switch -Regex -File $_.FullName {
        '^Client n°\s*:\s*([^\s]+)\s+Client name\s*:\s*(.+)$' {
            # start collecting data for this client
            $client = '{0}/{1}' -f $matches[2], $matches[1]
            # reset the Hashtable to keep track of the status values
            $status = @{'OK' = 0; 'Disconnected'= 0; 'Unresponsive' = 0; 'Unreachable host'= 0 }
        }
        '^\d+' {
            # increment the various statuses in the Hahstable
            ($_ -split '\d+').Trim() | ForEach-Object { $status[$_]++ }
        }
        '^Version:\s(.+)$' {
            $version = $matches[1]
            # since this is the last line for this client, output the collected data as object
            [PsCustomObject]@{
                'Client name and n°' = $client
                'OK'                 = $status['OK']
                'Disconnected'       = $status['Disconnected']
                'Unresponsive'       = $status['Unresponsive']
                'Unreachable host'   = $status['Unreachable host']
                'Version'            = $version
            }
        }
    }
}

# output on screen
$result | Format-Table -AutoSize

# output to CSV file
$result | Export-Csv -Path 'D:\Test\clientdata.csv' -UseCulture -NoTypeInformation

屏幕上的结果:

Client name and n° OK Disconnected Unresponsive Unreachable host Version
------------------ -- ------------ ------------ ---------------- -------
xxx/xxx             1            1            2                1 x.x.x 

【讨论】:

  • 它工作得很好,只是有些地方我需要适应。这个值“'n°\d+'”我假设它指的是电脑号码,我在示例文件上犯了一个错误,状态实际上如下:1 OK 2 Unresponsive 所以“n°”实际上不存在在原始文件中
【解决方案2】:

我用这个作为练习来测试我的能力。我用不同的数据创建了三个相同的文件,并测试了这个脚本。

只要它们是目录中的文本文件,脚本就会遍历每个文件,并按照您的要求从每个文件中提取数据。如果添加了一个杂散的文本文件,则脚本不知道也不关心,并将像对待其他文件一样对待它。如果有数据,它会找到它,并将该数据输出到excel文件。最后将文件设置为自行保存,然后立即关闭。

首先创建 Excel 文件,然后是工作簿。 (我注释掉了工作簿的命名。如果您喜欢,可以将其添加回来。)查找目录中的所有文本文件,然后在文本中搜索您在上面指定的文本中的特定内容。

在编写脚本期间,我尽可能多地发表评论,以帮助稍后进行修改。

输出格式如下: Excel Output

#Create An Excel File
$excel = New-Object -ComObject excel.application
$excel.visible = $True

#Add Workbook
$workbook = $excel.Workbooks.Add()

<#Rename Workbook
$workbook= $workbook.Worksheets.Item(1)
$workbook.Name = 'Client name and #'#>

#create the column headers
$workbook.Cells.Item(1,1) = 'Client name and n°'
$workbook.Cells.Item(1,2) = 'OK'
$workbook.Cells.Item(1,3) = 'Disconnected'
$workbook.Cells.Item(1,4) = 'Unresponsive'
$workbook.Cells.Item(1,5) = 'Unreachable'
$workbook.Cells.Item(1,6) = 'Version'
$workbook.Cells.Item(1,7) = 'Date Gathered'

$move = "C:\Users\iNet\Desktop\Testing"
$root = "C:\Users\iNet\Desktop\Testing"
$files = Get-ChildItem -Path $root -Filter *.txt

#Starting on Row 2
[int]$i = 2
ForEach ($file in $files){
   
$location = $root+"\"+$file

#Format your client data to output what you want to see. 
$ClientData = select-string -path "$location" -pattern "Client"
$ClientData = $ClientData.line
$ClientData = $ClientData -replace "Client n° :" -replace ""
$ClientData = $ClientData -replace "Client name :" -replace "|"
$row = $i
$Column = 1
$workbook.Cells.Item($row,$column)= "$ClientData"

#Data Read Date
$DataReadDate = select-string -path "$location" -pattern "Data read"
$DataReadDate = $DataReadDate.line
$DataReadDate = $DataReadDate -replace "Data read " -replace ""
    #Data Read Date, you asked for everything but this.
$row = $i
$Column = 7
$workbook.Cells.Item($row,$column)= "$DataReadDate" 

#Version
$Version = select-string -path "$location" -pattern "Version:"
$Version = $Version.line
$Version = $Version -replace "Version: " -replace ""
$row = $i
$Column = 6
$workbook.Cells.Item($row,$column)= "$Version"

#How Many Times Unresponsive Shows Up
$Unresponsive = (Get-Content "$location" | select-string -pattern "Unresponsive").length
$row = $i
$Column = 4
$workbook.Cells.Item($row,$column)= "$Unresponsive"
 
#How Many Times Disconnected Shows Up
$Disconnected = (Get-Content "$location" | select-string -pattern "Disconnected").length
$row = $i
$Column = 3
$workbook.Cells.Item($row,$column)= "$Disconnected"

#How Many Times Unreachable host Shows Up
$Unreachable = (Get-Content "$location" | select-string -pattern "Unreachable host").length
$row = $i
$Column = 5
$workbook.Cells.Item($row,$column)= "$Unreachable"

#How Many Times OK Shows Up
$OK = (Get-Content "$location" | select-string -pattern "OK").length
$row = $i
$Column = 2
$workbook.Cells.Item($row,$column)= "$OK"
#Iterate by one so each text file goes to its own line.
$i++
}

#Save Document
$output = "\Output.xlsx"
$FinalOutput = $move+$output
#saving & closing the file
$workbook.SaveAs($move)
$excel.Quit()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-06
    • 2015-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 2021-10-19
    • 2020-11-05
    相关资源
    最近更新 更多