简体   繁体   English

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

[英]Powershell: pass system key as a argument

I want to pass -Unique as parameter for select-string 我想传递-Unique作为选择字符串的参数

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

But whenever i try to pass the parameter, i get the following error 但是每当我尝试传递参数时,都会出现以下错误

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

Also want to pass multiple system key if possible such as -unique -simplesearch -casesensitive 如果可能,还希望传递多个系统密钥,例如-unique -simplesearch -casesensitive

You can't pass a switch as a string parameter like that. 您不能像这样将开关作为字符串参数传递。 You need to use a technique called 'splatting'. 您需要使用一种称为“喷涂”的技术。 See help about_splatting or https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.core/about/about_splatting 请参阅help about_splattinghttps://msdn.microsoft.com/zh-cn/powershell/reference/5.1/microsoft.powershell.core/about/about_splatting

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM