{"id":288,"date":"2014-09-21T15:17:49","date_gmt":"2014-09-21T19:17:49","guid":{"rendered":"http:\/\/adsecurity.org\/?p=288"},"modified":"2014-09-21T21:42:42","modified_gmt":"2014-09-22T01:42:42","slug":"powershell-parse-a-large-multi-line-text-field-based-on-string-value-extract-text","status":"publish","type":"post","link":"https:\/\/adsecurity.org\/?p=288","title":{"rendered":"PowerShell: Parse a Large Multi-Line Text Field Based on String Value &#038; Extract Text"},"content":{"rendered":"<p>Parsing a large multi-line text field (variable) for a specific string and extract text from it:<\/p>\n<p><code>$EventMessage =<br \/>\n@\u201d<br \/>\nAn account was successfully logged on.<\/p>\n<p>Subject:<br \/>\nSecurity ID:\u00a0 SYSTEM<br \/>\nAccount Name:\u00a0 METCORPWKS201$<br \/>\nAccount Domain:\u00a0 METCORP<br \/>\nLogon ID:\u00a0 0x2b5<br \/>\nLogon Type:10<br \/>\nNew Logon:<br \/>\nSecurity ID:\u00a0 METCORP\\Administrator<br \/>\nAccount Name:\u00a0 Administrator<br \/>\nAccount Domain:\u00a0 METCORPWKS201<br \/>\nLogon ID:\u00a0 0bc123d<br \/>\nLogon GUID:\u00a0 {00000000-0000-0000-0000-000000000000}<br \/>\nProcess Information:<br \/>\nProcess ID:\u00a0 0x123<br \/>\nProcess Name:\u00a0 C:\\Windows\\System32\\winlogon.exe<br \/>\nNetwork Information:<br \/>\nWorkstation Name: METCORPWKS201<br \/>\nSource Network Address: 10.10.10.201<br \/>\nSource Port:\u00a0 1234<br \/>\nDetailed Authentication Information:<br \/>\nLogon Process:\u00a0 User32<br \/>\nAuthentication Package: Negotiate<br \/>\nTransited Services: \u2013<br \/>\nPackage Name (NTLM only): \u2013<br \/>\nKey Length:\u00a0 0<\/p>\n<p>This event is generated when a logon session is created. It is generated on the computer that was accessed.<\/p>\n<p>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.<\/p>\n<p>The logon type field indicates the kind of logon that occurred. The most common types are 2 (interactive) and 3 (network).<\/p>\n<p>The New Logon fields indicate the account for whom the new logon was created, i.e. the account that was logged on.<\/p>\n<p>The network fields indicate where a remote logon request originated. Workstation name is not always available and may be left blank in some cases.<\/p>\n<p>The authentication information fields provide detailed information about this specific logon request.<\/p>\n<p>The authentication information fields provide detailed information about this specific logon request.<\/p>\n<p>Logon GUID is a unique identifier that can be used to correlate this event with a KDC event.<br \/>\nTransited services indicate which intermediate services have participated in this logon request.<br \/>\nPackage name indicates which sub-protocol was used among the NTLM protocols.<br \/>\nKey length indicates the length of the generated session key. This will be 0 if no session key was requested.<\/p>\n<p>\u201c@<\/p>\n<p>$EventMessageLogonNumber = $EventMessage | Select-String -Pattern \u201cLogon Type:\\w+\u201d -AllMatches | Select -ExpandProperty matches | Select -ExpandProperty value<br \/>\n$EventMessageAccountNameText = $EventMessage | Select-String -Pattern \u201cAccount Name:\\s+\\w+\u201d -AllMatches | Select -ExpandProperty matches | Select -ExpandProperty value<br \/>\n$EventMessageAccountName = (($EventMessageNameText -split \u201c:\u201d)[1]) -Replace(\u201c`t\u201d,\u201d\u201d)<br \/>\n$EventMessageWorkstationNameText = $EventMessage | Select-String -Pattern \u201cWorkstation Name:\\s+\\S+\u201d -AllMatches | Select -ExpandProperty matches | Select -ExpandProperty value<br \/>\n$EventMessageWorkstationName = (($EventMessageWorkstationNameText -split \u201c:\u201d)[1]) -Replace(\u201c`t\u201d,\u201d\u201d)<br \/>\n$EventMessageSourceIPText = $EventMessage | Select-String -Pattern \u201cSource Network Address:\\s+\\S+\u201d -AllMatches | Select -ExpandProperty matches | Select -ExpandProperty value<br \/>\n$EventMessageSourceIP = (($EventMessageSourceIPText -split \u201c:\u201d)[1]) -Replace(\u201c`t\u201d,\u201d\u201d)<br \/>\n$EventMessageLogonNumber<br \/>\n$EventMessageAccountName<br \/>\n$EventMessageWorkstationName<br \/>\n$EventMessageSourceIP<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Parsing a large multi-line text field (variable) for a specific string and extract text from it: $EventMessage = @\u201d An account was successfully logged on. Subject: Security ID:\u00a0 SYSTEM Account Name:\u00a0 METCORPWKS201$ Account Domain:\u00a0 METCORP Logon ID:\u00a0 0x2b5 Logon Type:10 New Logon: Security ID:\u00a0 METCORP\\Administrator Account Name:\u00a0 Administrator Account Domain:\u00a0 METCORPWKS201 Logon ID:\u00a0 0bc123d Logon &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"https:\/\/adsecurity.org\/?p=288\">Continue reading<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[119,22,120],"class_list":["post-288","post","type-post","status-publish","format-standard","hentry","category-powershell","tag-findspecificstring","tag-powershellcode","tag-textparsing","item-wrap"],"_links":{"self":[{"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/posts\/288","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/adsecurity.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=288"}],"version-history":[{"count":1,"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/posts\/288\/revisions"}],"predecessor-version":[{"id":289,"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/posts\/288\/revisions\/289"}],"wp:attachment":[{"href":"https:\/\/adsecurity.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/adsecurity.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/adsecurity.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}