【问题标题】:Modifying CSV content with Powershell使用 Powershell 修改 CSV 内容
【发布时间】:2013-12-09 12:00:12
【问题描述】:

我有一个 CSV 文件 Devices.csv,其中包含 IP 地址、设备名称、序列号、MAC 地址、用户名

Devices.csv 的所有行中的 Users 列都预填充了值 [Unknown]

代码...

{Import-CSV Devices.csv | Where-Object {$_.IPAddress -eq '192.168.2.124'} | Select-Object -last 1 | FT  IPAddress, devicveName, SerialNumber,  MACAddress, User -AutoSize }

....输出

IPAddress     deviceName     SNumber     MACAddress   User
---------     -----          -------     ----------   ----
192.168.2.124 ComputerA      1ABCDEFG    00xxYYbbCCdd [Unknown]   

我希望能够将 [Unknown] 文本替换为我从其他来源检索到的用户名。我可以使用 powershell 只更新 Devices.csv 中这一行的用户列,并保持 CSV 文件的其余部分不变吗?

谢谢,布赖恩

【问题讨论】:

    标签: powershell csv


    【解决方案1】:
    $csv = Import-CSV Devices.csv
    $csv | Where-Object {$_.IPAddress -eq '192.168.2.124'} | Select-Object -Last 1 | ForEach-Object {$_.User = $user}
    $csv | Export-Csv Devices.csv -NoTypeInformation
    

    【讨论】:

    • Shay,正是我想要的,谢谢 Brian
    【解决方案2】:

    试试这个:

    $otheruser = '...'
    
    Import-Csv Devides.csv | % {
      if ($_.User -eq '[Unknown]') { $_.User = $otheruser }
      $_
    } | Export-Csv Divices_modified.csv -NoTypeInformation
    

    【讨论】:

      猜你喜欢
      • 2021-09-30
      • 2020-07-29
      • 1970-01-01
      • 2019-03-22
      • 2011-08-23
      • 2021-06-22
      • 2018-10-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多