Powershell:传递系统密钥作为参数

时间:2017-06-27 18:58:25

标签: powershell

我想将-Unique作为select-string

的参数传递
param(
    $y="CAR"
    $parameter=""
)
get-childitem -r -i "*.txt"| select-string "TABLE FILE $y" | Select-Object filename,path $parameter

但每当我尝试传递参数时,我都会收到以下错误

powershell : Select-Object : A positional parameter cannot be found that accepts argument '-Unique'.
At line:1 char:1
+ powershell -ExecutionPolicy ByPass -File x.ps1 -parameter '-Unique'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Select-Object :...ment '-Unique'.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

At C:\Users\aravikumar\Downloads\x.ps1:5 char:62
+ ... elect-string "TABLE FILE $y" | Select-Object filename,path $parameter
+                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Select-Object], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand

如果可能的话,还想传递多个系统密钥,例如-unique -simplesearch -casesensitive

1 个答案:

答案 0 :(得分:1)

您不能将交换机作为字符串参数传递。您需要使用一种名为“splatting'”的技术。请参阅help about_splattinghttps://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.core/about/about_splatting