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!
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!
Depends on the data type of that duration property. What do you get if you pipe that to Get-Member?
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.
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.
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:
#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
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
Thank you Ivan!
I appreciate the response and detail