PowerShell script exchange
Post Reply
ldasilva
Novice
Posts: 5
Liked: never
Joined: Apr 25, 2017 5:43 pm
Full Name: Lisandro da Silva
Contact:

Get time in hours from schedule

Post by ldasilva »

Hi,

How can i retrieve only the time of the schedule and not the complete date and time?

Example

$jobOptions | Add-Member -MemberType NoteProperty -Name "Time" -value $job.ScheduleOptions.get_NextRun()

This returns the date and time.

and when i try this one

$jobOptions | Add-Member -MemberType NoteProperty -Name "Time" -value $Job.ScheduleOptions.OptionsDaily.kind

nothing happens

Please help
veremin
Product Manager
Posts: 20284
Liked: 2258 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Get time in hours from schedule

Post by veremin »

Try this script and see whether it answers your requirements:

Code: Select all

Asnp VeeamPSSnapin
$BackupJob = Get-VBRJob -Name "Name of your Job"
$Date = Get-Date $BackupJob.ScheduleOptions.NextRun
$Date.ToShortTimeString()
Thanks.
ldasilva
Novice
Posts: 5
Liked: never
Joined: Apr 25, 2017 5:43 pm
Full Name: Lisandro da Silva
Contact:

Re: Get time in hours from schedule

Post by ldasilva »

Hi Eremin,

Thank you for your reply.

When i added this to my script, it does populate the time, however i do get an error msg

Code: Select all

"Get-Date : Cannot bind parameter 'Date'. Cannot convert value "" to type "System.DateTime". Error: "String was not reco
gnized as a valid DateTime."
At C:\tmp\collection.ps1:20 char:33
+                 $time = get-date <<<<  $job.ScheduleOptions.get_NextRun()
    + CategoryInfo          : InvalidArgument: (:) [Get-Date], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetDateCommand"

This is what i added in my script

Code: Select all

$time = get-date $job.ScheduleOptions.get_NextRun()

$jobOptions | Add-Member -MemberType NoteProperty -Name "Time" -value $time.ToShortTimeString()
kind regards,

Lisandro
veremin
Product Manager
Posts: 20284
Liked: 2258 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Get time in hours from schedule

Post by veremin »

There is no such method as get_NextRun(). So, the result of $job.ScheduleOptions.get_NextRun() line is $Null which cannot be converted to DateTime format. Kindly, leverage the code I suggested. Thanks.
SRedlin
Influencer
Posts: 10
Liked: 1 time
Joined: Sep 23, 2016 10:20 am
Full Name: Stefan Redlin
Contact:

Re: Get time in hours from schedule

Post by SRedlin »

This lines of code display the output wanted.

Code: Select all

$time = get-date $BackupJob.ScheduleOptions.OptionsDaily.TimeLocal 
$time.ToShortTimeString()
veremin
Product Manager
Posts: 20284
Liked: 2258 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Get time in hours from schedule

Post by veremin »

The problem I can see with the last example is the fact that it works only for jobs with daily type of schedule, and it doesn't take backup window settings into account. So, keep this in mind, if you stick to this script. Thanks.
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 17 guests