I have text below where and need to get only the word “Completed”
Does anyone have a tip?
SessionID: 2020/03 / 31-2
Backup Specification: JOB_BACKUP
Session type: Backup (trans)
Started: Tuesday, March 31, 2020, 12:15:06 AM
Finished: Tuesday, March 31, 2020, 12:15:50 AM
Status: Completed
Number of warnings: 4
Number of errors: 0
User: HI
Group: EMPLOYED
Host: localhost
It depends on what you want to do with the keyword. For just picking the word, you can use match operator. If you want to validate that the string contains the keyword, you can use Like operator.
$content = get-content filecontainthestring
$content -match “completed”
You will get the keyword inside $Matches variable.
… hmmm … sorry, that’s wrong. Have you tried it? When you use a comparison operator like a filter on more than one object you get the resulting object. In this case the line containing the match.
… hhhmmmm … no again … like I mentioned above. You will only get the line containing the match.
Good night, sorry for the delay in responding. I forgot to comment, only the word “Completed” appears and nothing else and I have little knowledge in powershell. I need to use Foreach because it has several lines with the same result.
Based on what you said you need to only capture the word completed. If I understand this correctly, if you have a document with the word completed in it 8 times, you want to end up with a list of 8 instances of the word Completed?
In both examples, we are simply picking out the line that has Status: in it, splitting that line at the colon, grabbing the second [1] element, and trimming the extra white space in front of the word Completed. The output is captured in the variable result and is a single string ‘Completed’
Hi @Maurer, thanks for your support.
Script one, result appears blank.
Script two, only one result appears
PS C:\dp> C:\dp\Untitled2.ps1
Completed
PS C:\dp>
Example Content.txt
SessionID : 2020/03/31-2
Backup Specification: JOByyyyy
Session type : Backup (trans)
Started : terça-feira, 31 de março de 2020, 00:15:06
Finished : terça-feira, 31 de março de 2020, 00:15:50
Status : Completed
Number of warnings : 4
Number of errors : 0
User : DATAPROTECTOR
Group : yyyyy
Host : yyyyy
SessionID : 2020/03/31-3
Backup Specification: JOBXXXX
Session type : Backup (trans)
Started : terça-feira, 31 de março de 2020, 01:15:07
Finished : terça-feira, 31 de março de 2020, 01:15:51
Status : Completed
Number of warnings : 4
Number of errors : 0
User : DATAPROTECTOR
Group : XXXX
Host : XXXXX
I see I moved the output of $result outside of the entire foreach loop. I’ve corrected and tested it, however, I much prefer Olaf’s answer. Give them both a try and let us know if it is what you needed.