我试图获得powershell脚本的解决方案,以便将其从非结构化文本转换为csv格式,而且如果'TechnologyType'包含不同的数据但附加到相同的服务器名称,我将坚持如何重复更新服务器名称。我只发现了直接更新,但是这种非结构化的逐行读取方式有些不同。
来源:
Servername=svr343 TechnologyType=Storage ID=100 DiskSize=3gb Drive=PhysicalDrive Type=SCSI TechnologyType=Storage ID=110 DiskSize=9gb Drive=PhysicalDrive Type=SCSI TechnologyType=Interface ID=200 localhostname=svr343 ipaddress=1.1.1.1 TechnologyType=Interface ID=220 localhostname=svr343 ipaddress=2.2.2.2 Servername=svr400 TechnologyType=Storage ID=180 DiskSize=5gb Drive=PhysicalDrive Type=SCSI TechnologyType=Storage ID=190 DiskSize=15gb Drive=PhysicalDrive Type=SCSI TechnologyType=Interface ID=250 localhostname=svr400 ipaddress=5.5.5.5 TechnologyType=Interface ID=260 localhostname=svr400 ipaddress=6.6.6.6
CSV输出:
"ServerName","TechnologyType","ID","DIskSize","Drive","Type","localhostname","IPAddress" "svr343","Storage","100","3gb","PhysicalDrive","SCSI","","" "svr343","Storage","110","9gb","PhysicalDrive","SCSI","","" "svr343","Interface","200","","","","svr343","1.1.1.1" "svr343","Interface","220","","","","svr343","2.2.2.2" "svr400","Storage","180","5gb","PhysicalDrive","SCSI","","" "svr400","Storage","190","15gb","PhysicalDrive","SCSI","","" "svr400","Interface","250","","","","svr343","5.5.5.5" "svr400","Interface","260","","","","svr343","6.6.6.6"
答案 0 :(得分:0)
这是一个可能的解决方案。
我选择不对其进行评论,因此,您需要尝试自己理解它。
最后使用Export-Csv
将数据导出到csv中。
$data = @"
Servername=svr343
TechnologyType=Storage
ID=100
DiskSize=3gb
Drive=PhysicalDrive
Type=SCSI
TechnologyType=Storage
ID=110
DiskSize=9gb
Drive=PhysicalDrive
Type=SCSI
TechnologyType=Interface
ID=200
localhostname=svr343
ipaddress=1.1.1.1
TechnologyType=Interface
ID=220
localhostname=svr343
ipaddress=2.2.2.2
Servername=svr400
TechnologyType=Storage
ID=180
DiskSize=5gb
Drive=PhysicalDrive
Type=SCSI
TechnologyType=Storage
ID=190
DiskSize=15gb
Drive=PhysicalDrive
Type=SCSI
TechnologyType=Interface
ID=250
localhostname=svr400
ipaddress=5.5.5.5
TechnologyType=Interface
ID=260
localhostname=svr400
ipaddress=6.6.6.6
"@
# Convert data
$outData = @()
$data -split 'Servername=' | foreach {
$_ -match '([\w|\d]+)\s' | Out-Null
$serverName = $Matches[1]
$pattern = [Environment]::NewLine+"\s*"+[Environment]::NewLine
$_ -split $pattern | foreach {
try {
$stringData = ConvertFrom-StringData -StringData $_
$stringData.Add("ServerName", $serverName)
$outData += $stringData
} catch { }
}
}
# Output data
$outData[3]
答案 1 :(得分:0)
要读取和解析这样的文件,以下代码应该对您有用:
# read the textfile in as one single string
$serverInfo = Get-Content 'FULL PATH TO THE UNSTRUCTURED TEXT FILE' -Raw
$outputFile = 'FULL PATH TO THE RESULTING CSV FILE'
$result = @()
# match all 'Servername' blocks of text
$regex = [regex]'(?s)Servername=((?!Servername=).)*'
$serverBlocks = $regex.Matches($serverInfo)
foreach ($server in $serverBlocks) {
# get the server name from this text block. If missing, call it 'unknown'
$serverName = if ($server -match 'Servername=(.*)') { $matches[1].Trim() } else { 'unknown' }
# match all 'Technology' (server details) blocks of text
$regex = [regex]'(?s)TechnologyType=((?!TechnologyType=).)*'
$detailsBlocks = $regex.Matches($server.Value)
foreach ($details in $detailsBlocks) {
# create an object for output
$data = "" | Select-Object 'ServerName','TechnologyType','ID','DiskSize','Drive','Type','LocalHostname','IPAddress'
$data.ServerName = $serverName
# split the Technology block into separate strings and read the name/value pairs
$details.Value.Trim() -split '\r?\n' | ForEach-Object {
$name, $value = $_ -split '=', 2
$value = $value.Trim()
switch ($name) {
'TechnologyType' { $data.TechnologyType = $value; break }
'ID' { $data.ID = $value; break }
'DiskSize' { $data.DiskSize = $value; break }
'Drive' { $data.Drive = $value; break }
'Type' { $data.Type = $value; break }
'localhostname' { $data.LocalHostname = $value; break }
'ipaddress' { $data.IPAddress = $value; break }
}
}
# add the object to the result array
$result += $data
}
}
# save the info as CSV
$result | Export-Csv $outputFile -NoTypeInformation
此后,输出CSV文件包含:
"ServerName","TechnologyType","ID","DiskSize","Drive","Type","LocalHostname","IPAddress" "svr343","Storage","100","3gb","PhysicalDrive","SCSI",, "svr343","Storage","110","9gb","PhysicalDrive","SCSI",, "svr343","Interface","200",,,,"svr343","1.1.1.1" "svr343","Interface","220",,,,"svr343","2.2.2.2" "svr400","Storage","180","5gb","PhysicalDrive","SCSI",, "svr400","Storage","190","15gb","PhysicalDrive","SCSI",, "svr400","Interface","250",,,,"svr400","5.5.5.5" "svr400","Interface","260",,,,"svr400Servername","6.6.6.6"