Link to home
Start Free TrialLog in
Avatar of winsystems
winsystems

asked on

Windows Powershell need help with Regular Expressions (RegEx) pattern

I am working with automating some tasks and am having issues with using regular expressions.  Here is part of some powershell lines that I am working on:

$InputFile = 'C:\Batch\Log\Quota.log'
$OutputFile = 'C:\Temp\Userlist.csv'
$RegEx = "This is where I am having difficulty"
Select-String -Path $InputFile -Pattern $RegEx -AllMatches | % {$_.Matches} | % {$_.Value} > $OutputFile

I have output in a file, "quota.log" which contain 'errors'.  Example are shown below

=========================================================================
The operation couldn't be performed because object 'hsuryawans' couldn't be found on 'DC1.University.edu'.
    + CategoryInfo          : NotSpecified: (0:Int32) [Set-Mailbox], ManagementObjectNotFoundException
    + FullyQualifiedErrorId : 3CF4B73F,Microsoft.Exchange.Management.RecipientTasks.SetMailbox
 
The operation couldn't be performed because object 'voleti' couldn't be found on 'DC1.University.edu'.
    + CategoryInfo          : NotSpecified: (0:Int32) [Set-Mailbox], ManagementObjectNotFoundException
    + FullyQualifiedErrorId : D35847FD,Microsoft.Exchange.Management.RecipientTasks.SetMailbox
 
The operation couldn't be performed because object 'egrinman' couldn't be found on 'DC1.University.edu'.
    + CategoryInfo          : NotSpecified: (0:Int32) [Set-Mailbox], ManagementObjectNotFoundException
    + FullyQualifiedErrorId : 21A1FB11,Microsoft.Exchange.Management.RecipientTasks.SetMailbox
=========================================================================
If I set the $RegEx value to “DC1.University.edu” I get back three returns so I know the Select-String command line works fine.

What I wish to get back is just the unique values for each ‘error’:

'hsuryawans'
'voleti'
'egrinman'

I have tried some tests setting the value of $RegEx but can’t seem to get what I wish.  This file contains multiple unique values contained within a single quote with no spaces and only has alphabetic character (no numbers, spaces or special characters like “.”’s)

As a bonus if someone know how to put a "header" on the output .csv file called Name; this would be appreciated.
Avatar of SubSun
SubSun
Flag of India image

Try this and see if it works for you...
GC C:\Temp\quota.log | 
	Select-string -Pattern "\s'([^']+)'\s" | 
		select  -ExpandProperty Matches | 
 % { $_ | Select @{N="Name";E={$_.groups[1].value}}}|
Export-Csv C:\Temp\Error.csv -NTI

Open in new window

BTB, It would be more easy if you do the error control on the script which generate "quota.log".
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America 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
Avatar of winsystems
winsystems

ASKER

Thank you both for your different perspectives on good solutions