Coallate logs by date/time, from seperate servers, with powershell - sorting

I'd like to have a script that grabs the logs from a list of paths and sorts all the entries in the logs into one large consolidated log. I'm fairly new at programming and powershell, but I've got a start. The log entries look like this:
2013-07-17 05:00:00,003 INFO [com.mpi.mp.viewer.web.servlet.ViewerLogFlusher] Skipped sending the empty string to wmsiislo
What I'm thinking is that I need to somehow parse each line into a multidimensional array, and sort it. The problem is, I can't get the hang of the [DateTime] method. My very incomplerte code is below:
#Code to append all logs together
$Biglog = Get-Content C:\Temp\logs\server1.log
foreach ($line in $Biglog){
#do something
}

try this :
ls c:\temp\logs\*.log | gc | out-file c:\temp\log.txt
Import-Csv C:\temp\log.txt -Header "date","info" |Sort-Object date | export-csv c:\temp\sortedlogs.csv

Related

Powershell script: List files with specific change date (Amount if possible)

For license porpuses I try to automate the counting process instead of having to login into every single server, go into directory, search a file name and count the results based on the change date.
Want I'm aiming for:
Running a powershell script every month that checks the directory "C:\Users" for the file "Outlook.pst" recursively. And then filters the result by change date (one month or newer). Then packing this into an email to send to my inbox.
I'm not sure if that's possible, cause I am fairly new to powershell. Would appreciate your help!
It is possible.
I dont know how to start a ps session on a remote computer, but I think the cmdlet Enter-PSSession will do the trick. Or at least it was the first result while searching for "open remote powershell session". If that does not work use the Invoke-Command as suggested by lit to get $outlookFiles as suggested below.
For the rest use this.
$outlookFiles = Get-ChildItem -Path "C:\Users" -Recurse | Where-Object { $_.Name -eq "Outlook.pst" }
Now you have all files that have this name. If you are not familiar with the pipe in powershell it redirects all objects it found with the Get-ChildItem to the next pipe section and here the Where-Object will filter the received objects. If the current object ($_) will pass the condition it is returned by the whole command.
Now you can filter these objects again to only include the latest ones with.
$latestDate = (Get-Date).AddMonths(-1)
$newFiles = $outlookFiles | Where-Object { $_.LastAccessTime -gt $latestDate }
Now you have all the data you want in one object. Now you only have to format this how you like it e.g. you could use $mailBody = $newFiles | Out-String and then use Send-MailMessage -To x#y.z -From r#g.b -Body $mailBodyto send the mail.

How to use the log parser in Windows PowerShell by using for each loops to query the logs from multiple exchange servers?

I would like to use the below mentioned command in windows powershell by using the for each loops. Please let me know if you have any ideas on this query.
As I have little bit idea to input multiple values for the FROM parameter as because I need to search logs from the different servers from the given location.
I would like to feed the values for FROM filed like below but I don't have an complete idea to create the entire PowerShell script structure.
\\server1\d$\Program Files\Microsoft\Exchange\V15\Logging\HttpProxy\Mapi\*.*
\\server2\d$\Program Files\Microsoft\Exchange\V15\Logging\HttpProxy\Mapi\*.*
Command I created for individual server:
LogParser.exe "
SELECT DateTime,AuthenticatedUser,UserAgent
FROM 'd:\Program Files\Microsoft\Exchange\V15\Logging\HttpProxy\Mapi\*.*'
WHERE AuthenticatedUser LIKE '%user1%'
AND UserAgent LIKE '%Microsoft Office%'" -i:CSV -o:csv > "C:\Log parser\server1\1.csv"
Assuming you have access to the administrative shares on the remote servers you could do something like this:
$servers = 'server1', 'server2', ...
$servers | ForEach-Object {
LogParser.exe -i:csv -o:csv -q "SELECT DateTime,AuthenticatedUser,UserAgent
FROM '\\${_}\D$\Program Files\Microsoft\Exchange\V15\Logging\HttpProxy\Mapi\*.*'
WHERE AuthenticatedUser LIKE '%user1%'
AND UserAgent LIKE '%Microsoft Office%'" |
Out-File "C:\Log parser\${_}.csv"
}

Unable to Output Data to text file

