Solved

Need help with Powershell regular expressions

Posted on 2014-10-08
9
335 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
 
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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

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 script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
This tutorial will give a short introduction and overview of Backup Exec 2012 and how to navigate and perform basic functions. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as conne…
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…

863 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

19 Experts available now in Live!

Get 1:1 Help Now