Get 2nd & 3rd Line from a common log file for a list of servers

I have a log file called Maintenance.log on all servers on the drive F:\. All I am looking is the 2nd and 3rd line in the log file which gives me the date and time it executed the maintenance script. Can we get this info from all the servers where the maintenance.log file resides?

Can any one help me to write a power shell script for this...
PavanPAAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
This will write the results to the pipeline, where you can process them further, like exporting them to a csv (.\Whatever.ps1 | Export-Csv -Path C:\Temp\maintenance.csv -NoTypeInformation)
$LogFile = 'C:\Temp\Maintenance.log'
Get-Content C:\Temp\servers.txt | ForEach-Object {
	$ComputerName = $_
	Try {
		$Lines = Get-Content "\\$($_)\$($LogFile.Replace(':', '$'))" -ErrorAction Stop | Select-Object -First 3
		New-Object -TypeName PSObject -Property ([ordered]@{'ComputerName' = $ComputerName; 'Date' = $Lines[1]; 'Time' = $Lines[2]; Exception = $Null})
	} Catch {
		New-Object -TypeName PSObject -Property ([ordered]@{'ComputerName' = $ComputerName; 'Date' = $Null; 'Time' = $Null; Exception = $_.Exception.Message})
	}
}

Open in new window

1
PavanPAAuthor Commented:
Below is the output:
ComputerName                               Date                                       Time                                      Exception                                
------------                               ----                                       ----                                      ---------                                
APBNGM501                                                                                                                       Cannot find path '\\APBNGM501\F$\Maint...

The correct path of the file is on F drive.

Below is what I modified and Ran,

$LogFile = 'F:\Maintenance.log'
Get-Content C:\Temp\servers.txt | ForEach-Object {
      $ComputerName = $_
      Try {
            $Lines = Get-Content "\\$($_)\$($LogFile.Replace(':', '$'))" -ErrorAction Stop | Select-Object -First 3
            New-Object -TypeName PSObject -Property ([ordered]@{'ComputerName' = $ComputerName; 'Date' = $Lines[1]; 'Time' = $Lines[2]; Exception = $Null})
      } Catch {
            New-Object -TypeName PSObject -Property ([ordered]@{'ComputerName' = $ComputerName; 'Date' = $Null; 'Time' = $Null; Exception = $_.Exception.Message})
      }
}

Its failing
0
PavanPAAuthor Commented:
Below are the text from Line one:

~~~~~~~~~~~~~ BEGIN ~~~~~~~~~~~~~ Line 1
USXXXXXXXXXX Line 2
.  Line 3
Sat 08/13/2016 Line 4
09:07 PM Line 5
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

oBdACommented:
That means that the file can't be accessed, either because it doesn't exist at the location specified, because administrative shares are disabled, or because the account you're running this with doesn't have permissions to access the administrative share.
0
PavanPAAuthor Commented:
I have access to those files and servers
I open them and access them every month
0
oBdACommented:
"Open them" is not very descriptive.
What's the result of
Get-Item '\\APBNGM501\F$\Maintenance.log'

Open in new window

Do you get a directory listing including 'Maintenance.log' when running this:
Get-ChildItem '\\APBNGM501\F$'

Open in new window

0
PavanPAAuthor Commented:
Get-ChildItem '\\APBNGM501\F$' is working and showing all files
0
oBdACommented:
Did the Get-Item work, and if so, does this work:
Get-Content '\\APBNGM501\F$\Maintenance.log'

Open in new window

0
PavanPAAuthor Commented:
Get-Content '\\APBNGM501\F$\Maintenance.log' works and gets all log :)
0
PavanPAAuthor Commented:
What I want is first 5 lines and last 3 lines
0
PavanPAAuthor Commented:
I will be on cloud 9 if this can be done
0
oBdACommented:
This is a bit more verbose; it works flawlessly here.
$LogFile = 'C:\Temp\Maintenance.log'
$ServerFile = 'C:\Temp\servers.txt'
$First = 5
$Last = 3
$PropertyList = @('Col_1', 'Col_2', 'Col_3', 'Col_4', 'Col_5', 'Col_6', 'Col_7', 'Col_8')

Function Get-ResultObject([string]$ComputerName, [string[]]$Lines, [string]$Exception) {
	$Result = '' | Select-Object -Property (@('ComputerName') + $PropertyList + @('Exception'))
	$Result.ComputerName = $ComputerName
	If ([string]::IsNullOrEmpty($Exception)) {
		## Lines[] is zero based!
		$Result.'Col_1' = $Lines[0]
		$Result.'Col_2' = $Lines[1]
		$Result.'Col_3' = $Lines[2]
		$Result.'Col_4' = $Lines[3]
		$Result.'Col_5' = $Lines[4]
		$Result.'Col_6' = $Lines[5]
		$Result.'Col_7' = $Lines[6]
		$Result.'Col_8' = $Lines[7]
	} Else {
		$Result.Exception = $Exception
	}
	Return $Result
}

$Results = Get-Content C:\Temp\servers.txt | ForEach-Object {
	$ComputerName = $_
	Try {
		$Unc = "\\$($_)\$($LogFile.Replace(':', '$'))"
		"Retrieving '$($Unc)' ... " | Write-Host -ForegroundColor White -NoNewline
		$Lines = Get-Content -Path $Unc -ErrorAction Stop | Select-Object -First $First -Last $Last
		"OK" | Write-Host -ForegroundColor Green
		Get-ResultObject -ComputerName $ComputerName -Lines $Lines
	} Catch {
		"ERROR" | Write-Host -ForegroundColor Red
		Get-ResultObject -ComputerName $ComputerName -Exception $_.Exception.Message
	}
}
$Results

Open in new window

2

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PavanPAAuthor Commented:
Hi Savanth, (oBdA)

I hope I got your name correct and the script worked like as you said and I am on cloud 9 for this. I really do not know how to thank you so I would request you to please give me a contact number or way to speak to you for thanking you. Please do share how can I speak to you. I will write a review and appreciation to exchange expert for the help you have been doing for people like us to understand and learn things. Thanks exchange expert. I dont mind taking the big membership soon :)
1
PavanPAAuthor Commented:
Hi Savanth, (oBdA)

I hope I got your name correct and the script worked like as you said and I am on cloud 9 for this. I really do not know how to thank you so I would request you to please give me a contact number or way to speak to you for thanking you. Please do share how can I speak to you. I will write a review and appreciation to exchange expert for the help you have been doing for people like us to understand and learn things. Thanks exchange expert. I dont mind taking the big membership soon :)
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.