Solved

Need help with Powershell regular expressions

Posted on 2014-10-08
9
338 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 79

Expert Comment

by:David Johnson, CD, MVP
ID: 40370029
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 14

Assisted Solution

by:frankhelk
frankhelk earned 100 total points
ID: 40370408
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
ID: 40370438
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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 45

Assisted Solution

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

Open in new window

0
 
LVL 39

Expert Comment

by:footech
ID: 40371525
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
ID: 40372192
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
ID: 40372194
Great help.
0
 
LVL 39

Expert Comment

by:footech
ID: 40372595
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
ID: 40372604
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

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...
This article will help you understand what HashTables are and how to use them in PowerShell.
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

816 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

12 Experts available now in Live!

Get 1:1 Help Now