Solved

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

Posted on 2016-08-15
14
30 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 82

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
 
LVL 82

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 82

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 82

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 82

Accepted Solution

by:
oBdA earned 500 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
This article will help you understand what HashTables are and how to use them in PowerShell.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now