0

I have the ouput of an IBM Tivoli command that is really a bunch of keys and values, it looks like this

Name                   : NT_Paging_File_Warning
Full Name              : 
Description            : Knt:KNT1340
Type                   : Windows OS
Formula                : *IF *VALUE NT_Paging_File.%_Usage *GE 75 *AND *VALUE NT_Paging_File.%_Usage *LT 0 *AND *VALUE NT_Paging_File.Pagefile_Name_U *NE _Total
Sampling Interval      : 0/0:15:0
Run At Start Up        : Yes
Distribution           : 
Text                   : ADVICE("knt:"+$ISITSTSH.SITNAME$);
Action Location        : Agent
Action Selection       : System Command
System Command         : *NONE
True For Multiple Items: Action on First Item only
TEC Severity           : Warning
TEC Forwarding         :  
TEC Destination        :  

I want to efficiently take that output and cast it into a powershell object with the obvious attributes and values. As you can see some values may be empty and some attributes have spaces in the names. The : is the standard seperation between key and value.

What is a nice clean powershell way to go about this?

The reason I want to do this, is to send these records as objects down the pipe where I will use things like Where-Object on them to filter out the ones I want, etc, etc

That Robin
  • 75
  • 1
  • 9
  • 1
    Does this answer your question? [Trouble parsing string to object with PowerShell](https://stackoverflow.com/questions/51602171/trouble-parsing-string-to-object-with-powershell). Also note since PowerShell 7 [`ConvertFrom-StringData`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertfrom-stringdata?view=powershell-7) supports the `-Delimiter` parameter. – iRon Mar 19 '20 at 11:39

1 Answers1

3
  1. Use the -split operator to split on the (first) :
  2. Trim the resulting strings and use them to populate an [ordered] dictionary
  3. Cast dictionary to [pscustomobject]
$tivoliOutput = @'
Name                   : NT_Paging_File_Warning
Full Name              : 
Description            : Knt:KNT1340
Type                   : Windows OS
Formula                : *IF *VALUE NT_Paging_File.%_Usage *GE 75 *AND *VALUE NT_Paging_File.%_Usage *LT 0 *AND *VALUE NT_Paging_File.Pagefile_Name_U *NE _Total
Sampling Interval      : 0/0:15:0
Run At Start Up        : Yes
Distribution           : 
Text                   : ADVICE("knt:"+$ISITSTSH.SITNAME$);
Action Location        : Agent
Action Selection       : System Command
System Command         : *NONE
True For Multiple Items: Action on First Item only
TEC Severity           : Warning
TEC Forwarding         :  
TEC Destination        :  
'@ -split '\r?\n'

# Prepare dictionary to hold properties
$properties = [ordered]@{}

foreach($line in $tivoliOutput){
    # Split line into key and value
    $key,$value = $line -split ':',2 |ForEach-Object Trim

    # Remove spaces from the key names, comment out this line to retain names as-is
    $key = $key -replace '\s'

    # Add to our dictionary of properties
    $properties[$key] = $value
}

# create object
[pscustomobject]$properties
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • I get an error when I try to cast `$properties` to the `[pscustomobject]` it says `Cannot convert value "System.Collections.Specialized.OrderedDictionary" to type "System.Management.Automation.LanguagePrimitives+InternalPSCustomObject". Error: "Cannot process argument because the value of argument "name" is not valid. Change the value of the "name" argument and run the operation again."` I added a `$properties | Format-Table` and can see that the `$properties` are populdated correctly – That Robin Mar 19 '20 at 11:45
  • @ThatRobin What version of PowerShell are you using? (`$PSVersionTable.PSVersion.ToString()`) – Mathias R. Jessen Mar 19 '20 at 11:49
  • Version: 5.1.14409.1018 I have found I can cast it into another obejct, then poke that on down the pipe ... ` [pscustomobject]$test = $properties; $test` – That Robin Mar 19 '20 at 12:11