Substract Start Date time with End date time in Powershell.

Hello Experts

I have created below code for my use, everything is working fine except the time duration object. How do I substract the End date time with Start Date time to get the job duration?

$Jobs +=Get-DPMJob -ProtectionGroup $PG -From (Get-Date).AddDays(-$days) |? {$.JobCategory -notlike ‘Inventory’ -and $.JobCategory -notlike ‘LibraryRescan’} | Select @{l=‘JobName’;e={$.JobCategory}}, @{l=‘Status’;e={$.Status}}, @{l=‘ProtectionGroup’;e={$.ProtectionGroupName}}, @{l=‘starttime’;e={$.starttime}}, @{l=‘Endtime’;e={$.Endtime}}, @{l=‘Data Transferred(MB)’;e={"{0:N2}" -f ($.Datasize/1024/1024)}}, @{l=‘Duration’; e={"{0:N2}"; f={[DateTime]$.Endtime - [DateTime]$.Starttime}}}

Any help would be appreciated and sorry if I couldn’t put this as a code. Not sure how to use it. Thanks.

I cannot test but I’d think you have a syntax error in your last calulated property. Don’t you get errors?

Try it this way:
@{Name = 'Duration'; Expression = { "{0:N2}" -f ( [DateTime]_.Endtime - [DateTime]_.Starttime)}}

Hi,

Thanks for your response. No, I don’t get any error. The Duration Column is just empty. I also tried your suggestion but it is still coming as empty.

Thanks.

If you take the data and do it by itself, you get a formatting error. You are trying to pass a datetime object to a string formatter. I’m assuming you want seconds, so try this.

$Jobs +=Get-DPMJob -ProtectionGroup $PG -From (Get-Date).AddDays(-$days) |? {$.JobCategory -notlike ‘Inventory’ -and $.JobCategory -notlike ‘LibraryRescan’} | Select @{l=‘JobName’;e={$_.JobCategory}},

@{l=‘Status’;e={$_.Status}},

@{l=‘ProtectionGroup’;e={$_.ProtectionGroupName}},

@{l=‘starttime’;e={$_.starttime}},

@{l=‘Endtime’;e={$_.Endtime}},

@{l=‘Data Transferred(MB)’;e={"{0:N2}" -f ($_.Datasize/1024/1024)}},

@{n=‘Duration’;e={"{0:N2}" -f ([datetime]$.Endtime - [DateTime]$.Starttime).TotalSeconds}}

Hi Doug,

Thanks for pitching in yet again and it worked perfectly. So the result is in seconds right? if it want the result to come in mins then do I use: ([datetime]$.Endtime - [DateTime]$.Starttime).TotalMinutes}}

Also, I noticed that you used @{n=‘Duration’ instead L, could you please explain me the difference between these 2? Thank you so much again. :slight_smile:

 

@{n=’Duration’ instead L,

N is short vor Name and L is short for Label … works both the same and it’s recommended to use full name. :wink:

Yes I thought so but wanted to be sure. Thanks again to both of you. Is there a way I can mark your replies as answers or thumbs up for this thread?

@Maxwell, yes if you change the .totalseconds to .totalminutes. And apologies for adding confusion, Olaf explained they are the same. I never got in the habit of L so I didn’t even think about it when typing it out. For readability for yourself and others, the full names should be used in scripts. Spacing helps too. Also in this case, you can drop the type accelerator since the value is already of that type.

@{Name = 'Duration'; Expression = { "{0:N2}" -f ($.Endtime - $.Starttime).TotalSeconds }}

or

@{Label = 'Duration'; Expression = { "{0:N2}" -f ($.Endtime - $.Starttime).TotalSeconds }}

I really need that edit button back…

Thanks Doug. Really appreciate your help. I am still wondering if there is a way to mark your reply as ANS so that you get the much deserved points :slight_smile:

 

I am still wondering if there is a way to mark your reply as ANS so that you get the much deserved points

Something like that does not exist. We are glad to be of any help for you … even without any other reward than your “Thanks” :wink:

I really need that edit button back..

Fully agreed … :-}

Thanks a ton to both you… Stay safe. :slight_smile: