将用户输入添加到 Select-String 命令
Adding user input to a Select-String command
我目前正在尝试创建一个 select 字符串命令来确定我公司的用户是否仍然出现在某些目录中。我当前的命令如下:
Select-String -path "C:\filepath\*.csv" -Pattern "<string>" |
Format-Table -Property LineNumber,Line,Path -Wrap |
Out-File "C:\outfile.txt"
当前命令意味着我需要在每次 运行 运行脚本之前更改 Pattern 参数的内容。相反,我想做的是在 运行 脚本时请求我输入字符串。是否可以将 while 循环添加到此命令以请求用户输入?
谢谢
将此添加到脚本的头部:
Param(
[Parameter(Mandatory = $True)]
[System.String]
$Pattern
)
Select-String -Path C:\filepath\*.csv -Pattern $Pattern |
Tee-Object -FilePath C:\outfile.txt |
Format-Table -Property LineNumber, Line, Path -Wrap
当您调用脚本时,该属性确保您为 -Pattern
提供参数。如果您希望它不为空 ([ValidateNotNullOrEmpty()]
) 等,您可以添加更多属性。
顺便说一句,永远不要从格式化程序中使用管道。
我目前正在尝试创建一个 select 字符串命令来确定我公司的用户是否仍然出现在某些目录中。我当前的命令如下:
Select-String -path "C:\filepath\*.csv" -Pattern "<string>" |
Format-Table -Property LineNumber,Line,Path -Wrap |
Out-File "C:\outfile.txt"
当前命令意味着我需要在每次 运行 运行脚本之前更改 Pattern 参数的内容。相反,我想做的是在 运行 脚本时请求我输入字符串。是否可以将 while 循环添加到此命令以请求用户输入?
谢谢
将此添加到脚本的头部:
Param(
[Parameter(Mandatory = $True)]
[System.String]
$Pattern
)
Select-String -Path C:\filepath\*.csv -Pattern $Pattern |
Tee-Object -FilePath C:\outfile.txt |
Format-Table -Property LineNumber, Line, Path -Wrap
当您调用脚本时,该属性确保您为 -Pattern
提供参数。如果您希望它不为空 ([ValidateNotNullOrEmpty()]
) 等,您可以添加更多属性。
顺便说一句,永远不要从格式化程序中使用管道。