Powershell功能赢得';行不通
我在旧的powershell文件夹中找到此函数:Powershell功能赢得';行不通,powershell,Powershell,我在旧的powershell文件夹中找到此函数: Function listAllPaths([string]$fromFolder,[string]$filter) { Get-ChildItem -Path $fromFolder -Recurse $filter | Select-Object -Property FullName } 想测试一下。我将其放入我的个人资料中,启动Powershell并键入以下内容: PS C:\> listAllPaths("C:\Downloa
Function listAllPaths([string]$fromFolder,[string]$filter) {
Get-ChildItem -Path $fromFolder -Recurse $filter | Select-Object -Property FullName
}
想测试一下。我将其放入我的个人资料中,启动Powershell并键入以下内容:
PS C:\> listAllPaths("C:\Downloads\Nemi","*.jpg")
该文件夹是定制的,它恰好与Vista下载文件夹同名。所涉及的子文件夹没有但是有jpg文件,但屏幕上没有打印任何内容。有人能告诉我我做错了什么吗?(因为我肯定可能是我做错了什么)。经典问题-调用PowerShell函数就像调用PowerShell cmdlet一样-使用空格分隔符而不使用括号,例如:
PS> listAllPaths C:\Downloads\Nemi *.jpg
注意,当这样调用时,在args周围不需要双qout。在PowerShell 2.0中,请确保使用Set StrictMode-版本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
仅供参考,您调用ListAllPath的方式会导致一个数组(“C:\Downloads\Nemi”,“*.jpg”)被传递到$fromFolder参数。$filter参数没有收到任何值
我还应该提到,在调用.NET/COM/WMI方法时,您只想使用逗号和括号
PS> foo 1 2
1 2