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

2020-10-20 16:01:36 +08:00
 AndyAO
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

... ... ...

650 次点击
所在节点    问与答
1 条回复
AndyAO
2020-10-20 16:09:46 +08:00
懂了,PowerShell 这奇葩语法也是扶它了.

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/716771

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX