Regex kurz erklärt

Regex Pattern

Wichtige Regex Pattern:

\d digit [0-9]
\w alpha numeric [a-zA-Z0-9_]
\s whitespace character
.  any character except newline
() sub-expression 
\  escape the next character

Cmdlets

Get-Content
Select-String
Select-Object
Split
Replace

Aus einem Logfile einen Wert exportieren

Dazu benötigen wir ein Logfile und die Cmdlets Select-String und Select-Object mit ihren Parametern.

$content = Get-Content C:\Windows\WindowsUpdate.log

$content enthält nun:
Windows Update logs are now generated using ETW (Event Tracing for Windows).
Please run the Get-WindowsUpdateLog PowerShell command to convert ETW traces into a readable WindowsUpdate.log.

For more information, please visit http://go.microsoft.com/fwlink/?LinkId=518345

Select-String

Mit Select-String schränken wir den Inhalt auf die Zeilen ein, welche ‘PowerShell’ enthalten.

$ret = $content | Select-String -Pattern 'PowerShell' -AllMatches

$ret ist zu einem System.Object umgewandelt worden und enthält nun:

Please run the Get-WindowsUpdateLog PowerShell command to convert ETW traces into a readable WindowsUpdate.log.

Dieses Object können wir nun nach deren Properties und Methods befragen:

$ret | Get-Member

TypeName: Microsoft.PowerShell.Commands.MatchInfo

Name         MemberType Definition
----         ---------- ----------
Equals       Method   bool Equals(System.Object obj)
GetHashCode  Method   int GetHashCode()
GetType      Method   type GetType()
RelativePath Method   string RelativePath(string directory)
ToString     Method   string ToString(), string ToString(string directory)
Context      Property Microsoft.PowerShell.Commands.MatchInfoContext Context {get;set;}
Filename     Property string Filename {get;}
IgnoreCase   Property bool IgnoreCase {get;set;}
Line         Property string Line {get;set;}
LineNumber   Property int LineNumber {get;set;}
Matches      Property System.Text.RegularExpressions.Match[] Matches {get;set;}
Path         Property string Path {get;set;}
Pattern      Property string Pattern {get;set;}

Für uns interessant ist das Property Matches, welches wir um weitere Properties befragen (können):

$ret.Matches

Groups   : {0}
  Success  : True
  Name     : 0
  Captures : {0}
  Index    : 36
  Length   : 10
  Value    : PowerShell

Select-Object

Nun interessiert uns ja eigentlich nur ob ‘PowerShell’ im Value steht und dies wollen wir zurückgeben:

$ret.Matches | Select-Object -ExpandProperty value

PowerShell

Hier die Kurzfassung

$content = Get-Content C:\Windows\WindowsUpdate.log
$ret = $content | Select-String -Pattern 'PowerShell' -AllMatches | `
Select-Object -ExpandProperty matches | Select-Object -ExpandProperty value

Sind nun mehr als eine Zeile mit den gesuchten Werten im Return, kann z.B. der letzte mit Select-Object ausgegeben werden:

$ret | Select-Object -Last 1

Split

$ret -split ':')

Replace

$ret -replace '\s+','''

 

Weitere Informationen zu Regex siehe Kevin Marquette.

Sidebar



Entdecke mehr von Tinus IT Wiki

Jetzt abonnieren, um weiterzulesen und auf das gesamte Archiv zuzugreifen.

Weiterlesen