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], ManagementObjectNotFoundEx ception
+ FullyQualifiedErrorId : 3CF4B73F,Microsoft.Exchang e.Manageme nt.Recipie ntTasks.Se tMailbox
The operation couldn't be performed because object 'voleti' couldn't be found on 'DC1.University.edu'.
+ CategoryInfo : NotSpecified: (0:Int32) [Set-Mailbox], ManagementObjectNotFoundEx ception
+ FullyQualifiedErrorId : D35847FD,Microsoft.Exchang e.Manageme nt.Recipie ntTasks.Se tMailbox
The operation couldn't be performed because object 'egrinman' couldn't be found on 'DC1.University.edu'.
+ CategoryInfo : NotSpecified: (0:Int32) [Set-Mailbox], ManagementObjectNotFoundEx ception
+ FullyQualifiedErrorId : 21A1FB11,Microsoft.Exchang e.Manageme nt.Recipie ntTasks.Se tMailbox
========================== ========== ========== ========== ========== =======
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.
$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], ManagementObjectNotFoundEx
+ FullyQualifiedErrorId : 3CF4B73F,Microsoft.Exchang
The operation couldn't be performed because object 'voleti' couldn't be found on 'DC1.University.edu'.
+ CategoryInfo : NotSpecified: (0:Int32) [Set-Mailbox], ManagementObjectNotFoundEx
+ FullyQualifiedErrorId : D35847FD,Microsoft.Exchang
The operation couldn't be performed because object 'egrinman' couldn't be found on 'DC1.University.edu'.
+ CategoryInfo : NotSpecified: (0:Int32) [Set-Mailbox], ManagementObjectNotFoundEx
+ FullyQualifiedErrorId : 21A1FB11,Microsoft.Exchang
==========================
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you both for your different perspectives on good solutions
Open in new window
BTB, It would be more easy if you do the error control on the script which generate "quota.log".