I am using the following script to pull a list of all my computers from my wsus server. It then places then in a neat list and this works fine.
However, when I try to output the data to a text file it creates the text file but writes no data. Is there something I am missing in the Output part? Full script below.
$wsus = 'halvedge2'
[reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration") | Out-Null
$wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer()
$wsus.GetComputerTargets() | Select FullDomainName, LastReportedStatusTime, LastSyncTime
out-file -filepath "\\halvedge2\c$\PS1\WSUS\Last installed Update\updates.txt"
Additionally I would like to add an If statement to it looks at the LastSyncTime and for any LastSyncTime over 30 days it only returns them Computer Targets. How can I achieve this?
You do not have to out-file separately:
If the below yields value, then Instead of this:
$wsus.GetComputerTargets() | Select FullDomainName, LastReportedStatusTime, LastSyncTime
Do This:
$wsus.GetComputerTargets() | Select FullDomainName, LastReportedStatusTime, LastSyncTime | Out-File "D:\updates.txt" -Append -Force
Note: First try saving locally. If it works, then try remotely on the UNC. If remote fails, then give permission to the specific user to save it.

Sort output of Get-Migrations ascending?

By default, the Get-Migrations command, when run in the Visual Studio Package Manager Console, returns the list of Entity Framework migrations that have been applied to the target database in descending order. That is, the list is sorted to have most recent migrations first.
Is there a way to make Get-Migrations return the list of migrations in ascending order, that is, oldest migrations first, instead?
What I've tried so far:
Piped the output to the Sort-Object cmdlet, e.g. Get-Migrations [my params] | Sort-Object -Property Name (I'm not sure what property name to specify, or if this is even applicable?). All of these attempts silently failed (no error message displayed, no change in the Get-Migrations output).
Looked at the Powershell help for Get-Migrations (Get-Help Get-Migrations) and searched the web to see if Get-Migrations itself has a parameter that controls the output sort order. Couldn't find one.
Output of the Get-Migrations command I'm currently getting:
PM> Get-Migrations [my arguments...]
Retrieving migrations that have been applied to the target database.
201704121534436_SomeMigration999
201703291334212_SomeMigration998
[lots and lots more records here that are a pain to scroll through...]
201410110448547_InitializeTables
201410110018266_InitialCreate
Based on the provided sample output it seems the command returns text.
I'd split on the linebreaks, and the on the _ to get the 2 properties separated, be able to sort things afterwards:
#$migrations = Get-Migrations "..."
#test values
$migrations = "201704121534436_SomeMigration999
201703291334212_SomeMigration998
201410110448547_InitializeTables
201410110018266_InitialCreate"
$migrations.Split("`n") | ForEach-Object {
New-Object -TypeName psobject -Property #{
Date = $_.Split("_")[0]
Name = $_.Split("_")[1]
}
} | Sort-Object Name

Extract hostnames from Perfmon blg with Powershell

I'm writing a script which will automate the extraction of data from .blg Perfmon logs.
I've worked out the primary Import-Counter commands I will need to use to get the data out, but am trying to parametrise this so that I can do it for each machine in the log file (without having to open the log up in Perfmon, which can take 15 minutes or sometimes more, and is the reason I'm writing this script), and find out what each hostname is.
The script I have does the job, but it still takes a minute to return the data I want, and I wondered if there was a simpler way to do this, as I'm not too familiar with Powershell?
Here's what I have:
$counters = Import-Counter -Path $log_path$logfile -ListSet * | Select-Object paths -ExpandProperty paths
$svrs = #()
# for each line in the list of counters, extract the name of the server and add it to the array
foreach ($line in $counters) {
$svrs += $line.split("\")[2]
}
# remove duplicates and sort the list of servers
$sorted_svrs = $svrs | sort -unique
foreach ($svr in $sorted_svrs) {
Write-Host $svr
}
I'm just printing the names for the moment, but they'll go into an array in the proper script, and then I'll run my Import-Counter block with each of these hosts parametrised in.
Just wondered if there was a better way of doing this?
$sorted_svrs=Import-Counter "$log_path$logfile" -Counter "\\*\physicaldisk(_total)\% disk time" | %{$_.countersamples.path.split("\")[2]} | sort -Unique

Resources