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

1

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

3 Comments

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
@ThatRobin What version of PowerShell are you using? ($PSVersionTable.PSVersion.ToString())
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`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.