将此 cmdlet 转换为脚本:Get-Content -Path C:\Users\Einar\Desktop\log\* -filter *1234567.log.txt | Select - 前 2 | Select - 最后 1

Turn this cmdlet into a script: Get-Content -Path C:\Users\Einar\Desktop\log\* -filter *1234567.log.txt | Select -First 2 | Select -last 1

基本上,我想输入 7 位数字并在日志文件中获取第二行作为输出。

顺便说一句,这是我第一次在 powershell 中尝试任何东西

获取内容-路径 C:\Users\Einar\Desktop\log* -过滤器 *1234567.log.txt | Select - 前 2 | Select - 最后 1

我有一个包含日志文件的文件夹,所有文件都以 (random7digits) 结尾。log.txt,我想通过输入一组 7 位数字来仅输出第二行。

感谢任何帮助:)

以非常基本的形式,这实现了你想要的。您可以将此保存为 .ps1 并将其保存在您的桌面上。如果你右击Run with PowerShell,那么控制台会在立即返回后关闭。从 pre-existing 控制台会话调用脚本,或者在末尾添加 pause 或其他内容。

我建议您将其扩展为学习练习,其中包含以下一些想法:

  • 检查文件是否存在并向用户提供合理的错误处理或消息
  • 检查文件是否包含您的 'ideal' 内容,或者是否包含两行内容,并向用户提供合理的错误处理或消息
  • 也许添加另一个参数,将默认值添加到您的桌面,以防您需要 运行 它与磁盘上其他地方的日志一起
param(
  [Parameter(Mandatory)]
  [int]$Digit
)
Get-ChildItem -Path <path_containing_files> -File -Filter *$Digit.log.txt | ForEach-Object { 
  Get-Content -Path $_ | Select-Object -Index 1 
}

函数Get-Logline

function Get-Loglines{      
   param(
    [Parameter(Mandatory=$true, ValueFromPipeline=$true, HelpMessage='Enter Path to Log Folder:' )]
    [string]$Path,
    [Parameter(Mandatory=$true, ValueFromPipeline=$true, HelpMessage='Enter string to filter for: *(string).log.txt')]
    [int]$filename,
    [Parameter(Mandatory=$true, ValueFromPipeline=$true, HelpMessage='Enter line number to read back:')]
    [int]$linenumber
    )

  process{
      $lookdepth = $linenumber - 1
      Get-ChildItem -Path $Path -File -Filter ('*{0}.log.txt' -f $filename) -force -Verbose |`
      
      ForEach-Object{
       Get-Content $_ -Verbose |`
       Select-Object -Skip $lookdepth -First 1 -Verbose
  }
 }
}

测试用例

C:\> Get-Loglines
cmdlet Get-Loglines at command pipeline position 1
Supply values for the following parameters:
(Type !? for Help.)
Path: C:\Downloads\Newfolder
filename: 666666
linenumber: 2
line 2

示例数据

C:\Downloads\Newfolder [master +39 ~0 -0 !]> Get-ChildItem


    Directory: C:\Downloads\Newfolder


Mode                 LastWriteTime         Length Name                                                                                                                                                          
----                 -------------         ------ ----                                                                                                                                                          
-a----         4/21/2022   2:37 PM             38 ab123456.log.txt                                                                                                                                              
-a----         4/21/2022   2:37 PM             38 ab666666.log.txt                                                                                                                                              
-a----         4/21/2022   2:37 PM             38 ab666667.log.txt                                                                                                                                              
-a----         4/21/2022   2:37 PM             38 acb666666.log.txt  


C:\Downloads\Newfolder> Get-Content .\ab666666.log.txt 
line 1
line 2
line 3
line 4
line 5