创建具有包含派生文件系统路径信息的自定义属性的对象,并将它们导出到 CSV 计算属性 [英] Create objects with custom properties containing derived filesystem path information and export them to a CSV - calculated properties
问题描述
编者注:
这个问题的要点是:
* 如何向包含派生路径信息的 Get-ChildItem
输出的对象添加自定义属性,即父文件夹路径(如 Folder
和名称(如 Foldername
)?
* 如何将结果对象导出到 CSV 文件?
Editor's note:
The gist of this question is:
* How do I add custom properties to objects output by Get-ChildItem
that contain derived path information, namely the parent folder path (as Folder
and name (as Foldername
)?
* How do I export the resulting objects to a CSV file?
目前有一个脚本正在运行,我从 StackOverflow 获取并修改以供我自己使用.该脚本的目的是查看目录,获取文件名,然后将它们作为 .csv 文件导出到目录中.
Currently have a script running that I took from StackOverflow and modified for my own use. The purpose of the script is to look at a directory, grab file names, then export them to a directory as a .csv file.
我能够修改脚本以仅提取文件名,因为它最初只提取了path、user
等.我可以通过添加 ,Name代码>.出于某种原因,我无法让脚本通过它所在的父文件夹,也需要父文件夹.
I was able to modify the script to pull through just the file name as originally it only pulled through the path, user
etc. Was able to do this by adding ,Name
. For some reason I can't get the script to pull through the parent folder it's in, also wanting the parents parent folder.
我相信这部分代码是我最麻烦的地方.在 select
后,我可以添加 ,Name
但在 之后添加
没有正确通过.,folder
或 ,foldername
select
I believe this part of the code is what I am having most trouble with. Upon the select
I was able to add ,Name
but adding ,folder
or ,foldername
after the select
doesn't pull through properly.
ForEach-Object {$_ | Add-Member -Name "Owner" -MemberType NoteProperty -Value (Get-Acl $_.FullName).Owner -PassThru} |
Sort-Object fullname |
Select FullName,CreationTime,LastWriteTime,Owner,Name,Folder,Foldername
推荐答案
Get-ChildItem
为文件返回的 [System.IO.FileInfo]
实例没有Folder
或 FolderName
属性.
The [System.IO.FileInfo]
instances returned by Get-ChildItem
for files do not have Folder
or FolderName
properties.
Get-ChildItem -File $HOMEDesktop |例如,Get-Member
会显示可用的属性,并会显示所需的信息可以从 PSPath
和 中派生PSParentPath
属性.
Get-ChildItem -File $HOMEDesktop | Get-Member
, for instance, will show you the available properties, and will show you that the desired information can be derived from the PSPath
and PSParentPath
properties.
Select-Object
允许 基于哈希表的属性定义,即所谓的计算属性,允许您重命名和/或转换属性和/或添加自定义属性通过脚本块从其他属性值派生.
Select-Object
allows hashtable-based property definitions, so-called calculated properties, which allow you to rename and/or transform properties and/or add custom properties derived from other property values by way of a script block.
注意:您还可以将计算属性与 Format-Table
和 Format-List
cmdlet 结合使用,为显示创建输出 仅(以及其他 cmdlet - 请参阅链接的帮助主题).
Note: You can also use calculated properties with the Format-Table
and Format-List
cmdlets for creating output for display only (as well as with other cmdlets - see the linked help topic).
您要查找的内容的简化示例(包括输出到 CSV 文件):
A simplified example of what you're looking for (includes output to a CSV file):
Get-ChildItem $HOMEDesktop | Select-Object Name,
@{ Name = 'Folder'; Expression = { Convert-Path $_.PSParentPath } },
@{ Name = 'Foldername'; Expression = { ($_.PSPath -split '\')[-2] } } |
Export-Csv Out.csv -Encoding Utf8 -NoTypeInformation
注意,或者,您可以通过 Add-Member
将 Folder
和 FolderName
属性添加到输入对象,因为您在您的问题中使用 Owner
属性.
Note that, alternatively, you could add Folder
and FolderName
properties to the input objects via Add-Member
, as you did with the Owner
property in your question.
说明:
请注意,您可以通过运行 Get-Help
;添加 -online
以在浏览器中查看帮助主题;要了解有关 -split
运算符的更多信息,请运行 Get-Help about_split
;要了解 PowerShell 的一般帮助系统,请运行 Get-Help Get-Help -online
.
Note that you can get more detailed information about any of the commands mentioned by running Get-Help <command-name> -Full
; add -online
to view the help topic in a browser; to learn more about the -split
operator, run Get-Help about_split
; to learn about PowerShell's help system in general, run Get-Help Get-Help -online
.
每个传递给
Select-Object
的@{ ... }
构造都是一个 哈希表,它定义了一个属性附加到每个输出对象:
Each
@{ ... }
construct passed toSelect-Object
is a hash table that defines a property to attach to each output object:
- 哈希表必须有两个条目:
Name
或Label
,它定义了属性的名称;为简洁起见,您可以使用键名的(不区分大小写的)前缀,例如n
或l
.Expression
,定义了属性的值;同样,键名的(不区分大小写的)前缀也有效,例如e
.- 表达式可以只是一个属性名称(一个字符串),以防您只是想重命名一个输入属性,但更典型的是一个脚本块 (
{ ... }
),这是一段代码,为每个输入对象执行,其输出成为正在定义的属性的值;在该脚本块内,自动变量$_
(或$PSItem
)指的是手头的输入对象.
- The hash table must have two entries:
Name
orLabel
, which defines the property's name; for brevity, you may use a (case-insensitive) prefix of the key name, such as justn
orl
.Expression
, which defines the property's value; again, a (case-insensitive) prefix of the key name works too, such as juste
.- The expression can be a mere property name (a string), in case you simply want to rename an input property, but is more typically a script block (
{ ... }
), which is a piece of code that gets executed for each input object, and whose output becomes the value of the property being defined; inside that script block, automatic variable$_
(or$PSItem
) refers to the input object at hand.
Folder
属性的定义:Convert-Path $_.PSParentPath
转换的完全限定 PowerShell 路径>PSParentPath
属性包含 - 其中包括标识驱动器提供程序的前缀 - 到常规文件系统路径;例如,Microsoft.PowerShell.CoreFileSystem::C:UsersjdoeDesktop
->C:UsersjdoeDesktop
.Definition of the
Folder
property:Convert-Path $_.PSParentPath
converts the fully qualified PowerShell path that thePSParentPath
property contains - which includes a prefix identifying the drive provider - to a regular filesystem path; e.g.,Microsoft.PowerShell.CoreFileSystem::C:UsersjdoeDesktop
->C:UsersjdoeDesktop
.Foldername
属性的定义:($_.PSPath -split '\')[-2]
拆分通过路径分隔符进入组件的完整路径,然后访问倒数第二个组件(
-2
),即父文件夹名称;例如,C:UsersjdoeDesktopfile.txt
->桌面
Definition of the
Foldername
property:($_.PSPath -split '\')[-2]
splits the full path into components by path separator, and then accesses the next-to-last component (
-2
), which is the parent folder name; e.g.,C:UsersjdoeDesktopfile.txt
->Desktop
'\'
必须用来表示,因为
-split
的第一个 RHS 操作数是一个正则表达式,其中具有特殊含义,因此必须加倍以作为字面量.
- 如果您想支持
/
作为路径分隔符以及跨平台支持,您可以使用($_.PSPath -split '[\/]')[-2]
.
'\'
must be used to represent, because
-split
's 1st RHS operand is a regular expression, wherehas special meaning and must therefore be doubled to be taken as a literal.
- If you wanted to support
/
as the path separator as well for cross-platform support, you'd use($_.PSPath -split '[\/]')[-2]
.
Export-Csv
将Select-Object
输出的对象导出为CSVOut.csv
,其中输入对象的属性名称成为标题行,属性值成为数据行.Export-Csv
exports the objects output bySelect-Object
to CSVOut.csv
, where the input objects' property names become the header row, and the property values the data rows.-Encoding Utf8
,仅在 Windows PowerShell 中需要(幸运的是,PowerShell (Core) 7+ 现在一直使用无 BOM 的 UTF-8),确保非 ASCII 字符被正确编码;默认情况下,Export-Csv
使用 ASCII 编码并简单地将非 ASCII 字符(例如外文字母)替换为 verbatim?
字符,从而导致信息丢失;请注意,Windows PowerShell 中的-Encoding Utf8
总是创建带有 BOM 的 UTF-8 文件.
-Encoding Utf8
, required in Windows PowerShell only (PowerShell (Core) 7+ now fortunately consistently uses BOM-less UTF-8), ensures that non-ASCII characters are properly encoded; by default,Export-Csv
uses ASCII encoding and simply replaces non-ASCII characters, such as foreign letters, with verbatim?
characters, resulting in loss of information; note that-Encoding Utf8
in Windows PowerShell invariably creates UTF-8 files with a BOM.
-NoTypeInformation
,同样仅在 Windows PowerShell 中需要,抑制Export-Csv
默认添加为第一行输出文件的行,其中包含输入对象的完整类型名称(类名称)(例如,#TYPE System.Management.Automation.PSCustomObject
;这是为了便于以后重新转换为对象).-NoTypeInformation
, again required in Windows PowerShell only, suppresses a line thatExport-Csv
by defaults adds as the first line of the output file, which contains the full type name (class name) of the input objects (e.g.,#TYPE System.Management.Automation.PSCustomObject
; this is meant to facilitate later reconversion into objects).这篇关于创建具有包含派生文件系统路径信息的自定义属性的对象,并将它们导出到 CSV 计算属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- The expression can be a mere property name (a string), in case you simply want to rename an input property, but is more typically a script block (
- 表达式可以只是一个属性名称(一个字符串),以防您只是想重命名一个输入属性,但更典型的是一个脚本块 (