Author Posts

January 12, 2016 at 7:32 am

When I run my function

 get-stuff | select-object duration 

I get an output that looks like
1 min
2 min
6 min

What I am trying to do is remove the "min" , so i can sum the numbers.

Thanks in advance fellas!

January 12, 2016 at 8:07 am

Depends on the data type of that duration property. What do you get if you pipe that to Get-Member?

January 12, 2016 at 8:13 am

Assuming that the values are always returned in minutes (and that the value is actually a string) you can try something like that:

Get-Stuff | Select @{n='DurationMinutes'; e={[convert]::ToInt32(($_.Duration -replace "^(\d+).*", '$1'))}} | Measure -Sum DurationMinutes

It is possible, however, that what you're seeing is a custom formatting of the type, in which case the approach will differ a bit.

January 12, 2016 at 10:03 am

TypeName: System.Management.Automation.PSCustomObject

Name MemberType Definition
—- ———- ———-
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Distance NoteProperty System.String Distance=1.1 mi
Duration NoteProperty System.String Duration=2 mins
Instructions NoteProperty System.String Instructions=Head north on Central Ave toward Tilt St
TravelMode NoteProperty System.String TravelMode=DRIVING

This is what I get when I pipe to get member, Ivan good point, Sometime I get a returned value of 1 hour 6 minutes for example. I will probally just edit the function to only send back minutes as the fix to that.

January 13, 2016 at 12:28 am

Well, if you can edit the Get-stuff function, I'd recommend you to have it return the value as an integer (say in seconds) instead of a string. That will make it easier for you (and whoever else may work later with it) to work with the output (like sorting, getting sum, avg, etc.) and you'll be able to convert it later on in the pipeline to human-readable string (adding days, hours, minutes, etc.). By the way, the same applies to the Distance property.

Alternatively you can make it both human and machine readable, by creating a custom format view (i.e. like Get-Process) but that will involve some additional work, like:
1. you'll need to update the Get-Stuff cmdlet to spit out objects of a custom type (an not just PSObject), i.e. like this:

#Say you have a PSCustomObject that is being returned as a result:
$Result = [pscustomobject]@{
    Distance=1.1
    Duration=120
    Instructions='Head north on Central Ave toward Tilt St'
    TravelMode='DRIVING'
}

#You can define a custom type name for your object, say 'My.GetStuffType' as follows:
$Result.psobject.TypeNames.Insert(0,'My.GetStuffType')

# Running Get-Member on $Result will produce the following (you may want to compare TypeName before and after):
# 
# 
#    TypeName: My.GetStuffType
# 
# Name         MemberType   Definition                                                  
# ----         ----------   ----------                                                  
# Equals       Method       bool Equals(System.Object obj)                              
# GetHashCode  Method       int GetHashCode()                                           
# GetType      Method       type GetType()                                              
# ToString     Method       string ToString()                                           
# Distance     NoteProperty double Distance=1,1                                         
# Duration     NoteProperty int Duration=2                                              
# Instructions NoteProperty string Instructions=Head north on Central Ave toward Tilt St
# TravelMode   NoteProperty string TravelMode=DRIVING         

2. Create a Custom Formatting File (more info can be found here) linking it to a custom type that your function returns
3.a. If your function is distributed in a script file, you can add the following to the beginning of that script to load up your custom format file:

Update-Format -AppendPath 

3.b. If it's a module, you can create a module manifest, and reference your formatting file in there:

New-ModuleManifest -Path  -RootModule  -FormatsToProcess 

Hope that helps.

Kind Regards,
Ivan

January 14, 2016 at 3:08 pm

Thank you Ivan!
I appreciate the response and detail