Search This Blog

Thursday, January 20, 2011

Get-Objects.ps1 Recursively Retrieves Object Properties

Here's my holy grail of Powershell and Powercli scripts to recursively get objects. In the past I've used Format-Custom and Select-Object * such as this:
$object| Format-Custom
or
$object | Select *

But that doesn't work for me. I wanted to get the object properties AND the properties of the properties recursively like this:

(Get-Process textpad) | .\Get-Objects.ps1 -Depth 1

I want to get you to the script, so the output to the example above is below.

Hence, Get-Objects.ps1 below:

param($Object = $null, [byte]$Depth = 3 , $Verbose=$False)

Process {
$Description ="This Script recursively returns an array of objects showing properties of an object."
$i = 1

Function GetList ($Object, $PreviousLabel ){
if($Object ){
#Get Property List excluding object methods
$PropertyList = $Object | Get-Member -Membertype Properties
$PreviousLabel = $PreviousLabel + "[.]"

#Return/Print Label and Object
$ErrorActionPreference = "silentlycontinue"
if($Verbose) { write-host "Depth: $i of $Depth - $PreviousLabel " -fore yellow}

"Depth $i of $Depth - " + $PreviousLabel+"[.]"+$Label
$Object | select *

#Proceed if iteration less than $Depth
if (($I+1) -LE $Depth ) {
#Increment Iteration $I
$i++
if ($PropertyList ) {
#Recursively GetList for Each Properties
foreach ($Property in $PropertyList ) {
#Get Property Name
$n = $Property.name
#Get object from PropertyList.Property
$obj = $object.$n
#GetList again (recurse)
GetList -Object $obj -PreviousLabel ($PreviousLabel + $n )
}
}
}
}else{
##Show Script Syntax if
if ($i -LE 1) {
Write-Host "
SYNOPSIS:
" ($Description | Out-String -Width 30) "

PARAMETERS
-Depth Default = $Depth. Sets the recursive depth level.
-Object Any process, variable, or object that has a property.
Any object without a property will not be displayed.
-Verbose Defaults to `$FALSE. -Verbose `$True writes progress to screen

SYNTAX
" (Get-help $$) "

SYNTAX EXAMPLES:
----------------------- EXAMPLE 1 -----------------------
`$AnyVariableOrObject | $$ -Depth 2

----------------------- EXAMPLE 2 -----------------------
$$ -Depth 2 `-Object `$AnyVariableOrObject


"
}
}
}

#Recursively get object from passed objects
if ($object){
GetList -Object $Object
} else {
GetList -Object $_
}
}




Copy the script above to a text file, save as Get-Objects.ps1 and have fun!



Output Example:

Depth 1 of 1 - [.]System.Diagnostics.Process
__NounName : Process
Name : TextPad
Handles : 152
VM : 88981504
WS : 9117696
PM : 7483392
NPM : 12608
Path : C:\tools\textpad\TextPad.exe
Company : Helios Software Solutions
CPU : 6.2868403
FileVersion : 5.2.0
ProductVersion : 5.2.0
Description : TextPad
Product : TextPad
Id : 6248
PriorityClass : BelowNormal
HandleCount : 152
WorkingSet : 9117696
PagedMemorySize : 7483392
PrivateMemorySize : 7483392
VirtualMemorySize : 88981504
TotalProcessorTime : 00:00:06.2868403
BasePriority : 6
ExitCode :
HasExited : False
ExitTime :
Handle : 1956
MachineName : .
MainWindowHandle : 0
MainWindowTitle :
MainModule : System.Diagnostics.ProcessModule (TextPad.exe)
MaxWorkingSet : 1413120
MinWorkingSet : 204800
Modules : {System.Diagnostics.ProcessModule (TextPad.exe), System.Diagnostics.Pr
ocessModule (ntdll.dll), System.Diagnostics.ProcessModule (wow64.dll),
System.Diagnostics.ProcessModule (wow64win.dll)...}
NonpagedSystemMemorySize : 12608
NonpagedSystemMemorySize64 : 12608
PagedMemorySize64 : 7483392
PagedSystemMemorySize : 143672
PagedSystemMemorySize64 : 143672
PeakPagedMemorySize : 7565312
PeakPagedMemorySize64 : 7565312
PeakWorkingSet : 13725696
PeakWorkingSet64 : 13725696
PeakVirtualMemorySize : 88985600
PeakVirtualMemorySize64 : 88985600
PriorityBoostEnabled : True
PrivateMemorySize64 : 7483392
PrivilegedProcessorTime : 00:00:01.3572087
ProcessName : TextPad
ProcessorAffinity : 255
Responding : True
SessionId : 0
StartInfo : System.Diagnostics.ProcessStartInfo
StartTime : 1/14/2011 5:15:03 PM
SynchronizingObject :
Threads : {5768, 6432, 1816}
UserProcessorTime : 00:00:04.9296316
VirtualMemorySize64 : 88981504
EnableRaisingEvents : False
StandardInput :
StandardOutput :
StandardError :
WorkingSet64 : 9117696
Site :
Container :

2 comments: