PowerShell脚本执行Import-Csv报错:The member '0' is already present
问题:PowerShell读取无表头CSV触发"The member '0' is already present"错误
错误信息
Import-Csv : The member "0" is already present. At D:\OpNetTags\scripts\PiconfigProcUtililty1.0.ps1:8 char:12 + $records = Import-Csv -Path $inputFile + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Import-Csv], ExtendedTypeSystemException + FullyQualifiedErrorId : AlreadyPresentPSMemberInfoInternalCollectionAdd,Microsoft.PowerShell.Commands.ImportCsvCommand
现有代码
# Define the input and log file paths $inputFile = "D:\OpNetTags\tags\00_UFL_TEST_PROC.csv" $logFile = "D:\OpNetTags\logs" # Read the CSV file $records = Import-Csv -Path $inputFile # Process each record foreach ($record in $records) { # Construct the PIConfig command $piconfigCommand = @" @table pipoint, classic @mode create, edit @istr tag,descriptor,digitalset,engunits,pointsource,pointtype,span,step,typicalvalue,zero,instrumenttag,location1,location2,location3,location4,location5 $($record | ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1) @ends "@ # Run the PIConfig command (assuming piconfig.exe is in the PATH) D:/OpNetTags/piconfig.exe -cmd $piconfigCommand # Log the processed record $record | Out-File -FilePath $logFile -Append }
输入文件示例(片段)
ProspectPark_ProspectPark - %Missing,2.01102009_1,,_BLANK,UFL_BKL_ANA,Float32,100,0,50,0,2.01102009_1,1,0,0,2,0 ProspectPark_ProspectPark - Blown Fuse Threshold,2.01102015_1,,Amps,UFL_BKL_ANA,Float32,100,0,50,0,2.01102015_1,1,0,0,2,0 ProspectPark_ProspectPark - Min Amp Threshold,2.01102016_1,,Amps,UFL_BKL_ANA,Float32,100,0,50,0,2.01102016_1,1,0,0,2,0 ProspectPark_ProspectPark - Open NWP Threshold,2.01102014_1,,Perct,UFL_BKL_ANA,Float32,100,0,50,0,2.01102014_1,1,0,0,2,0 ProspectPark_ProspectPark - SOCCS MW,2.01102011_1,,MW,UFL_BKL_ANA,Float32,100,0,50,0,2.01102011_1,1,0,0,2,0 ProspectPark_ProspectPark - Total Reporting Threshold,2.01102013_1,,Perct,UFL_BKL_ANA,Float32,100,0,50,0,2.01102013_1,1,0,0,2,0 RichmondHill_RichmondHill - #Blown Fuses,2.01100805_1,,_BLANK,UFL_BKL_ANA,Float32,100,0,50,0,2.01100805_1,1,0,0,2,0 RichmondHill_RichmondHill - #Feeders,2.01100807_1,,_BLANK,UFL_BKL_ANA,Float32,100,0,50,0,2.01100807_1,1,0,0,2,0
问题原因
使用Import-Csv读取无表头CSV时,PowerShell会自动生成数字序列的列名(0、1、2...),若文件存在解析异常(比如部分行字段数不一致、编码问题或隐藏字符),就会触发成员重复的错误。此外,你的场景无需将CSV行转成对象——只需原始行内容拼接PIConfig命令,Import-Csv属于多余操作。
解决方案
改用Get-Content直接读取每行原始内容,跳过对象转换步骤:
# Define the input and log file paths $inputFile = "D:\OpNetTags\tags\00_UFL_TEST_PROC.csv" $logFile = "D:\OpNetTags\logs" # Read all lines from the file $records = Get-Content -Path $inputFile # Process each line foreach ($line in $records) { # Skip empty lines to avoid invalid commands if ([string]::IsNullOrWhiteSpace($line)) { continue } # Construct the PIConfig command $piconfigCommand = @" @table pipoint, classic @mode create, edit @istr tag,descriptor,digitalset,engunits,pointsource,pointtype,span,step,typicalvalue,zero,instrumenttag,location1,location2,location3,location4,location5 $line @ends "@ # Run the PIConfig command & "D:\OpNetTags\piconfig.exe" -cmd $piconfigCommand # Log the processed line $line | Out-File -FilePath $logFile -Append }
额外说明
- 新增空行判断,避免生成无效的PIConfig命令
- 用
&调用外部程序,路径加双引号更规范 - 直接使用原始行内容,完全匹配
@istr字段顺序,避免对象转换带来的格式偏差
内容的提问来源于stack exchange,提问作者NanoNet
相关产品推荐
相关产品推荐

