PowerShell:将系统密钥作为参数传递



我想将 - 唯一作为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:UsersaravikumarDownloadsx.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

也希望通过多个系统密钥,例如-Inique -simplesearch -casessentive

您不能将开关作为字符串参数这样。您需要使用一种称为" Splatting"的技术。请参阅help about_splatting或https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.core/about/about/about_splatting

最新更新