【发布时间】:2021-11-30 07:47:48
【问题描述】:
我有一个包含 10 列的 A.csv 文件,除了最后一列之外,所有列都有一个名称。
我想生成一个带有标题"ID", "Type", "Name", "Documentation" 的新 D.csv 文件。
刚开始工作时,我给这个空列起了个名字,因为我不知道如何读取 powershell 中空列的值。 "TechnologyEvent"这种类型我不能做,请你帮帮我?
fichier A.Csv
Jobstream,Jobstream Description,Op num, Job,Script or expected file(s), Server, user,location,Job Description,
PAXCWEBX965H,"RMNF - xWEBX1","9","","technical","","","","Begin Of JobStream","05h00"
PAXCWEBX965H,"RMNF - xWEBX1","40","PWEBX0GJ","PWEBX-965H-005S.KSH","PRAXCWBXLBDD01","svc_bddo_user","F+WEBX-027","Lancement Sauv. RMAN","05h00"
Fichier D.csv
ID,"Type","Name","Documentation"
79570056-ab4f-6969-8c58-a5bd5847a895,"TechnologyInteraction","WEBX965H","RMNF - xWEBX1"
b57a5fff-9dd2-4cm1-9141-04c042f49498,"TechnologyService","WEBX965H-005S","Lancement Sauv. RMAN"
id-bdca3a1b39ed49cd80ae41deaa88094c,"TechnologyEvent","Lancement Sauv. RMAN 05:00",""
function newElements{
process{
# import data
$data = Import-csv -Path $env:USERPROFILE\A.csv -Delimiter ';'
$NewExtract_AGRe_TWS_ALL_20200925= ForEach($Entry in $ElementCsv){
# -or [string]::IsNullOrEmpty($Entry."Script or expected file(s)")
if ( ($Entry."Script or expected file(s)" -ilike 'technical') -or [string]::IsNullOrEmpty($Entry."Script or expected file(s)") ) {
$Entry."Jobstream"=$Entry."Jobstream" -replace '^(?:PAXC)?(.+?)','$1'
}
else {
$Entry."Jobstream" = $Entry.'Script or expected file(s)' -replace '^(?:P|A|X|C)?(.+?)(\.(?:BAT|KSH)|$)','$1' -replace '^(.{4})-','$1'
}
$Entry
}
# Export Extract_AGRe_TWS_ALL_20211005.csv in new B.csv file
$NewExtract_AGRe_TWS_ALL_20200925 | Export-Csv $env:USERPROFILE\B.csv -NoTypeInformation -Encoding UTF8
$NewExtract_AGRe_TWS_ALL_20200925Jobstream= Import-Csv $env:USERPROFILE\B.csv | Where { $oldElementsCsv.Name -notcontains $_.Jobstream}| Export-Csv $env:USERPROFILE\C.csv -NoTypeInformation -Encoding UTF8
$ImportCsv=Import-Csv $env:USERPROFILE\C.csv
#Output progress:
$output= @()
#Jobstream, Jobstream Description,Op num, Job,Script or expected file(s),Server, user, location, Job Description
ForEach ($column in $ImportCsv){
$pattern='^(.*)-'
$pattern2='^(.*)_'
if($column.Jobstream -notmatch $pattern){
$output1= New-Object PsObject -Property @{"ID"=[guid]::NewGuid().ToString(); "Type"="TechnologyInteraction"; "Name"= $column.Jobstream; "Documentation"=$column."Job Description" + " `r`nSever: $($column.Server) `r`nuser: $($column.user) " ; "Planification"= $column.Planification }
Write-Warning "Found new Jobstream : $($column.Jobstream)"
Write-Warning "Found new Jobstream Description : $($column."Job Description")"
$output= $output + $output1
}
elseif($column.Jobstream -match $pattern2)
{
$output2= New-Object PsObject -Property @{"ID"=[guid]::NewGuid().ToString(); "Type"="TechnologyService"; "Name"=$column.Jobstream; "Documentation"= $column."Job Description"+ " `r`nSever: $($column.Server) `r`nuser: $($column.user) " ; "Planification"= $column.Planification }
Write-Warning "New Jobstream : $($column.Jobstream)"
Write-Warning "New Jobstream Description : $($column."Job Description")"
$output= $output + $output2
}
elseif (![string]::IsNullOrEmpty($column.Planification) ){
$output4= New-Object PsObject -Property @{"ID"=[guid]::NewGuid().ToString(); "Type"="TechnologyEvent"; "Name"= $column."Job Description" + " " + $column.Planification ; "Documentation"= "" }
Write-Warning "New Type TechnologyEvent : $( $column."Job Description")"
Write-Warning "New Type TechnologyEvent Description : $($column."Job Description")"
$output= $output + $output4
}
else{
$output3= New-Object PsObject -Property @{"ID"=[guid]::NewGuid().ToString(); "Type"="TechnologyService"; "Name"= $column.Jobstream; "Documentation"= $column."Job Description" + " " + $column.Planification + " `r`nSever: $($column.Server) `r`nuser: $($column.user)" }
Write-Warning "New Jobstream : $($column.Jobstream)"
Write-Warning "New Jobstream Description : $($column."Job Description")"
$output= $output + $output3
}
}
$output |Select-Object -Property "ID","Type","Name","Documentation" -Unique| Export-Csv $path\D.csv -NoTypeInformation -Encoding UTF8
#open new File:
notepad $path\D.csv
Write-Host "Ending newElements "
}
}newElements
【问题讨论】:
-
你的缩进太可怕了。请修复它,以便清楚地知道 foreach、if、elseif 等何时开始和结束。然后,示例中的 csv 文件使用 comma 作为分隔符,因此如果您的真实文件是这种情况,请删除
-Delimiter ';' -
“我给这个空列起了个名字” - 怎么样?在哪里?什么空栏?如果您想要一个包含
"ID","Type","Name","Documentation"列的 CSV 文件,您应该使用这四个属性名称创建 powershell 对象并将对象通过管道传输到Export-Csv或ConvertTo-Csv。 -
我没有将其作为列:Jobstream,Jobstream Description,Op num, Job,Script or expected file(s), Server, user,location,Job Description, 我将其转换为:Jobstream,Jobstream描述、操作编号、作业、脚本或预期文件、服务器、用户、位置、作业描述、计划
-
我把它变成了手写的
标签: windows powershell csv