?
Solved

Need help with Powershell regular expressions

Posted on 2014-10-08
9
Medium Priority
?
364 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 82

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 400 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 40

Accepted Solution

by:
footech earned 1200 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 46

Assisted Solution

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

Open in new window

0
 
LVL 40

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 40

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

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
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 …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

765 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