Sql server 使用powershell使行成为txt中的列
我有一个包含不同数据的输入txt文件,如下所示:Sql server 使用powershell使行成为txt中的列,sql-server,powershell,Sql Server,Powershell,我有一个包含不同数据的输入txt文件,如下所示: ------------------------------------------------------------------------ Current Local Time: Fri Jul 01 04:54:27 2016 Current GMT Time: Thu Jun 30 20:54:27 2016 Machine ID: 6090
------------------------------------------------------------------------
Current Local Time: Fri Jul 01 04:54:27 2016
Current GMT Time: Thu Jun 30 20:54:27 2016
Machine ID: 6090
Machine Name: WL6090
Display S/N: 0253G020TG
Terrain version: 5.3.843.843
CAESTaskList DLL version: 5.3.843.843
Application Type: Terrain / CAES Ultra
Operating System: 1.04
Total RAM used by active files: 339.72 kB
----------------------------------------------------------------------------------------------------------
Disk Space
Free Disk Space: 6758 MB Total Disk Space: 7076 MB
----------------------------------------------------------------------------------------------------------
Current Local Time: 07-01-16, 04:54:27
Current Service Hours: 314.41
$Location = "D:\terrain_dia\Diag01Jul2016_045427.dia"
$a = Get-Content D:\terrain_dia\Diag01Jul2016_045427.dia
#Common
$option = [System.StringSplitOptions]::RemoveEmptyEntries
#ID
$LineID = Select-String -Path $Location -Pattern "Machine ID:" | Select-Object -Expand LineNumber
$ID = ($a)[$LineID-1]
$ID -replace '(?:\s|\r|\n)',''
#Name
$LineName = Select-String -Path $Location -Pattern "Machine Name:" | Select-Object -Expand LineNumber
$Name = ($a)[$LineName-1]
$Name -replace '(?:\s|\r|\n)',''
#SN
$LineSN = Select-String -Path $Location -Pattern "Display S/N:" | Select-Object -Expand LineNumber
$SN = ($a)[$LineSN-1]
$SN -replace '(?:\s|\r|\n)',''
#Version
$LineVersion = Select-String -Path $Location -Pattern "Terrain version:" | Select-Object -Expand LineNumber
$Version = ($a)[$LineVersion-1]
$Version -replace '(?:\s|\r|\n)',''
#RAM
$LineRam = Select-String -Path $Location -Pattern "Total RAM" | Select-Object -Expand LineNumber
$RAM = ($a)[$LineRam-1]
$RAM -replace '(?:\s|\r|\n)',''
#GPSSN
$LineGPSSN = Select-String -Path $Location -Pattern "GPS Receiver SN:" | Select-Object -Expand LineNumber
$GPSSN = ($a)[$LineGPSSN-1]
$GPSSN -replace '(?:\s|\r|\n)',''
#GPSType
$LineGPSType = Select-String -Path $Location -Pattern "GPS Receiver Type:" | Select-Object -Expand LineNumber
$GPSType = ($a)[$LineGPSType-1]
$GPSType -replace '(?:\s|\r|\n)',''
#NAV
$LineNAV = Select-String -Path $Location -Pattern "NAV firmware version:" | Select-Object -Expand LineNumber
$NAV = ($a)[$LineNAV-1]
$NAV -replace '(?:\s|\r|\n)',''
#SIG
$LineSig = Select-String -Path $Location -Pattern "SIG firmware version:" | Select-Object -Expand LineNumber
$SIG = ($a)[$LineSig-1]
$SIG -replace '(?:\s|\r|\n)',''
#ROM
$LineROM = Select-String -Path $Location -Pattern "ROM firmware version:" | Select-Object -Expand LineNumber
$ROM = ($a)[$LineROM-1]
$ROM -replace '(?:\s|\r|\n)',''
我每天都有很多这样的文件。
作为一名真正的powershell新手,我试图从这些文件中获取我所需的信息,我的做法如下:
------------------------------------------------------------------------
Current Local Time: Fri Jul 01 04:54:27 2016
Current GMT Time: Thu Jun 30 20:54:27 2016
Machine ID: 6090
Machine Name: WL6090
Display S/N: 0253G020TG
Terrain version: 5.3.843.843
CAESTaskList DLL version: 5.3.843.843
Application Type: Terrain / CAES Ultra
Operating System: 1.04
Total RAM used by active files: 339.72 kB
----------------------------------------------------------------------------------------------------------
Disk Space
Free Disk Space: 6758 MB Total Disk Space: 7076 MB
----------------------------------------------------------------------------------------------------------
Current Local Time: 07-01-16, 04:54:27
Current Service Hours: 314.41
$Location = "D:\terrain_dia\Diag01Jul2016_045427.dia"
$a = Get-Content D:\terrain_dia\Diag01Jul2016_045427.dia
#Common
$option = [System.StringSplitOptions]::RemoveEmptyEntries
#ID
$LineID = Select-String -Path $Location -Pattern "Machine ID:" | Select-Object -Expand LineNumber
$ID = ($a)[$LineID-1]
$ID -replace '(?:\s|\r|\n)',''
#Name
$LineName = Select-String -Path $Location -Pattern "Machine Name:" | Select-Object -Expand LineNumber
$Name = ($a)[$LineName-1]
$Name -replace '(?:\s|\r|\n)',''
#SN
$LineSN = Select-String -Path $Location -Pattern "Display S/N:" | Select-Object -Expand LineNumber
$SN = ($a)[$LineSN-1]
$SN -replace '(?:\s|\r|\n)',''
#Version
$LineVersion = Select-String -Path $Location -Pattern "Terrain version:" | Select-Object -Expand LineNumber
$Version = ($a)[$LineVersion-1]
$Version -replace '(?:\s|\r|\n)',''
#RAM
$LineRam = Select-String -Path $Location -Pattern "Total RAM" | Select-Object -Expand LineNumber
$RAM = ($a)[$LineRam-1]
$RAM -replace '(?:\s|\r|\n)',''
#GPSSN
$LineGPSSN = Select-String -Path $Location -Pattern "GPS Receiver SN:" | Select-Object -Expand LineNumber
$GPSSN = ($a)[$LineGPSSN-1]
$GPSSN -replace '(?:\s|\r|\n)',''
#GPSType
$LineGPSType = Select-String -Path $Location -Pattern "GPS Receiver Type:" | Select-Object -Expand LineNumber
$GPSType = ($a)[$LineGPSType-1]
$GPSType -replace '(?:\s|\r|\n)',''
#NAV
$LineNAV = Select-String -Path $Location -Pattern "NAV firmware version:" | Select-Object -Expand LineNumber
$NAV = ($a)[$LineNAV-1]
$NAV -replace '(?:\s|\r|\n)',''
#SIG
$LineSig = Select-String -Path $Location -Pattern "SIG firmware version:" | Select-Object -Expand LineNumber
$SIG = ($a)[$LineSig-1]
$SIG -replace '(?:\s|\r|\n)',''
#ROM
$LineROM = Select-String -Path $Location -Pattern "ROM firmware version:" | Select-Object -Expand LineNumber
$ROM = ($a)[$LineROM-1]
$ROM -replace '(?:\s|\r|\n)',''
我得到了输出:
MachineID:6090
MachineName:WL6090
DisplayS/N:0253G020TG
Terrainversion:5.3.843.843
TotalRAMusedbyactivefiles:339.72kB
GPSReceiverSN:3351J508SP
GPSReceiverType:MS992
NAVfirmwareversion:00506
SIGfirmwareversion:00506
ROMfirmwareversion:00425
FreeDiskSpace:6758MB
TotalDiskSpace:7076MB
所以我有一个固定的分隔符
但我真正需要的是:
MachineID MachineName DisplayS/N
6090 WL6090 0253G020TG
等等
所以我需要将这个输出格式化为txt文件中的一个表,然后我将把它的结果放入MS SQL db中
请告知如何使用powershell格式化所有这些行
任何帮助都是非常有用的
谢谢像这样解析文档可能有点烦人,但上面的内容非常简单
$InputFile.replace("MB","MB`n") -split "\n" |
Select-String ":" | % {
$_.Line.Trim() -replace ":\s+","`t"
} | ConvertFrom-Csv -Delimiter "`t" -Header "Detail","Value"
这将以字符串形式获取输入,然后:
- 在
值后添加一个换行符,因为一行上有多个换行符MB
- 仅选择其中包含
的行,以排除垃圾和空白:
- 将其转换为带有标题的CSV,以便于处理
Detail Value
------ -----
Current Local Time Fri Jul 01 04:54:27 2016
Current GMT Time Thu Jun 30 20:54:27 2016
Machine ID 6090
etc...
您可以使用以下命令获得长格式输出:
$c.Detail -join "`t"
$c.Value -join "`t"
希望这能有所帮助,我已经多次使用代码(不包括
MB
->MB\n
部分)解析类似的日志文件。这一行程序将处理当前文件夹中的所有*.dia文件,并使用正则表达式过滤包含冒号的所有行,构建压缩的名称-值对,并显示它们的格式:
Select-String .\*.dia -Pattern '^([^:]+):\s+(.*)$' | ForEach-Object { "{0,-20} {1,-25}: {2}" -f $_.FileName,$($_.Matches.Groups[1].Value -replace ' '), ($_.Matches.Groups[2].Value) }
样本输出:
Sample1.dia CurrentLocalTime : Fri Jul 01 04:54:27 2016
Sample1.dia CurrentGMTTime : Thu Jun 30 20:54:27 2016
Sample1.dia MachineID : 6090
Sample1.dia MachineName : WL6090
Sample1.dia DisplayS/N : 0253G020TG
Sample1.dia Terrainversion : 5.3.843.843
Sample1.dia CAESTaskListDLLversion : 5.3.843.843
Sample1.dia ApplicationType : Terrain / CAES Ultra
以下PowerShell脚本使用相同的正则表达式并构建[PSCustomObject]并向其附加所有属性(此时文件中出现两次错误,错误为CurrentLocalTime
,TotalDiskSpace与FreeDiskSpace在同一行上)否则,它会累积在文件中找到的所有数据
CumulatedDia.Csv
保存它,并通过Out GridView重新导入以显示内容
我将这两个缺陷留给您/其他人修复
$CumDia = ".\CumulatedDia.Csv"
Remove-Item $CumDia -ErrorAction Ignore
ForEach($File IN (Get-ChildItem *.dia)){
$DiaFile = $Null
$DiaFile = [PSCustomObject]@{'FileOrigin' = $File}
Select-String -Path $File -Pattern '^([^:]+):\s+(.*)$' | ForEach-Object {
$DiaFile | Add-Member -EA SilentlyContinue `
-NotePropertyName ($_.Matches.Groups[1].Value -replace ' ')`
-NotePropertyValue $_.Matches.Groups[2].Value
}
$DiaFile | fl
$DiaFile | Export-Csv -Path $CumDia -Append -NoTypeInformation
}
Import-Csv $CumDia|Out-GridView
不需要的属性/列可以通过选择对象删除。
这是单行表的格式列表输出
FileOrigin : Q:\Test\2017\07\25\Sample1.dia
CurrentLocalTime : Fri Jul 01 04:54:27 2016
CurrentGMTTime : Thu Jun 30 20:54:27 2016
MachineID : 6090
MachineName : WL6090
DisplayS/N : 0253G020TG
Terrainversion : 5.3.843.843
CAESTaskListDLLversion : 5.3.843.843
ApplicationType : Terrain / CAES Ultra
OperatingSystem : 1.04
TotalRAMusedbyactivefiles : 339.72 kB
FreeDiskSpace : 6758 MB Total Disk Space: 7076 MB
CurrentServiceHours : 314.41
GPSReceiverSN : 3351J508SP
GPSReceiverType : MS992
NAVfirmwareversion : 00506
SIGfirmwareversion : 00506
ROMfirmwareversion : 00425
示例GridView pciture是否要生成文件并将其放入SQL Server?您是否可以将其直接放入SQL Server?此文件是否始终包含一台计算机的信息?或者它可能有几个MachineID?如果要导入SQL server,最好直接生成SQL语句,而不是在列中格式化。我可以帮你解决这个问题,但当我在Windows 7系统上运行你的代码时,我发现
无法索引到空数组错误,我不会花时间调试:)谢谢@ThomasG,我想这是因为powershell版本的不同。。我用Win 10运行它。@RigertaDemiri始终只有一个机器ID。所以故事是这样的-有几个机器,每个机器都有一个这样的文件。谢谢ConnorLSW。我会检查并回复您我这样做:$InputFile='D:\terrain\u dia\out.txt'$InputFile.replace(“MB”,“MB
n”)-split“\n”| Select String:“{$.Line.Trim()-replace”:\s+,“t”}ConvertFrom Csv-分隔符“
t”-Header“Detail”,“Value”`我得到的结果是这样的:Detail Value------D:\terrain\u dia\out.txt
我做错了什么?)你需要做$InputFile=Get Content'D:\terrain\u dia\out.txt'
首先从文件中取出文本。谢谢@LotPings。对不起,迟了答复。我正在检查。