Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2016-08-15
14
Medium Priority
?
57 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
  • 9
  • 5
14 Comments
 
LVL 86

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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
LVL 86

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 86

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 86

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 86

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
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 anti-spam), the admin…
Screencast - Getting to Know the Pipeline

577 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