?
Solved

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

Posted on 2016-08-15
14
Medium Priority
?
50 Views
Last Modified: 2016-08-19
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...
0
Comment
Question by:PavanPA
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 5
14 Comments
 
LVL 85

Expert Comment

by:oBdA
ID: 41757384
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
 

Author Comment

by:PavanPA
ID: 41757616
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
 

Author Comment

by:PavanPA
ID: 41757617
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
Bringing Advanced Authentication to the SMB Market

WatchGuard announces the acquisition of advanced authentication provider, Datablink, with one mission – to bring secure authentication to SMB, mid-market, and distributed enterprises with a cloud-based solution, ideal for resale via their established channel & MSSP community.

 
LVL 85

Expert Comment

by:oBdA
ID: 41757625
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
 

Author Comment

by:PavanPA
ID: 41757652
I have access to those files and servers
I open them and access them every month
0
 
LVL 85

Expert Comment

by:oBdA
ID: 41757687
"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
 

Author Comment

by:PavanPA
ID: 41757870
Get-ChildItem '\\APBNGM501\F$' is working and showing all files
0
 
LVL 85

Expert Comment

by:oBdA
ID: 41757895
Did the Get-Item work, and if so, does this work:
Get-Content '\\APBNGM501\F$\Maintenance.log'

Open in new window

0
 

Author Comment

by:PavanPA
ID: 41760683
Get-Content '\\APBNGM501\F$\Maintenance.log' works and gets all log :)
0
 

Author Comment

by:PavanPA
ID: 41760685
What I want is first 5 lines and last 3 lines
0
 

Author Comment

by:PavanPA
ID: 41760687
I will be on cloud 9 if this can be done
0
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 41761017
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
 

Author Comment

by:PavanPA
ID: 41763364
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
 

Author Closing Comment

by:PavanPA
ID: 41763366
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

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will help you understand what HashTables are and how to use them in PowerShell.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question