V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
AndyAO
V2EX  ›  问与答

为什么这两个看似相同的命令运行,结果截然不同?(PowerShell)

  •  
  •   AndyAO · 2020-10-20 16:01:36 +08:00 · 647 次点击
    这是一个创建于 1310 天前的主题,其中的信息可能已经有所发展或是发生改变。
    Get-ChildItem -Path "D:\GitRepository\test" -Recurse -Filter "*A*"
    
    function testFunc($path,$pattern) {
        Get-ChildItem -Path $path -Recurse -Filter $pattern
    }
    
    testFunc("D:\GitRepository\test","*A*")
    

    感觉这两条命令应该有相同的效果,一个是直接调用 Cmdlet,另一个是通过函数调用.

    但是结果不同,前者正常,后者则是将工作目录下的所有成员列出了.

    肯定是我有地方理解错了,但是想了很长时间也没搞清楚,所以寻求帮助.

    前者:

    Mode                 LastWriteTime         Length Name
    ----                 -------------         ------ ----
    -a---          2020/10/20    15:32            611 AAA.cfg
    

    后者:

    Mode                 LastWriteTime         Length Name
    ----                 -------------         ------ ----
    -a---          2020/10/20    15:32            611 AAA.cfg
    -a---          2020/10/20    15:32            611 BBB.cfg
    -a---          2020/10/20    15:32            611 CCC.cfg
    -a---          2020/10/20    15:42            218 test.ps1
    
        Directory: D:\GitRepository\PowerShell\example
    
    Mode                 LastWriteTime         Length Name
    ----                 -------------         ------ ----
    d----          2020/10/20    11:34                class
    
        Directory: D:\GitRepository\PowerShell\example\class
    
    Mode                 LastWriteTime         Length Name
    ----                 -------------         ------ ----
    -a---          2020/10/14    21:08            850 example-static.ps1
    -a---          2020/10/16    11:25            694 learn-example-static.ps1
    
        Directory: D:\GitRepository\PowerShell\example
    
    Mode                 LastWriteTime         Length Name
    ----                 -------------         ------ ----
    d----          2020/10/20    11:34                hash-table
    
        Directory: D:\GitRepository\PowerShell\example\hash-table
    
    Mode                 LastWriteTime         Length Name
    ----                 -------------         ------ ----
    -a---          2020/10/15    11:23            267 hash-table.ps1
    -a---          2020/10/15    11:22            258 hash-table-cannot-self-reference.ps1
    
    ... ... ...
    
    
    AndyAO
        1
    AndyAO  
    OP
       2020-10-20 16:09:46 +08:00
    懂了,PowerShell 这奇葩语法也是扶它了.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5012 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 06:35 · PVG 14:35 · LAX 23:35 · JFK 02:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.