AndyAO
V2EX  ›  问与答

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

  •  
  •   AndyAO · Oct 20, 2020 · 965 views
    This topic created in 2047 days ago, the information mentioned may be changed or developed.
    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
       Oct 20, 2020
    懂了,PowerShell 这奇葩语法也是扶它了.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2809 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 05:08 · PVG 13:08 · LAX 22:08 · JFK 01:08
    ♥ Do have faith in what you're doing.