【问题标题】:Display a CSV file in 2 columns in Powershell在 Powershell 中以 2 列显示 CSV 文件
【发布时间】:2019-11-22 14:06:33
【问题描述】:

我想读取 2 列中的 csv 文件,当我打开我的 excel 时,它将显示 2 列中显示的 CSV 文件的信息。

实际上,我只在一列中显示以下代码:

Function Read-File {
    Param([string]$file, [string]$head)
    if (Test-Path $file) {
        Write-Verbose "$file exists, importing list."
        $data = Import-Csv -Path $file
        $s = @()
        if ($data.$head) {
            foreach ($device in $data) {
                try {
                    $s += $device.$head
                    Write-Verbose -Message "$device.$head"
                } catch {
                    Write-Error -Message "Failed to add $device to list."
                }
            }
        } else {
            Write-Error -Message "No such column, $head, in CSV file $file."
        }

        return $s
    }
}

$list = Read-File $file $fileColumn

所以现在我想这样做,但是在 2 列中,我是 PowerShell 的初学者,所以我希望得到一些帮助:) 谢谢你

这是我的 CSV 文件:

Asset Number, Serial Number
cd5013172cffd6a317bd2a6003414c1,N5WWGGNL
8df47f5b1f1fcda12ed9d390c1c55feaab8,SN65AGGNL
dc0d1aaba9d55ee8992c657,B2NGAA3501119

我只是想在我的 excel 的 2 列上显示这些(资产编号和序列号),不用担心这些信息根本不敏感,所以没关系 :)

【问题讨论】:

  • 我已经修复了损坏的语法,@Theo。我认为如何从仅读取 1 个值(这是代码当前所做的)转换为 2 是 Jordy 所问的问题。
  • @mklement0 顺便说一句,它说不可能将 arg 链接到参数“path”,因为它是一个空字符串,在 if (test-path $file) 上:/什么我应该做我不确定
  • 这意味着您将$null 值或空字符串作为-File 参数传递给ReadFile 函数。您可以通过将 -File 参数声明为仅接受非空输入来更早地检测到此问题:[ValidateNotNullOrEmpty()][string]$file

标签: powershell csv


【解决方案1】:
  • 使用Select-Object 从输入对象中提取多个属性(包装在自定义对象实例中,[pscustomobject])。

  • 在您的函数中使用隐式输出 - 无需先将结果收集到数组中,尤其是不要使用+= 低效地构建它,它在幕后创建了一个 em> 每次迭代中的数组。

    • 请注意,从 PowerShell 中的函数返回(输出)数据从不需要 return;任何其输出既没有被捕获、抑制或重定向的命令都会影响函数的整体输出。
Function Read-File {

  # Not that $column (formerly: $head) now accepts an *array* of strings,
  # [string[]]
  Param([string] $file, [string[]] $column)

  if (Test-Path $file) {

    Write-Verbose "$file exists, importing list."

    # Import the CSV file, create custom objects with only the column(s)
    # of interest, and output the result.
    # Also store the result in variable $result via -OutVariable, for
    # inspection below.
    Import-Csv -Path $file | Select-Object -Property $column -OutVariable result

    # If at least 1 object was output, see if all columns specified exist
    # in the CSV data.
    if ($result.Count) {
      foreach ($col in $column) {
        if ($null -eq $result[0].$col) {
          Write-Error -Message "No such column in CSV file $file`: $column"
        }
      }  
    }

  }

}

# Sample call with 2 column names.
$list = Read-File $file 'Asset Number', 'Serial Number'

$list 然后可以通过管道传送到Export-Csv 以创建一个新的 CSV 文件,您可以在 Excel 中打开该文件:

# Export the selected properties to a new CSV file.
$list | Export-Csv -NoTypeInformation new.csv

# Open the new file with the registered application, asssumed to be Excel.
Invoke-Item new.csv

【讨论】:

    猜你喜欢
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    • 2013-07-14
    • 2017-12-19
    • 2018-10-01
    • 1970-01-01
    • 2020-12-06
    相关资源
    最近更新 更多