有没有办法将变量值拉入 select-object 表达式?

Is there a way to pull a variable value into a select-object expression?

这个脚本的目的是找到文件的名称、目录和最后写入时间并将其输出到csv文件。

get-childitem D:\testing -recurse -filter *.txt | select-object Name,DirectoryName,LastWriteTime, @{Name="New_colimn";Expression={"copy-item \`"DirectoryName\`" To_Compile_directory"}} | where { $_.DirectoryName -ne $NULL } | Export-CSV D:\testing\rdf.csv

我的问题是我想用另一个脚本填充 1 个单元格,该脚本从生成的 csv 文件中获取值。有没有办法拉取每个 DirectoryName 的值并将其粘贴到同一行的表达式中?我只收到一条错误消息,指出 DirectoryName 是无效键。

当我尝试使用 $.DirectoryName 拉取时,脚本只读取 $ 并且它的值是名称。

感谢帮助。

您的意思是从这样的文件中收集数据吗:

Get-ChildItem -Path 'D:\testing' -Filter '*.txt' -File -Recurse | 
Select-Object Name,DirectoryName,LastWriteTime | Export-Csv -Path 'D:\testing\rdf.csv' -NoTypeInformation

然后让您的其他脚本像这样从中读取 DirectoryName

$directories = (Import-Csv -Path 'D:\testing\rdf.csv').DirectoryName | Select-Object -Unique
# maybe do something with these directories here?
foreach ($folderPath in $directories) {
    # copy the directories including the files to an existing root destination folder
    Copy-Item -Path $folderPath -Destination 'D:\SomeExistingDestinationPath' -Recurse -Force
}