Parse a PowerShell string is something that may need to be done when handling a report or log file.
Looking around the net on how to parse a PowerShell string, there’s a lot of commands out there using “Select-String” or “-split” on a variable. This is good for separating the words or values of a string, but not much else. Further functionality is required to search those strings and determine further action.
For Example, a log file could contain the info below:
1 2 3 4 5 |
Time: 2016-07-07 10:00:00 Host-IP: 192.168.1.1 Response: GET |
We want our script to always return the Host-IP, which might always be unique
The script to parse a string is below. While it is meant to be an example of how a string can be parsed, numbered, and called, it allows you to search for a value in the string. You’re also able to return a value AFTER the one you searched, which will be key in the example above.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
### Parse an inputted string and search for a value, ### return the value or any value after it. ############################################################ # Prompt for string, search value, and adjust returned value. ############################################################ write-host -NoNewline -BackgroundColor Black -ForegroundColor Green "Please input a string: " $string = Read-Host write-host `n write-host -NoNewline -BackgroundColor Black -ForegroundColor Green "Please specify search word: " $search = Read-Host write-host `n write-host -BackgroundColor Black -ForegroundColor Green "Enter which value [int] you would like to return" $returnValue = Read-Host '[Example: 0 is the value you searched. 1 is the next word]' ############################################################ ## Create array to hold and count string values ############################################################ $WordArray = New-Object System.Data.DataTable ## Create Columns ## $col1 = New-Object System.Data.DataColumn("Word") $col2 = New-Object System.Data.DataColumn("WordCount") $WordArray.columns.Add($col1) $WordArray.columns.Add($col2) # Function to add new row function Add-ArrayRow { $Word = $args[0] $WordCount = $args[1] $row = $WordArray.NewRow() $row["Word"] = $Word $row["WordCount"] = $WordCount $WordArray.rows.Add($row) } ############################################################ # Remove new lines and tabs from string. Depending on the type of string you're working with, you may need to modify how the string is handled. $string = $string.Replace("`n"," ") $string = $string.Replace("`t"," ") $string = $string.Replace(" "," ") $string = $string.Replace(" "," ") $string = $string.trim() # Split the string by space $splitString = $string -split ' ' # Set first word count $wordCount = 1 ########################################################### # Add the words of a string to the array foreach($word in $splitString){ Add-ArrayRow $word $wordCount $wordCount++ } # Print the Array Out-host -InputObject $WordArray ########################################################### # Search the array for the search value that was entered. foreach($entry in $WordArray){ # If search value was found, calculate which value to return. IF($entry.word -eq $search){ write-host '' # Create variable to hold search value wordCount. $wNumber = [int]($entry.WordCount) # Calculate actual value to return by adding search value with value that was entered. $WordEntry = $wNumber + $returnValue # Retrieve the value $Rvalue = ($wordArray.Rows | where {$_.wordCount -eq $wordEntry}).word write-host -NoNewline -BackgroundColor Black -ForegroundColor Green "Returned Value:" write-host -NoNewline ' ' # Return the value return $Rvalue } } |
To map out the steps of the script, after the user enters values, the following steps are taken:
- Remove line breaks, tabs, and most extra spaces from the string.
- Trim spaces from beginning and end of string.
- Split the string by ‘ ‘
- This creates a new array
- Loop through the array and add the values to a second array, which also counts the values.
- Loop through the second array, seeing if any value matches the “search” which the user provided.
- If value is found, calculate the actual value to return. 0 = the searched term. 1= the next value in the array, 2 = the second value after the searched term, etc.
- Return the value.
In our example, to test the functionality we can paste the string when prompted:
Then enter a value to search, in this case “Host-IP:” – including the colon:
Enter the value you want to return, relative to “Host-IP”, which is the next value (1) “192.168.1.1”
Instead of inputting a value manually, this script could be modified to import a file instead. I would be open to expanding the script to do so by request.