Get-Process output into a .txt Name, Id, PriorityClass, UserProcessorTime, TotalProcessorTime sorted by TotalProcessorTime asc with Id > 100 - powershell-2.0

I am trying to do the task using this container:
Get-Process | Format-List Name, Id, PriorityClass, UserProcessorTime, TotalProcessorTime | Where-Object {$_.Id -gt 100} | Sort-Object TotalProcessorTime
But, it gives no output and I don't know how to do the file output here. Could you, please, point me where my mistake is? Thank you.

Related

How to select first tab-separated column in first line?

I would like to grep -o the date... only the first value in line 1 from a tab-separated file with PowerShell v2.
I used the following command to get the first line:
Get-Content "C:\filepath\file.txt" | select -first 1
How can get also the first column of this line? Or via Regex?
Sample:
15.03.2017 22:42:49 uSecAction = 4
15.03.2017 22:42:49 uAction_Program = 5
15.03.2017 22:42:49 uSecAction_Program = 4
Thanks in advance.
This snippet should help you to get the first column of the tab separated file :
Get-Content "d:\my docs\file.txt" |select -first 1| %{ ($_ -split "\t+")[0]}

How to parse a File in power shell with conditional and garbage

I need to get part of this file for example, I need extract the following
Main, Branches\Branch1
in one variable also the I cannot have duplicate values
It is possible with powershell?
This is the file:
This is a garbage line
This is another garbage line
c:\Folder\Main\Folder\..\Folder
c:\Folder\Main\Folder\..\Folder
c:\Folder\Branches\Branch1\Folder\..\Folder
c:\Folder\Branches\Branch1\Folder\..\Folder
c:\Folder\Branches\Branch1\Folder\..\Folder
c:\Folder\Main\Folder\..\Folder
c:\Folder\Main\Folder\..\Folder
this is the final line..
But of course ...
According to the fact $files contain your lines
$files = Get-content "your file"
You can use the following to be sure that there is no duplicate :
$files | Sort-Object -Unique
Then you can use Test-path to be sure that path exists
$files | Sort-Object -Unique | where {Test-Path $_ -ErrorAction SilentlyContinue}
This will extract those values from the sample data using a -like filter to take out the garbage and a -replace to do the extract. The sort -unique will remove the duplicates, but it won't keep the extracted values in the same order they were in the file.
(get-content testfile.txt) -like 'c:\Folder*' -replace 'c:\\Folder\\(.+?)\\Folder.+','$1' |
sort -unique

Parsing lines no older than a specific date

I am working on some PowerShell scripting stuff not having a strong PowerShell skill under my belt. Basically I am more into Java, but I need to use PowerShell to get some things done at work.
What I have so far is a little snippet that parses my desired log file, returns an object and writes it to a file.
# find all lines with "successfully installed":
Select-String -Path $env:windir\WindowsUpdate.log -Pattern 'successfully installed' |
ForEach-Object {
$information = $_ | Select-Object -Property Date, Product
$parts = $_.Line -split '\t'
[DateTime]$information.Date = $parts[0] + ' ' + $parts[1].SubString(0,8)
$information.Product = ($_.Line -split 'following update: ')[-1]
$information
} | Out-File parsedUpdate.log
My output looks like this
What I would love to do next is to
Get rid of the line that labels the Properties and the line below it as well as I would send the output to EventLog soon.
Select only the lines that are no older than a specific date.
So how would I go about rejecting these two lines? Regarding the date problem I would love the exclude lines older than a specific date which I specify.
I have read that that Select-String has an -Exclude <String> property.
Would it be smart (and of course possible) to use this Directive to Exclude lines with a specific date and how would I do that - lets say for example, to reject any line older than a week from now?
thanks in advance.
Andrew
The timestamps on the actual log entries are in string sortable format, so you can do your date filtering early, before object creation:
$Start = (get-date).AddDays(-7).ToString('yyyy-MM-dd')
Select-String -Path $env:windir\WindowsUpdate.log -Pattern 'successfully installed' |
Where {$_.line -gt $Start} |
All you have to do for the (2) part of your question is add a filter to your result set, since you've already got objects with the Date property. The filter clause will look like Where-Object { $_.Date -ge "2/6/2015" } just replace with the date you are interested in.
# find all lines with "successfully installed" no older than 2/6/2015:
Select-String -Path $env:windir\WindowsUpdate.log -Pattern 'successfully installed' |
ForEach-Object {
$information = $_ | Select-Object -Property Date, Product
$parts = $_.Line -split '\t'
[DateTime]$information.Date = $parts[0] + ' ' + $parts[1].SubString(0,8)
$information.Product = ($_.Line -split 'following update: ')[-1]
$information
} | Where-Object { $_.Date -ge "2/6/2015" } | Out-File parsedUpdate.log

How to use Sort-Object on name with paren

I"m trying to sort the output from Get-Process, like this:
Get-Process | where { $_.Name -Like "Chrome" } | Sort id
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
Sorting by Id works as expected. How do I specify 'CPU(s)'? If I replace id with CPU(s), I get an error:
Get-Process | where { $_.Name -Like "Chrome" } | Sort CPU(s)
The term 's' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Thanks for any hints...
you can use CPU without the s
Get-Process | where { $_.Name -Like "Chrome" } | Sort CPU
if you do
(Get-Process | where { $_.Name -Like "Chrome" } )[0] | FL *
You can have the right names in a list.
What you see normally is the custom format used by powershell command.

Use grep -A1 to return a value in the second line as long as a numeric value in the first line is met

I have log entries that are paired two lines each. I have to parse the first line to extract
a number to know if it is greater than 5000. If this number is greater than 5000 then I need to return the second line, which will also be parsed to retrieve an ID.
I know how to grep all of the info and to parse it. I don't know how to make the grep ignore
things if they are less than a particular value. Note that I am not committed to using grep if some
other means like awk/sed can be substituted.
Raw Data (two lines separated for example clarity).
The target of my grep is the number 5001
following "credits extracted = ", if this is over 5000 then I want to return number "12345" from
the second line --------------------------
2012-03-16T23:26:12.082358 0x214d000 DEBUG ClientExtractAttachmentsPlayerMailTask for envelope 22334455 finished: credits extracted = 5001, items extracted count = 0, status = 0. [Mail.heomega.mail.Mail](PlayerMailTasks.cpp:OnExtractAttachmentsResponse:944)
2012-03-16T23:26:12.082384 0x214d000 DEBUG Mail Cache found cached mailbox for: 12345 [Mail.heomega.mail.Mail](MailCache.cpp:GetCachedMailbox:772)
Snippits --------------------------
-- Find the number of credits extracted, without the comma noise:
grep "credits extracted = " fileName.log | awk '{print $12}' | awk -F',' '{print $1}'
-- Find the second line's ID no matter what the value of credits extracted is:
grep -A1 "credits extracted = " fileName.log | grep "cached mailbox for" | awk -F, '{print $1}' | awk '{print $10}'
-- An 'if' statement symbolizing the logic I need to acquire:
v_CredExtr=5001; v_ID=12345; if [ $v_Cred -gt 5000 ]; then echo $v_ID; fi;
You can do everything with a single AWK filter I believe:
#!/usr/bin/awk -f
/credits extracted =/ {
credits = substr($12, 1, length($12) - 1) + 0
if (credits > 5000)
show_id = 1
next
}
show_id == 1 {
print $10
show_id = 0
}
Obviously, you can stuff all the AWK script in a shell string inside a script, even multiline. I showed it here in its own script for clarity.
P.S: Please notify when it works ;-)

Resources