我正在尝试从文本文件中提取特定的数据片段并将它们写入excel工作簿。最初我能够将数据提取到excel,但我只需要某些单元格的一部分,因此修改excel中的数据,只取得了一定的成功。我想在导出它之前在powershell中挑出特定的数据,但是已经碰壁了。 这就是我到目前为止所做的:
#User input relevant folder location
$i = read-host "Please enter Folder Location: "
#import data
Select-String -pattern " CISCO1941/K9 " -path "$i\*-Live.txt" | Export-CSV $i\InventoryRAW_Router.csv
#parse data
$RouterHNM = Import-CSV $i\InventoryRAW_Router.csv | select-object Line
$a = -split $RouterHNM
$b = $a -join ","
$b
返回:
@{Line=*0,CISCO1941/K9,FCxxxxxxxYR},@{Line=*0,CISCO1941/K9,FCxxxxxxxCF},@{Line=*0,CISCO1941/K9,FCxxxxxxxBN}
老实说,当我尝试使用以下方法导出数据时,我不知道下一步该做什么:
| Export-CSV $ i \ b.txt“
我明白了:
#TYPE System.String
“长度”
“107”
我似乎无法分割数据。
任何帮助将不胜感激!! 感谢
进一步信息: 示例文本文件数据从以下位置导入:
router01-Live#sh ver
Cisco IOS Software, xxx, RELEASE SOFTWARE (fc1)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2012 by Cisco Systems, Inc.
Compiled Tue 20-Mar-12 17:58 by prod_rel_team
ROM: System Bootstrap, Version xxx
router01 uptime is 1 minute
System returned to ROM by power-on
System restarted at xxx
System image file is "flash0:xxx.bin"
Last reload type: Normal Reload
This product contains cryptographic features and is subject to United
States and local country laws governing import, export, transfer and
use. Delivery of Cisco cryptographic products does not imply
third-party authority to import, export, distribute or use encryption.
Importers, exporters, distributors and users are responsible for
compliance with U.S. and local country laws. By using this product you
agree to comply with applicable laws and regulations. If you are unable
to comply with U.S. and local laws, return this product immediately.
A summary of U.S. laws governing Cisco cryptographic products may be found at:
http://www.cisco.com/wwl/export/crypto/tool/stqrg.html
If you require further assistance please contact us by sending email to
export@cisco.com.
Cisco CISCO1941/K9 (revision 1.0) with 491520K/32768K bytes of memory.
Processor board ID xxxx
DRAM configuration is 64 bits wide with parity disabled.
255K bytes of non-volatile configuration memory.
250880K bytes of ATA System CompactFlash 0 (Read/Write)
License Info:
License UDI:
-------------------------------------------------
Device# PID SN
-------------------------------------------------
*0 CISCO1941/K9 FCxxxxxxxYR
Technology Package License Information for Module:'c1900'
-----------------------------------------------------------------
Technology Technology-package Technology-package
Current Type Next reboot
------------------------------------------------------------------
ipbase ipbasek9 Permanent ipbasek9
security None None None
data None None None
Configuration register is 0x2102
并且excel输出应该是主机名,序列号和模型的简单列表,返回的数据显示如下: @ {行= * 0,CISCO1941 / K9,FCxxxxxxxYR},@ {行= * 0,CISCO1941 / K9,FCxxxxxxxCF},@ {行= * 0,CISCO1941 / K9,FCxxxxxxxBN}
包含模型&来自3个不同文本文件的主机名,所以我想从每个数组中提取第2和第3条信息到excel。
答案 0 :(得分:0)
示例,展示如何从每个文件中提取主机名,模型和序列并将其导出为CSV:
$sModel = "CISCO1941/K9"
$sLocation = Read-Host "Please enter Folder Location: "
$cRows = @()
foreach ($oFile in Get-ChildItem $sLocation -Filter "*-Live.txt") {
$cRawText = Get-Content -Path $oFile.FullName
$sHostName = ($cRawText | Select-Object -First 1) `
-replace '^(\w+)-.*', '$1'
$sModelSerial = $cRawText | Select-String -Pattern "\*.*$sModel"
$sSerial = $sModelSerial -replace ".*$Model[ \t]+(\w+)", '$1'
$oRow = New-Object PSObject -Property @{
"HostName" = $sHostName;
"Model" = $sModel;
"Serial" = $sSerial
}
$cRows += $oRow
}
$cRows | Export-Csv -NoTypeInformation outfile.csv
如果您需要导出到Excel,请使用Google Excel自动化或使用Export-XLSX。