Public/ConvertTo-PropertyValue.ps1
function ConvertTo-PropertyValue { <# .SYNOPSIS Convert an object with various properties into an array of property, value pairs .DESCRIPTION Convert an object with various properties into an array of property, value pairs If you output reports or other formats where a table with one long row is poorly formatted, this is a quick way to create a table of property value pairs. There are other ways you could do this. For example, I could list all noteproperties from Get-Member results and return them. This function will keep properties in the same order they are provided, which can often be helpful for readability of results. .PARAMETER inputObject A single object to convert to an array of property value pairs. .PARAMETER leftheader Header for the left column. Default: Property .PARAMETER rightHeader Header for the right column. Default: Value .PARAMETER memberType Return only object members of this membertype. Default: Property, NoteProperty, ScriptProperty .EXAMPLE get-process powershell_ise | convertto-propertyvalue I want details on the powershell_ise process. With this command, if I output this to a table, a csv, etc. I will get a nice vertical listing of properties and their values Without this command, I get a long row with the same info .EXAMPLE #This example requires and demonstrates using the New-HTMLHead, New-HTMLTable, Add-HTMLTableColor, ConvertTo-PropertyValue and Close-HTML functions. #get processes to work with $processes = Get-Process #Build HTML header $HTML = New-HTMLHead -title "Process details" #Add CPU time section with top 10 PrivateMemorySize processes. This example does not highlight any particular cells $HTML += "<h3>Process Private Memory Size</h3>" $HTML += New-HTMLTable -inputObject $($processes | sort PrivateMemorySize -Descending | select name, PrivateMemorySize -first 10) #Add Handles section with top 10 Handle usage. $handleHTML = New-HTMLTable -inputObject $($processes | sort handles -descending | select Name, Handles -first 10) #Add highlighted colors for Handle count #build hash table with parameters for Add-HTMLTableColor. Argument and AttrValue will be modified each time we run this. $params = @{ Column = "Handles" #I'm looking for cells in the Handles column ScriptBlock = {[double]$args[0] -gt [double]$args[1]} #I want to highlight if the cell (args 0) is greater than the argument parameter (arg 1) Attr = "Style" #This is the default, don't need to actually specify it here } #Add yellow, orange and red shading $handleHTML = Add-HTMLTableColor -HTML $handleHTML -Argument 1500 -attrValue "background-color:#FFFF99;" @params $handleHTML = Add-HTMLTableColor -HTML $handleHTML -Argument 2000 -attrValue "background-color:#FFCC66;" @params $handleHTML = Add-HTMLTableColor -HTML $handleHTML -Argument 3000 -attrValue "background-color:#FFCC99;" @params #Add title and table $HTML += "<h3>Process Handles</h3>" $HTML += $handleHTML #Add process list containing first 10 processes listed by get-process. This example does not highlight any particular cells $HTML += New-HTMLTable -inputObject $($processes | select name -first 10 ) -listTableHead "Random Process Names" #Add property value table showing details for PowerShell ISE $HTML += "<h3>PowerShell Process Details PropertyValue table</h3>" $processDetails = Get-process powershell_ise | select name, id, cpu, handles, workingset, PrivateMemorySize, Path -first 1 $HTML += New-HTMLTable -inputObject $(ConvertTo-PropertyValue -inputObject $processDetails) #Add same PowerShell ISE details but not in property value form. Close the HTML $HTML += "<h3>PowerShell Process Details object</h3>" $HTML += New-HTMLTable -inputObject $processDetails | Close-HTML #write the HTML to a file and open it up for viewing set-content C:\test.htm $HTML & 'C:\Program Files\Internet Explorer\iexplore.exe' C:\test.htm .FUNCTIONALITY General Command #> [cmdletbinding()] param( [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromRemainingArguments=$false)] [PSObject]$InputObject, [validateset("AliasProperty", "CodeProperty", "Property", "NoteProperty", "ScriptProperty", "Properties", "PropertySet", "Method", "CodeMethod", "ScriptMethod", "Methods", "ParameterizedProperty", "MemberSet", "Event", "Dynamic", "All")] [string[]]$memberType = @( "NoteProperty", "Property", "ScriptProperty" ), [string]$leftHeader = "Property", [string]$rightHeader = "Value" ) begin{ #init array to dump all objects into $allObjects = New-Object System.Collections.ArrayList } process{ #if we're taking from pipeline and get more than one object, this will build up an array [void]$allObjects.add($inputObject) } end{ #use only the first object provided $allObjects = $allObjects[0] #Get properties. Filter by memberType. $properties = $allObjects.psobject.properties | ?{$memberType -contains $_.memberType} | select -ExpandProperty Name #loop through properties and display property value pairs foreach($property in $properties){ #Create object with property and value $temp = "" | select $leftHeader, $rightHeader $temp.$leftHeader = $property.replace('"',"") $temp.$rightHeader = try { $allObjects | select -ExpandProperty $temp.$leftHeader -erroraction SilentlyContinue } catch { $null } $temp } } } |