powershell 管道问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
xinghen57

powershell 管道问题

  •  
  •   xinghen57 Apr 6, 2023 2166 views
    This topic created in 1131 days ago, the information mentioned may be changed or developed.

    看 Powershell Rename-Item 文档时,有批量命名文件的 example ,代码如下:

    Get-ChildItem *.txt | Rename-Item -NewName { $_.Name -replace '.txt','.log' } 

    无法理解 Get-ChildItem 获取的文件后,通过管道,具体是使用哪种方式传递给 Rename-Item ,以及传递了什么内容。

    上面代码我的理解,Get-ChildItem 命令的结果通过管道传递给了 Rename-Item 的 Path 参数。但 Path 只接受 string 。如果管道是 ByValue 方式,传递的应该是 Object ,所以不是 ByValue 。如果是 ByPropertyName ,Get-ChildItem 获取的 FileInfo 对象并没有 Path 属性,理论上也无法传递。

    2 replies    2023-04-06 22:48:26 +08:00
    geelaw
        1
    geelaw  
       Apr 6, 2023 via iPhone   1
    看文档就知道了,LiteralPath 可以 ByPropertyName 而且有别名 PSPath ,而 PowerShell 的 provider 返回的对象都用 PowerShell 扩展系统增加了 PSPath 属性。

    传入 Path 是错误的,,当然不能认为传入了 Path ,因为中括号既是合法文件名也是 PowerShell Path 字符串的通配符,这会导致带有中括号的文件名解读错误。此外 Path 的性能也不如 LiteralPath 。

    除了看文档,搜索也可以解决这个问题 https://stackoverflow.com/questions/57456944/nonexistent-literalpath-property-of-system-io-fileinfo-object-bound-to-parameter
    xinghen57
        2
    xinghen57  
    OP
       Apr 6, 2023
    @geelaw #1 太感谢了,醍醐灌顶。
    About     Help     Advertise     Blog     API     FAQ     Solana     1704 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 30ms UTC 16:25 PVG 00:25 LAX 09:25 JFK 12:25
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86