Reputation: 85
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
Upvotes: 0
Views: 142
Reputation: 174435
-split
operator to split on the (first) :
[ordered
] dictionary[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
Upvotes: 3