简体   繁体   English

Powershell CSV 有序 导出特定列

[英]Powershell CSV ordered Export with specific columns

i have following problem:我有以下问题:

I have a query with Get-ADUser and have there specific fields to get (only test query)我有一个 Get-ADUser 查询,并且有特定的字段可以获取(仅测试查询)

$AllADUsers = get-aduser -Identity m.mustermann -Properties * | select mail, title, l, GivenName, Surname, company, department, mobile

Also i must add some static fields how language or comment in to the CSV, i take some variable for that:此外,我必须在 CSV 中添加一些 static 字段如何语言或注释,我为此采取了一些变量:

$Language = "german"
$Comment = ""
$Link = ""
$gender = ""
$exportto = "C:\Temp\Export01.csv"

Than i want to export all entries in a ordered UTF8 CSV with Delimiter ":".比我想导出带有分隔符“:”的有序 UTF8 CSV 中的所有条目。 I do a foreach but always i get mistakes or not complete lists - i show you my code (but i have mistakes)我做了一个 foreach 但我总是出错或不完整的列表 - 我向你展示我的代码(但我有错误)

Foreach ($ADUser in $AllADUsers) {

  $MailAddress = $ADUser.mail
  $FullName = "$ADUser.GivenName" + "$ADUser.Surname"
  $Title = $AdUser.Title
  $Location = $ADUser.l
  $Comment = $Comment
  $Link = $Link
  $MobilePhone = $ADUser.mobile
  $Language = $Language
  $Divison = $ADUser.Department
  $GivenName = $ADUser.GivenName
  $Surname = $ADUser.Surname 


}

$ADUser | Export-CSV -Path $exportto -NoTypeInformation -Delimiter ":" -Encoding UTF8

Can you help me?你能帮助我吗?

The export must be with the ordered list and without "" and just in this order.导出必须带有有序列表,并且没有“”,并且仅按此顺序。

Thank you.谢谢你。 Max最大限度

There are some high-level issues in your code.您的代码中有一些高级问题。

Incorrect assignment不正确的分配

In your code you should assign the values to specific properties of $AdUser .在您的代码中,您应该将值分配给$AdUser的特定属性。 Something like:就像是:

foreach ($ADUser in $AllADUsers) {
  $ADUser.FullName = "$($ADUser.GivenName) $($ADUser.Surname)"
  $ADUser.Comment = $Comment
  $ADUser.Link = $Link
}

No export没有出口

Currently you export only last entry, you should pipe $AllADUsers to Export-Csv :目前你只导出最后一个条目,你应该 pipe $AllADUsersExport-Csv

$AllADUsers| Export-CSV -Path $exportto -NoTypeInformation -Delimiter ":" -Encoding UTF8

No use of Select-Object calculated properties不使用 Select-Object 计算属性

In general, you could have your code shorten to one-liner like:通常,您可以将代码缩短为单行代码,例如:

$AllADUsers | Select-Object @{n="Comment";{$Comment}},@{n="FullName";e={"$($ADUser.GivenName) $($ADUser.Surname)"}} ... | Export-CSV ...

Read more about calculated properties here . 在此处阅读有关计算属性的更多信息

Your code doesn't work, mainly because you are creating a series of variables that in the end you do not use.您的代码不起作用,主要是因为您正在创建一系列最终不使用的变量。

To output to CSV, you need to collect objects with properties, not single variables.到 output 到 CSV,您需要收集具有属性的对象,而不是单个变量。 A very elegant way is to create those objects using [PsCustomObject] syntax, where at the same time the order in which you set the properties make up for the order in the final output file.一种非常优雅的方法是使用[PsCustomObject]语法创建这些对象,同时您设置属性的顺序弥补了最终 output 文件中的顺序。

Using the colon as delimiter character is unusual and may get you into trouble when other applications need to read and understand this, but that is up to you.使用冒号作为分隔符是不常见的,当其他应用程序需要阅读和理解它时可能会给您带来麻烦,但这取决于您。

Another thing is that you mainly seem to use LDAP attribute names, where PowerShell has most of them mapped to more friendly attribute names (like 'City' which maps to LDAP 'l').另一件事是,您似乎主要使用 LDAP 属性名称,其中 PowerShell 大部分映射到更友好的属性名称(例如映射到 LDAP 'l')的属性名称。

Your code revised:您的代码已修改:

# Get-ADUSer already returns these properties by default:
# DistinguishedName, Enabled, GivenName, Name, ObjectClass, ObjectGUID, SamAccountName, SID, Surname, UserPrincipalName

# your hardcoded static variables:
$Language = "german"
$Comment  = ""
$Link     = ""
$gender   = ""
$exportto = "C:\Temp\Export01.csv"

# these are the extra properties you want to fetch
$userprops = 'EmailAddress','Title','Company','City','Department','MobilePhone'

# if you want this for all users, remove "-Identity 'm.mustermann'" and use "-Filter *" instead
$result = Get-ADUser -Identity 'm.mustermann' -Properties $userprops | ForEach-Object {
    # output an object to be collected in variable $result
    # the order in which you put them also determines the field order in the CSV
    [PsCustomObject]@{
        MailAddress = $_.EmailAddress
        FullName    = $_.DisplayName
        Title       = $_.Title
        Company     = $_.Company
        Location    = $_.City
        Comment     = $Comment      # static field
        Link        = $Link         # static field
        MobilePhone = $_.MobilePhone
        Language    = $Language     # static field
        Department  = $_.Department
        GivenName   = $_.GivenName
        Surname     = $_.Surname
    }
}

$result | Export-CSV -Path $exportto -NoTypeInformation -Delimiter ":" -Encoding UTF8

A note about Export-Csv :关于Export-Csv的说明:

Export-Csv will always quote everything, be it a header or data field. Export-Csv将始终引用所有内容,无论是 header 还是数据字段。

Simply trying to remove all quotes in a CSV file is risking mis-alignment in the field order and rendering the csv as unusable.简单地尝试删除 CSV 文件中的所有引号可能会导致字段顺序错位并使 csv 无法使用。
With PowerShell version 7 you have the option to use parameter -UseQuotes AsNeeded , but for older versions, you may safely do this using my function ConvertTo-CsvNoQuotes使用 PowerShell 版本 7,您可以选择使用参数-UseQuotes AsNeeded ,但对于旧版本,您可以使用我的 function ConvertTo-CsvNoQuotes安全地执行此操作

Since this code is all about getting AD information, I don't see why you would want to construct the FullName here.由于这段代码都是关于获取AD 信息的,我不明白你为什么要在这里构造 FullName。 Just use the DisplayName property that is set in the AD as the code above does只需像上面的代码一样使用在 AD 中设置的 DisplayName 属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM