Solved

Need help with Powershell regular expressions

Posted on 2014-10-08
9
332 Views
Last Modified: 2014-10-10
Hello All,

I have a few servers that requires Windows Updates and patches etc..

As part of our Change Request process we need to document all the patches required by all servers. I can copy all the details of the required updates in a txt file, but then I have to 'clean up' the information. A text file is attached as an example.
 
What I'd like to achieve is in the attached text file named 'HiusApp01-example.txt'.
I have multiple txt files, each with the hostname as the file name.

I really need some help with a Powershell script with some regular expression,s to extract only the line that includes the KB article, for example: "Security Update for Windows Server 2008 R2 x64 Edition (KB2862330)", and then add this to a text file that includes the hostname (file name) with a post-fix. The script has to loop through all the text files in the "$workdir".

Here's what I have at the moment:

$workdir = "C:\Software\Updates"
$Txt = "$workdir\HiusApp01.txt"
$SearchStr  = "KB\d{4,7}"
Function GetAsmName($rTxt)
{    
$captures = gc $rTxt |
            select-string -Pattern $SearchStr |
            select -expa matches | select -expa value    
Set-Content -path $workdir\Output.txt -value $captures
}

GetAsmName $Txt


Unfortunately I'm running out of time, and this would really make my life considerably easier if I could get some help with this. Any assistance with this would be greatly appreciated.

Thanks in advance.
HiusApp01-example.txt
HiusApp01.txt
0
Comment
Question by:Ampletrix
9 Comments
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
Comment Utility
As part of our Change Request process we need to document all the patches required by all servers.

why don't they just install WSUS or SCCM and pull the reports from there.
0
 
LVL 13

Assisted Solution

by:frankhelk
frankhelk earned 100 total points
Comment Utility
I havn't checked the code itself, but the regex doesn't work, I think. I've tried it with Expresso and it doesn't filter out the lines.

I've tried ".*\(KB\d*\)" and it worked nice. Maybe "\(KB\d*\)" would do, too.
0
 
LVL 39

Accepted Solution

by:
footech earned 300 total points
Comment Utility
and then add this to a text file that includes the hostname (file name) with a post-fix
I don't know what this means.

The below outputs the file into a separate folder so that it doesn't get read in again.
$workdir = "C:\Software\Updates"
$SearchStr  = "KB[0-9]{4,7}"
Function GetAsmName($rTxt)
{    
Get-Content $rTxt | Where { $_ -match $SearchStr } |
 Set-Content -path "$workdir\Output\$($_.BaseName)-results.txt"
}

Get-ChildItem $workdir -filter "*.txt" | Where { $_.PsIsContainer -eq $false } | % { GetAsmName $_.FullName }

Open in new window

0
 
LVL 45

Assisted Solution

by:aikimark
aikimark earned 100 total points
Comment Utility
This pattern gets the entire line
(?:^|\n)(\w.*\(KB\d+\))(?:\r\n|$)

Open in new window

0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 39

Expert Comment

by:footech
Comment Utility
Just thought I'd mention that what I posted outputs the entire line when it finds a match within the line.  The regex can be expanded if it's not specific enough, but my test produced the same file as your example of desired output.
0
 

Author Comment

by:Ampletrix
Comment Utility
Hello everyone,

Thanks top everybody participating here. One of the reasons why I support (and like) EE.  :-)

@David Johnson: Our environment is super complicated. No internet access, Classified etc.. Just to give you a bit of the picture... We do use SCCM as well as WSUS for deployment, but unfortunately our ITIL or Change Request process requires that I go through each individual server, export the details of all updates that are ready to be installed, as in the KB article and details thereof, and add all this information for every server to the Change Request. This CRQ (Change Request) process goes through multiple peer reviewer, and then multiple levels of managerial 'approves'. Not much fun. For example: Let's say we have more-or-less 300 Windows Servers. Each with its own required patches ready to be installed. I have to raise a (luckily only one) CRQ to apply all the Patches and Updates across all servers. One requirement is that I add a matrix with all Server hostnames, KB articles and details thereof. This CRQ takes about 4 hours for me to complete, and then send to 'peer-reviewers', followed by a Change Approval Board meeting, and finally - managerial approvals...

@frankhelk and aikimark: Thanks for your input. I'll award both of you points, because that work partially. The main part what I was looking for with a 'prefix' or 'post-fix' was in Footech's line:

Set-Content -path "$workdir\Output\$($_.BaseName)-results.txt"

I just changed the '-results.txt' bit to '_updates.txt', and that is exactly what I needed. So - thanks for your input.

@Footech: Thanks dude. That's fantastic.

This saved me today! Thanks everyone!
0
 

Author Closing Comment

by:Ampletrix
Comment Utility
Great help.
0
 
LVL 39

Expert Comment

by:footech
Comment Utility
Ah, I see what you mean by "post-fix".  I think the word you're looking for is "suffix".  :)
Glad I could help.
0
 

Author Comment

by:Ampletrix
Comment Utility
Oops... It is a second language. I thought it is similar as to pre and post as in before and after. You've helped me twice now. Unfortunately I can't reward you here except to say thank you.
So - Thanks again.
Have a great weekend.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…

743 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

17 Experts available now in Live!

Get 1:1 Help Now