powershell 管道问题

讨论 未结 1 35
xinghen57
xinghen57 会员 2023年4月6日 14:34 发表
<p>看 Powershell Rename-Item 文档时,有批量命名文件的 example ,代码如下:</p> <pre><code class="language-powershell">Get-ChildItem *.txt | Rename-Item -NewName { $_.Name -replace '.txt','.log' } </code></pre> <p>无法理解 Get-ChildItem 获取的文件后,通过管道,具体是使用哪种方式传递给 Rename-Item ,以及传递了什么内容。</p> <p>上面代码我的理解,Get-ChildItem 命令的结果通过管道传递给了 Rename-Item 的 Path 参数。但 Path 只接受 string 。如果管道是 ByValue 方式,传递的应该是 Object ,所以不是 ByValue 。如果是 ByPropertyName ,Get-ChildItem 获取的 FileInfo 对象并没有 Path 属性,理论上也无法传递。</p>
收藏(0)  分享
相关标签: 灌水交流
注意:本文归作者所有,未经作者允许,不得转载
1个回复
  • geelaw
    2023年4月6日 14:34
    看文档就知道了,LiteralPath 可以 ByPropertyName 而且有别名 PSPath ,而 PowerShell 的 provider 返回的对象都用 PowerShell 扩展系统增加了 PSPath 属性。 传入 Path 是错误的,,当然不能认为传入了 Path ,因为中括号既是合法文件名也是 PowerShell Path 字符串的通配符,这会导致带有中括号的文件名解读错误。此外 Path 的性能也不如 LiteralPath 。 除了看文档,搜索也可以解决这个问题
    0 0