PowerShell: Parse a Large Multi-Line Text Field Based on String Value & Extract Text

Parsing a large multi-line text field (variable) for a specific string and extract text from it:

$EventMessage =
@”
An account was successfully logged on.

Subject:
Security ID:  SYSTEM
Account Name:  METCORPWKS201$
Account Domain:  METCORP
Logon ID:  0x2b5
Logon Type:10
New Logon:
Security ID:  METCORP\Administrator
Account Name:  Administrator
Account Domain:  METCORPWKS201
Logon ID:  0bc123d
Logon GUID:  {00000000-0000-0000-0000-000000000000}
Process Information:
Process ID:  0x123
Process Name:  C:\Windows\System32\winlogon.exe
Network Information:
Workstation Name: METCORPWKS201
Source Network Address: 10.10.10.201
Source Port:  1234
Detailed Authentication Information:
Logon Process:  User32
Authentication Package: Negotiate
Transited Services: –
Package Name (NTLM only): –
Key Length:  0

This event is generated when a logon session is created. It is generated on the computer that was accessed.

The subject fields indicate the account on the local system which requested the logon. This is most commonly a service such as the Server service, or a local process such as Winlogon.exe or Services.exe.

The logon type field indicates the kind of logon that occurred. The most common types are 2 (interactive) and 3 (network).

The New Logon fields indicate the account for whom the new logon was created, i.e. the account that was logged on.

The network fields indicate where a remote logon request originated. Workstation name is not always available and may be left blank in some cases.

The authentication information fields provide detailed information about this specific logon request.

The authentication information fields provide detailed information about this specific logon request.

Logon GUID is a unique identifier that can be used to correlate this event with a KDC event.
Transited services indicate which intermediate services have participated in this logon request.
Package name indicates which sub-protocol was used among the NTLM protocols.
Key length indicates the length of the generated session key. This will be 0 if no session key was requested.

“@

$EventMessageLogonNumber = $EventMessage | Select-String -Pattern “Logon Type:\w+” -AllMatches | Select -ExpandProperty matches | Select -ExpandProperty value
$EventMessageAccountNameText = $EventMessage | Select-String -Pattern “Account Name:\s+\w+” -AllMatches | Select -ExpandProperty matches | Select -ExpandProperty value
$EventMessageAccountName = (($EventMessageNameText -split “:”)[1]) -Replace(“`t”,””)
$EventMessageWorkstationNameText = $EventMessage | Select-String -Pattern “Workstation Name:\s+\S+” -AllMatches | Select -ExpandProperty matches | Select -ExpandProperty value
$EventMessageWorkstationName = (($EventMessageWorkstationNameText -split “:”)[1]) -Replace(“`t”,””)
$EventMessageSourceIPText = $EventMessage | Select-String -Pattern “Source Network Address:\s+\S+” -AllMatches | Select -ExpandProperty matches | Select -ExpandProperty value
$EventMessageSourceIP = (($EventMessageSourceIPText -split “:”)[1]) -Replace(“`t”,””)
$EventMessageLogonNumber
$EventMessageAccountName
$EventMessageWorkstationName
$EventMessageSourceIP

(Visited 2,688 times, 1 visits today)