Home > Software design >  Turn this cmdlet into a script: Get-Content -Path C:\Users\Einar\Desktop\log\* -filter *1234567
Turn this cmdlet into a script: Get-Content -Path C:\Users\Einar\Desktop\log\* -filter *1234567

Time:04-22

Basically, I want to input 7 digits and get second line in the log file as an output.

This is my first time attempting anything in powershell btw

Get-Content -Path C:\Users\Einar\Desktop\log* -filter *1234567.log.txt | Select -First 2 | Select -last 1

I have a folder with log files, that all end with (random7digits).log.txt, and I want to output only the second line by inputting a set of 7 digits.

Any help is appreciated :)

CodePudding user response:

In very basic form, this achieves what you want. You can save this is a .ps1 and save it on your desktop. If you right click and Run with PowerShell, then the console will close after immediately returning. Either call the script from a pre-existing console session, or add a pause or something at the end.

I recommend you expand upon it as a learning exercise with some of these ideas:

  • Check whether the file exists and provide sensible error handling or message to the user
  • Check if the file contains your 'ideal' content, or even two lines at all, and provide sensible error handling or message to the user
  • Perhaps add another parameter, with a default value to your desktop, in case you ever need to run it with logs elsewhere on disk
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 
}

CodePudding user response:

function 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
  }
 }
}

Test Case

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

Example Data

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
  • Related