我在旧的powershell文件夹中找到了这个函数:
Function listAllPaths([string]$fromFolder,[string]$filter) {
Get-ChildItem -Path $fromFolder -Recurse $filter | Select-Object -Property FullName
}
Run Code Online (Sandbox Code Playgroud)
并希望测试它.我把它放在我的个人资料中,启动Powershell并键入以下内容:
PS C:\> listAllPaths("C:\Downloads\Nemi","*.jpg")
Run Code Online (Sandbox Code Playgroud)
该文件夹是自定义的,它恰好与Vista下载文件夹具有相同的名称.在考虑中的子文件夹中有什么,但 JPG文件,但没有什么是显示在屏幕上.谁能告诉我我做错了什么?(因为这可能是我做错了什么,我很确定).
经典问题 - 调用PowerShell函数就像调用PowerShell cmdlet一样 - 使用空格分隔符而不是parens,例如:
PS> listAllPaths C:\Downloads\Nemi *.jpg
Run Code Online (Sandbox Code Playgroud)
请注意,在这样调用时,您不需要围绕args进行双重qoutes.在PowerShell 2.0中,请务必使用Set-StrictMode -version 2.0,它将捕获此错误:
PS> Set-StrictMode -Version 2.0
PS> function foo($a,$b) {"$a $b"}
PS> foo(1,2)
The function or command was called as if it were a method.
Parameters should be separated by spaces. For information about
parameters, see the about_Parameters Help topic.
At line:1 char:4
+ foo <<<< (1,2)
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : StrictModeFunctionCallWithParens
Run Code Online (Sandbox Code Playgroud)
这是调用此PowerShell函数的正确方法:
PS> foo 1 2
1 2
Run Code Online (Sandbox Code Playgroud)
仅供参考,您调用listAllPaths会导致数组("C:\ Downloads\Nemi","*.jpg")传递给$ fromFolder参数.$ filter参数不接收任何值.
我还要提一下,在调用.NET/COM/WMI方法时,你只想使用逗号和parens.
归档时间: |
|
查看次数: |
1536 次 |
最近记录: |