PoshCode Logo PowerShell Code Repository

Convert-TextObject.ps1 by Lee Holmes 4 years ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2134"></script>download | new post

From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes

  1. ##############################################################################
  2. ##
  3. ## Convert-TextObject
  4. ##
  5. ## From Windows PowerShell Cookbook (O'Reilly)
  6. ## by Lee Holmes (http://www.leeholmes.com/guide)
  7. ##
  8. ##############################################################################
  9.  
  10. <#
  11.  
  12. .SYNOPSIS
  13.  
  14. Convert a simple string into a custom PowerShell object.
  15.  
  16. .EXAMPLE
  17.  
  18. "Hello World" | Convert-TextObject
  19. Generates an Object with "P1=Hello" and "P2=World"
  20.  
  21. .EXAMPLE
  22.  
  23. "Hello World" | Convert-TextObject -Delimiter "ll"
  24. Generates an Object with "P1=He" and "P2=o World"
  25.  
  26. .EXAMPLE
  27.  
  28. "Hello World" | Convert-TextObject -Pattern "He(ll.*o)r(ld)"
  29. Generates an Object with "P1=llo Wo" and "P2=ld"
  30.  
  31. .EXAMPLE
  32.  
  33. "Hello World" | Convert-TextObject -PropertyName FirstWord,SecondWord
  34. Generates an Object with "FirstWord=Hello" and "SecondWord=World
  35.  
  36. .EXAMPLE
  37.  
  38. "123 456" | Convert-TextObject -PropertyType $([string],[int])
  39. Generates an Object with "Property1=123" and "Property2=456"
  40. The second property is an integer, as opposed to a string
  41.  
  42. .EXAMPLE
  43.  
  44. PS >$ipAddress = (ipconfig | Convert-TextObject -Delim ": ")[2].P2
  45. PS >$ipAddress
  46. 192.168.1.104
  47.  
  48. #>
  49.  
  50. [CmdletBinding(DefaultParameterSetName = "ByDelimiter")]
  51. param(
  52.     ## If specified, gives the .NET Regular Expression with which to
  53.     ## split the string. The script generates properties for the
  54.     ## resulting object out of the elements resulting from this split.
  55.     ## If not specified, defaults to splitting on the maximum amount
  56.     ## of whitespace: "\s+", as long as Pattern is not
  57.     ## specified either.
  58.     [Parameter(ParameterSetName = "ByDelimiter", Position = 0)]
  59.     [string] $Delimiter = "\s+",
  60.  
  61.     ## If specified, gives the .NET Regular Expression with which to
  62.     ## parse the string. The script generates properties for the
  63.     ## resulting object out of the groups captured by this regular
  64.     ## expression.
  65.     [Parameter(Mandatory = $true,
  66.         ParameterSetName = "ByPattern",
  67.         Position = 0)]
  68.     [string] $Pattern,
  69.  
  70.     ## If specified, the script will pair the names from this object
  71.     ## definition with the elements from the parsed string.  If not
  72.     ## specified (or the generated object contains more properties
  73.     ## than you specify,) the script uses property names in the
  74.     ## pattern of P1,P2,...,PN
  75.     [Parameter(Position = 1)]
  76.     [Alias("PN")]
  77.     [string[]] $PropertyName = @(),
  78.  
  79.     ## If specified, the script will pair the types from this list with
  80.     ## the properties from the parsed string.  If not specified (or the
  81.     ## generated object contains more properties than you specify,) the
  82.     ## script sets the properties to be of type [string]
  83.     [Parameter(Position = 2)]
  84.     [Alias("PT")]
  85.     [type[]] $PropertyType = @(),
  86.  
  87.     ## The input object to process
  88.     [Parameter(ValueFromPipeline = $true)]
  89.     [string] $InputObject
  90. )
  91.  
  92. begin {
  93.     Set-StrictMode -Version Latest
  94. }
  95.  
  96. process {
  97.     $returnObject = New-Object PSObject
  98.  
  99.     $matches = $null
  100.     $matchCount = 0
  101.  
  102.     if($PSBoundParameters["Pattern"])
  103.     {
  104.         ## Verify that the input contains the pattern
  105.         ## Populates the matches variable by default
  106.         if(-not ($InputObject -match $pattern))
  107.         {
  108.             return
  109.         }
  110.  
  111.         $matchCount = $matches.Count
  112.     $startIndex = 1
  113.     }
  114.     else
  115.     {
  116.         ## Verify that the input contains the delimiter
  117.         if(-not ($InputObject -match $delimiter))
  118.         {
  119.             return
  120.         }
  121.  
  122.         ## If so, split the input on that delimiter
  123.         $matches = $InputObject -split $delimiter
  124.         $matchCount = $matches.Length
  125.         $startIndex = 0
  126.     }
  127.  
  128.     ## Go through all of the matches, and add them as notes to the output
  129.     ## object.
  130.     for($counter = $startIndex; $counter -lt $matchCount; $counter++)
  131.     {
  132.         $currentPropertyName = "P$($counter - $startIndex + 1)"
  133.         $currentPropertyType = [string]
  134.  
  135.         ## Get the property name
  136.         if($counter -lt $propertyName.Length)
  137.         {
  138.             if($propertyName[$counter])
  139.             {
  140.                 $currentPropertyName = $propertyName[$counter - 1]
  141.             }
  142.         }
  143.  
  144.         ## Get the property value
  145.         if($counter -lt $propertyType.Length)
  146.         {
  147.             if($propertyType[$counter])
  148.             {
  149.                 $currentPropertyType = $propertyType[$counter - 1]
  150.             }
  151.         }
  152.  
  153.         Add-Member -InputObject $returnObject NoteProperty `
  154.             -Name $currentPropertyName `
  155.             -Value ($matches[$counter].Trim() -as $currentPropertyType)
  156.     }
  157.  
  158.     $returnObject
  159. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me