Solved

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

Posted on 2016-08-15
14
41 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 84

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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 84

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 84

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 84

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 84

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.

829 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