为 PowerShell 的 Get-ChildItem 动态创建的参数参数
Dynamically created parameter arguments for PowerShell's Get-ChildItem
长话短说,我正在尝试在 PowerShell 的 Get-ChildItem
中动态使用参数 -Directory
或 -File
。你猜怎么着?我做不到。
这是交易(注意:伪代码):
Param(
[string]$filter = $(throw "Error: name"),
[string]$type = $(throw "error: file or directory")
)
if( $type -eq "file" ) {
$mode = '-File'
}
elseif( $type -eq "directory" ) {
$mode = '-Directory'
}
Function Find_Plugin_folder {
Write-Host "look for: '$($filter)'"
Invoke-Command -ComputerName (Get-Content servers.txt ) -ScriptBlock {
(Get-ChildItem -Path "z:\www" -Depth 5 $Using:mode -Filter $Using:filter -Recurse ) | %{$_.FullName}
} -ThrottleLimit 80
}
Find_Plugin_folder
$Using:mode
是它抛出错误的地方,要么:
PS C:\Users\janreilink> v:\test.ps1 vevida-optimizer file
look for: 'vevida-optimizer'
A positional parameter cannot be found that accepts argument '-File'.
+ CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
+ PSComputerName : webserver-01.example.net
或
PS C:\Users\janreilink> v:\test.ps1 vevida-optimizer directory
look for: 'vevida-optimizer'
A positional parameter cannot be found that accepts argument '-Directory'.
+ CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
+ PSComputerName : webserver-01.example.net
我整个下午都在阅读有关动态参数集的内容,但还不能全神贯注。非常感谢任何一点。
您需要使用 splatting
来代替。首先创建一个散列table,其中包含您要传递的部分或全部参数:
$dynamicArgs = @{}
if( $type -eq "file" ) {
$dynamicArgs['File'] = $true
}
elseif( $type -eq "directory" ) {
$dynamicArgs['Directory'] = $true
}
然后,在 Invoke-Command
中,在变量名称前加上 @
以表明您想要 "splat" 参数:
Get-ChildItem -Path "z:\www" -Depth 5 @Using:dynamicArgs -Filter $Using:filter -Recurse
如果splatting table包含键File
,值为$true
,相当于在命令行添加-File:$true
,反之亦然Directory
参数
长话短说,我正在尝试在 PowerShell 的 Get-ChildItem
中动态使用参数 -Directory
或 -File
。你猜怎么着?我做不到。
这是交易(注意:伪代码):
Param(
[string]$filter = $(throw "Error: name"),
[string]$type = $(throw "error: file or directory")
)
if( $type -eq "file" ) {
$mode = '-File'
}
elseif( $type -eq "directory" ) {
$mode = '-Directory'
}
Function Find_Plugin_folder {
Write-Host "look for: '$($filter)'"
Invoke-Command -ComputerName (Get-Content servers.txt ) -ScriptBlock {
(Get-ChildItem -Path "z:\www" -Depth 5 $Using:mode -Filter $Using:filter -Recurse ) | %{$_.FullName}
} -ThrottleLimit 80
}
Find_Plugin_folder
$Using:mode
是它抛出错误的地方,要么:
PS C:\Users\janreilink> v:\test.ps1 vevida-optimizer file
look for: 'vevida-optimizer'
A positional parameter cannot be found that accepts argument '-File'.
+ CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
+ PSComputerName : webserver-01.example.net
或
PS C:\Users\janreilink> v:\test.ps1 vevida-optimizer directory
look for: 'vevida-optimizer'
A positional parameter cannot be found that accepts argument '-Directory'.
+ CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
+ PSComputerName : webserver-01.example.net
我整个下午都在阅读有关动态参数集的内容,但还不能全神贯注。非常感谢任何一点。
您需要使用 splatting
来代替。首先创建一个散列table,其中包含您要传递的部分或全部参数:
$dynamicArgs = @{}
if( $type -eq "file" ) {
$dynamicArgs['File'] = $true
}
elseif( $type -eq "directory" ) {
$dynamicArgs['Directory'] = $true
}
然后,在 Invoke-Command
中,在变量名称前加上 @
以表明您想要 "splat" 参数:
Get-ChildItem -Path "z:\www" -Depth 5 @Using:dynamicArgs -Filter $Using:filter -Recurse
如果splatting table包含键File
,值为$true
,相当于在命令行添加-File:$true
,反之亦然Directory
参数