导入功能在PowerShell Core中不起作用,但在PowerShell中

时间:2019-03-11 19:19:58

标签: powershell

我希望有人可以帮助我。我在PowerShell中构建了一个函数来导入具有两个标题行的定界文件。我将两个标题行合并为一个并添加以下数据。在PowerShell中可以正常工作:

$Delimiter = ";"
# Load 2nd and 3rd row which contain header informstion

$imData = (Get-Content -Path $csv_file | Select-Object -Skip 1 | select -First 2 ).split($Delimiter)
$columnsCount = $imData.Count/2
# Check if first row header is emtpy, if not, add information to second row

for($i=0; $i -lt $columnsCount;$i++){
    if(![string]::IsNullOrEmpty($imData[$i].Substring(1,$imData[$i].Length-2))){
        $imData[$i+$columnsCount] = $imData[$i] #.Substring(1,$imData[$i].Length-2)
    } 
}

$csvData = Get-Content -Path $csv_file | Select-Object -Skip 3
# Remove first row, second row contains header
$imData = $imData[$columnsCount..($imData.Length-1)]
$csvData = $csvData | ConvertFrom-Csv -Delimiter $Delimiter -WarningAction Ignore -Header $imData

最后的$ csvData是正确的,我可以解决。 但是,在PowerShell Core中它不起作用。它以Get-Content命令开头,该命令不会在每列的开头和结尾自动添加“。其次,当我在PowerShell Core中调用此代码时,它不会设置标头。实际上,它甚至不能像在PowerShell中那样创建正确的csv对象。在我的核心代码下方:

$delimiter = ";"
#[array]$header = ::new()
# Load 2nd and 3rd row which contain header informstion
[array]$imData = (Get-Content -Path $path | Select-Object -Skip 1 | Select-Object -First 2 ).split($delimiter)

# Set header count
$columnsCount = $imData.Count/2

# Check if first row header is emtpy, if not, add information to second row
for($i=0; $i -lt $columnsCount;$i++){

    # Check if column header is empty
    if(![string]::IsNullOrEmpty($imData[$i])){
        #if(![string]::IsNullOrEmpty($imData[$i].Substring(1,$imData[$i].Length-2))){
        $imData[$i+$columnsCount] = $imData[$i] #.Substring(1,$imData[$i].Length-2)
    } 
}

# Import CSV without header
$csvData = Get-Content -Path $path | Select-Object -Skip 3

# Remove first row, second row contains header
$imData = $imData[$columnsCount..($imData.Length-1)]
# Add header to CSV
$header = ((($imData -split (" " )))) | ConvertFrom-Csv -Delimiter $delimiter 

$csvData =  $csvData | ConvertFrom-Csv -Delimiter $delimiter -Header $header
$csvData = $header + $csvData

我已经尝试了不同的方法,但是似乎没有任何效果。 我希望有人能给我锦上添花。 谢谢 斯蒂芬

1 个答案:

答案 0 :(得分:0)

 static [Object] loadCsv([String] $path){

        $delimiter = ";"
        #[array]$header = ::new()
        # Load 2nd and 3rd row which contain header informstion
        [array]$csvHeader = (Get-Content -Path $path | Select-Object -Skip 1 | Select-Object -First 2 ).split($delimiter)

        # Set header count
        $csvColumnsCount = $csvHeader.Count/2

        # Check if first row header is emtpy, if not, add information to second row
        for($i=0; $i -lt $csvColumnsCount;$i++){

            # Check if column header is empty
            if(![string]::IsNullOrEmpty($csvHeader[$i])){
                #if(![string]::IsNullOrEmpty($imData[$i].Substring(1,$imData[$i].Length-2))){
                $csvHeader[$i+$csvColumnsCount] = $csvHeader[$i] #.Substring(1,$imData[$i].Length-2)
            } 
        }

        # Remove first row, second row contains header
        $csvHeader = $csvHeader[$csvColumnsCount..($csvHeader.Length-1)]

        # Import CSV and add header
        $csvData = Get-Content -Path $path | Select-Object -Skip 3 | ConvertFrom-Csv -Delimiter $delimiter -Header $csvHeader

        return $csvData

    }