【问题标题】:Powershell edit Headers in CSV before exportPowershell 在导出前编辑 CSV 中的标头
【发布时间】:2018-07-25 17:13:13
【问题描述】:

您好,我正在尝试制作一个小的 PS 脚本,从一个 OU 获取用户以及一些标题,然后在保存之前编辑标题(以便我可以将它们导入 Office365)。

$Select = @{
'Filter' = '*'
'searchbase'='OU=TESTOU,DC=corp,DC=test,DC=com'
'Properties'= @("GivenName","SurName","DisplayName","samaccountname","Streetaddress","state","City","PostalCode","Country","Title","Company","Department","Office","telePhonenumber")
}

Get-ADUser @select|Select-Object -Property $propertyTranslation|export-csv c:\Test0218.csv -notype

$propertyTranslation = @(
    @{ Name = 'UserPrincipalName'; Expression = { $_.'User Name' } }
    @{ Name = 'GivenName';   Expression = { $_.'First Name'  } }
    @{ Name = 'SurName';   Expression = { $_.'Last Name'  } }
    @{ Name = 'DisplayName';   Expression = { $_.'Display Name'  } }
    @{ Name = 'Title';   Expression = { $_.'Job Title'  } }
    @{ Name = 'Department';   Expression = { $_.'Department'  } }
    @{ Name = 'telePhonenumber';   Expression = { $_.'Office Phone'  } }
    @{ Name = 'StreetAddress';   Expression = { $_.'Address'  } }
    @{ Name = 'City';   Expression = { $_.'City'  } }
    @{ Name = 'PostalCode';   Expression = { $_.'Zip or Postal Code'  } }
    @{ Name = 'Country';   Expression = { $_.'Country or Region'  } }
)

(Import-Csv -Path 'c:\Test0218.csv') |
Select-Object -Property $propertyTranslation |
Export-Csv c:\Test0230.csv -NoTypeInformation

这不起作用,因为文件的内容是垃圾:(

如您所见,文件标题保持不变,而且现在根本没有任何信息(只有 2 个而不是 11 个)我填写了所有信息,当仅导出带有 AD 标题的 CSV 时,它具有所需的信息. 有人可以帮我吗?

BR

【问题讨论】:

    标签: powershell csv active-directory office365


    【解决方案1】:

    我会在 ISE 或 VSCode 中对此进行调试,然后展开您的管道以协助完成这项工作。

    作为您尝试做的模拟,我使用一些预定义的 csv 数据创建了以下内容,我发现这些数据有助于调试任何与 csv 相关的工作流。

    $csvdata = @'
    "Header One", "Header Two"
    foo, bar
    baz, boo
    '@
    
    $xlate = @(
        @{Name = 'H1'; Expression = {$_.'Header One'} }
        @{Name = 'H2'; Expression = {$_.'Header Two'} }
    )
    
    $csv = ConvertFrom-Csv $csvdata
    
    $newcsv = $csv | Select -Property $xlate | ConvertTo-Csv -NoTypeInformation
    
    "csv----------------------"
    $csv
    
    "newcsv----------------------"
    $newcsv
    

    给予:

    csv----------------------
    Header One Header Two
    ---------- ----------
    foo        bar       
    baz        boo       
    newcsv----------------------
    "H1","H2"
    "foo","bar"
    "baz","boo"
    

    【讨论】:

      【解决方案2】:

      看起来您的属性名称翻译倒过来了:

      • 您提供的新属性名称与原始属性已有的名称相同(例如,GivenName

      • 您正在尝试使用它们没有的名称(例如,First Name)访问输入对象上的属性。

      如果您想重命名给定输入对象的属性,请使用以下方法:

      $co = [pscustomobject] @{ one = 1 } # sample input object
      
      $co | Select-Object @{ n = 'eins'; e = 'one' } # rename 'one' to 'eins'
      

      也就是说,将新名称分配给n (Name) 键,并在e (Expression) 键中引用原始属性名称。

      【讨论】:

      • 我已经改了。现在标题是正确的,但我没有得到第二行(johnnydoe@test.com、Johnny、Doe、Johnny Doe、销售经理、销售等信息......)
      • @Stefan.D:您正在应用转换两次,我认为这不是您的意图:在Get-AdUser 之后一次,然后在Import-Csv 之后再次跨度>
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-08
      • 2019-12-26
      • 2016-10-23
      • 1970-01-01
      相关资源
      最近更新 更多