【问题标题】:How do I rename properties on export to a CSV file?如何在导出到 CSV 文件时重命名属性?
【发布时间】:2020-06-14 16:27:20
【问题描述】:

我创建了一个脚本,它显示了一个以字母 A 开头的用户列表。 现在我想将它们导出到一个 CSV 文件,其中包含名为 full nameusernamecreation date 的列。

以下导出所需的列 values,但没有我想要的列 names(标题)。

Get-ADUser -Properties whenCreated -Filter {samaccountname -like 'A*'} | 
  Select-Object name, SamAccountName, whenCreated | 
    export-csv -path c:\userexport.csv

【问题讨论】:

  • 您能否展示您希望 CSV 文件的外观?包括头记录和至少一个虚拟数据记录。
  • 顺便说一句:虽然很方便,但最好是avoid the use of script blocks ({ ... }) as -Filter arguments
  • 正如 Vad 的回答所示,可以通过 calculated properties 重命名属性。
  • 请允许我给你一个标准的建议给新手:如果一个答案解决了你的问题,请accept it点击它旁边的大复选标记(✓),也可以选择投票(投票至少需要 15 个声望点)。如果您发现其他答案有帮助,请给他们投票。接受(您将获得 2 个声望点)和投票可以帮助未来的读者。如果您的问题尚未得到完全解答,请提供反馈或self-answer

标签: powershell csv rename


【解决方案1】:

像这样重命名输出属性:

Get-ADUser -Properties whenCreated -Filter {samaccountname -like 'A*'} | 
  Select-Object @{n='FullName';e={$_.Name}},@{n='Login';e={$_.SamaccountName}},@{n='When was Created';e={$_.WhenCreated}} |
    Export-Csv -path c:\userexport.csv -NoTypeInformation -Delimiter ';' -Encoding UTF8

按照 cmets 中后续问题的要求,稍后读取生成的 CSV 过滤器并按When was Created 列按时间顺序过滤:

Import-Csv -Path D:\testdir\userexport.csv -Delimiter ';' |
  Where-Object {[datetime]::parseexact($_.'When was Created','dd/MM/yyyy HH:mm:ss',$null) -le (Get-Date).AddYears(-2)} |
    Select-Object FullName

【讨论】:

  • 问题,现在我需要导入文件并输出工作两年以上的任何人的全名,我该怎么做?我认为我需要启动脚本 Import-Csv -Path C:\userexport.csv
  • 请不要在过滤器中使用大括号。该参数应该是一个字符串,而不是一个脚本块。请改写-Filter "samaccountname -like 'A*'"。还将-NoTypeInformation 开关添加到 Export-Csv cmdlet。
  • 不错;除了@Theo 的评论之外,还值得注意的是,如果 renaming 是计算属性所做的唯一事情 - 输入属性的 namestring 就可以了;例如,@{n='FullName';e={$_.Name}} 可以简化为 @{n='FullName';e='Name'}
  • @nadavshlomo 请不要提出新问题作为对原始问题答案的评论。
【解决方案2】:

我正在尝试这段代码

Get-ADUser -Properties whenCreated -Filter {samaccountname -like 'A*'} | 
  Select-Object @{n='FullName';e={$_.Name}},@{n='UserName';e={$_.SamaccountName}},@{n='CreateDate';e={$_.WhenCreated}} |
    Export-Csv -path c:\userexport.csv -NoTypeInformation -Delimiter ';' -Encoding UTF8

    Import-Csv -Path c:\userexport.csv -Delimiter ';' |
  Where-Object {[datetime]::parseexact($_.'CreatedDate','dd/MM/yyyy HH:mm:ss',$null) -le (Get-Date).AddYears(-2)} |
    Select-Object FullName

但我得到了错误:

    Ayman Ledawi                                        aymanle              19/12/2010 13:08:35
Ayoub Hassan                                        ayoubha              12/11/2012 16:00:38
Azure ADConnect                                     AzADConnect          24/10/2019 12:02:01
Azriel Perel Prof                                   azrielpe             19/02/2003 15:27:50
Azuretest1                                          azuretest1           27/05/2019 14:40:11
Azuretest2                                          azuretest2           27/05/2019 14:42:33
Azuretest3                                          Azuretest3           27/05/2019 14:43:34
Azuretest4                                          azuretest4           27/05/2019 14:44:52


Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
At line:11 char:17
+ ... ere-Object {[datetime]::parseexact($_.'When was Created','dd/MM/yyyy  ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FormatException

Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
At line:11 char:17
+ ... ere-Object {[datetime]::parseexact($_.'When was Created','dd/MM/yyyy  ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FormatException

Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
At line:11 char:17
+ ... ere-Object {[datetime]::parseexact($_.'When was Created','dd/MM/yyyy  ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FormatException

Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
At line:11 char:17
+ ... ere-Object {[datetime]::parseexact($_.'When was Created','dd/MM/yyyy  ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FormatException

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 2017-03-31
    • 2020-01-24
    • 1970-01-01
    • 2012-08-27
    相关资源
    最近更新 更多