Link to home
Start Free TrialLog in
Avatar of Ampletrix
AmpletrixFlag for Australia

asked on

Need help with Powershell regular expressions

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
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

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.
SOLUTION
Avatar of Frank Helk
Frank Helk
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of Ampletrix

ASKER

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!
Great help.
Ah, I see what you mean by "post-fix".  I think the word you're looking for is "suffix".  :)
Glad I could help.
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.