在powershell中分隔CSV中列的值



我有多个文件在一个单元格中包含一个人的全名。我想把这些名字分为两列——名字和姓氏

我用来分离值的代码在我以前的脚本迭代中工作过,但现在不再工作

我无法确定错误在哪里,有人能给我建议吗?

$path          = 'C:MAY2019correct'
#XYZ
$excelOut      = Join-Path -Path $path -ChildPath 'XYZ.csv'
$completedFile = Join-Path -Path $path -ChildPath 'Completed-XYZ.csv'
$defaultValue  = 'ABC'
$filter        = '*XYZ*'
$excelFile     = Get-ChildItem -Path $path -Filter $filter -File | 
Select-Object -First 1
$allstaff = @()
if ($excelFile) {
$excel = New-Object -ComObject Excel.Application -Property @{Visible = 
$false} 
# Open the file
$workbook = $excel.Workbooks.Open($excelFile.FullName) 
# Activate the first worksheet
$sheet = $workbook.Sheets.Item(1) 
[void]$sheet.Cells.Item(1, 1).EntireRow.Delete() # Delete the first row
[void]$sheet.Cells.Item(1, 1).EntireRow.Delete() # Delete the 2 row
[void]$sheet.Cells.Item(1, 1).EntireRow.Delete() # Delete the 3 row
$workbook.SaveAs($excelOut,6)
# Close workbook and save changes
$workbook.Close($true) 
# Quit Excel
$excel.Quit()
# clean-up Com objects
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($sheet) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbook) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
$headers = 'Element Name','Surname','EmployeeNo','Amount','YTD'
# import the csv file and select all the above headers plus one that is created using a calculated property
$csv = Import-Csv -Path $excelOut -Header $headers -UseCulture | Select-Object *, @{Name = 'Paycentre'; Expression = {$defaultValue}}|

Write-Host "Creating completed csv file '$completedFile'"
$csv | Export-Csv -Path $completedFile -UseCulture -Force - 
NoTypeInformation
}
else {
Write-Warning "Could not find a file using filter '$filter' in path 
'$path'"
}
foreach($staff in $completedFile)
{
#Get the values from the CSV for this row
$Surname = $staff.Surname
$Surname = $Surname.Substring(0, $Surname.lastIndexOf(' '))
$Initial = $staff.Surname
$Initial = $Initial.Substring($Initial.lastIndexOf(' ') + 1 )
$Firstname = $staff.Surname
$Firstname = $Firstname.Substring($Initial.lastIndexOf(' ') + 1 )
$EmployeeNo = $staff.EmployeeNo
$NINumber = $staff.NINumber
$Amount = $staff.Amount
$Paycentre = $staff.Paycentre
$staff2  = New-Object System.Object
$staff2 | Add-Member -MemberType NoteProperty -Name "Surname" -Value $Surname
$staff2 | Add-Member -MemberType NoteProperty -Name "FirstName" -Value $Firstname
$staff2 | Add-Member -MemberType NoteProperty -Name "EmployeeNo" -Value $EmployeeNo
$staff2 | Add-Member -MemberType NoteProperty -Name "NINumber" -Value $NINumber
$staff2 | Add-Member -MemberType NoteProperty -Name "Amount" -Value $Amount
$staff2 | Add-Member -MemberType NoteProperty -Name "FirstName" -Value $Initial
$staff2 | Add-Member -MemberType NoteProperty -Name "Paycentre" -Value $Paycentre
#add to array
$allstaff += $staff2
} 
$allstaff | Export-Csv -NoTypeInformation -Path $completedFile

我看到的第一件事是,您的foreach循环正在$completedFile变量上迭代,该变量看起来是文件路径,而不是集合。

应该改为foreach ($staff in $csv)吗?

此外,在处理代码中的名称时要非常小心。很容易做出糟糕的假设来打破现状。有关更多信息,请参阅Falsehoods Programmers Believe About Names。

最新更新