khalil1974
Enthusiast
Enthusiast

Scheduled Snapshot needed at same time in different countries

Jump to solution

HI

I was hoping someone could help me, i have a working script using the this link (Schedule Snapshot with PowerCli ) to take scheduled snapshot at a time(say Tuesday 11:30PM). I run the script from the UK and it work it set the time for tue at 11:30pm, when i runt he same script from the US it is -6 hours.  I think(i might be wrong) it is because our Vcentre in UK based and it is on UTC time

We need the script to set the vm time to tue 11:30pm even if we run this in UK, US, India, Australia. can anyone help me

0 Kudos
1 Solution

Accepted Solutions
khalil1974
Enthusiast
Enthusiast

thank you so much, sorry if i did not make it clear but we need to specify the date also as there was be various dates, can i use to schedule the snapshot for today at 11:30pm

$snapTime = (Get-Date '20/07/20 23:30:00').ToUniversalTime()

View solution in original post

0 Kudos
8 Replies
LucD
Leadership
Leadership

Just to make sure I understand the question, you want the snapshot to run at 11:30 local time on each of your vCenters (which are in different timezones)?

The time you specify for the scheduled task is interpreted as UTC time (always).

So to use 11:30 local time, you will have to amend for the timezone difference.

For example, Eastern Standard Time is -5 UTC.

So the UTC time would need to be 16:30 for a vCenter in that EST timezone.

You can use PowerShell to calculate those times.

# Start with 11:30 (assume in UTC)

$target = Get-Date '11:30:00'


# Get the target timezone

$cstzone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Eastern Standard Time")

# Get the UTC time for 11:30 in the target timezone

$cstTime = $target.AddHours(-$cstzone.BaseUtcOffset.TotalHours)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
khalil1974
Enthusiast
Enthusiast

Hi


Thank you for you reply, you are spot on i do need to the snapshot to run at 11:30pm. Just to summarise

- We have one VCentre in UK

- We have site in US, India, Australia, UK

- We have over 1500 vm and Admin will run task from their own PC (in US, India, Australia) to setup the snapshot for 11:30pm on tues as example

Based on you reply i would need a script for each site that will like this(is there way to have one script that will do this?).

I would need to change the time zone ...."Eastern Standard Time". on each script

Script would look like this

$vmName = 'MyVM'

$snapTime = Get-Date "31/10/16 23:00"

# Get the target timezone

$cstzone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Eastern Standard Time")

# Get the UTC time for 11:30 in the target timezone

$cstTime = $snaptime.AddHours(-$cstzone.BaseUtcOffset.TotalHours)

$snapName = 'Test'

$snapDescription = 'Scheduled snapshot'

$snapMemory = $false

$snapQuiesce = $true

$emailAddr = 'lucd@lucd.info'


$vm = Get-VM -Name $vmName

$si = get-view ServiceInstance

$scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager

$spec = New-Object VMware.Vim.ScheduledTaskSpec

$spec.Name = "Snapshot",$_.VMname -join ' '

$spec.Description = "Take a snapshot of $($vm.Name)"

$spec.Enabled = $true

$spec.Notification = $emailAddr

$spec.Scheduler = New-Object VMware.Vim.OnceTaskScheduler

$spec.Scheduler.runat = $snapTime

$spec.Action = New-Object VMware.Vim.MethodAction

$spec.Action.Name = "CreateSnapshot_Task"

@($snapName,$snapDescription,$snapMemory,$snapQuiesce) | %{

    $arg = New-Object VMware.Vim.MethodActionArgument

    $arg.Value = $_

    $spec.Action.Argument += $arg

}

$scheduledTaskManager.CreateObjectScheduledTask($vm.ExtensionData.MoRef, $spec)

0 Kudos
LucD
Leadership
Leadership

When the script is run in different timezones, you could use the following to get the local time 11:30 in UTC.
That way you only need 1 script.

(Get-Date '11:30:00').ToUniversalTime()


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
khalil1974
Enthusiast
Enthusiast

Thank you for you reply, just to confirm i need to add the time after the get date (as we specify the date and time within the script

$vmName = 'MyVM'

$snapTime = Get-Date "31/10/16 23:00"

(Get-Date '11:30:00').ToUniversalTime()

$snapName = 'Test'

$snapDescription = 'Scheduled snapshot'

$snapMemory = $false

$snapQuiesce = $true

$emailAddr = 'lucd@lucd.info'

$vm = Get-VM -Name $vmName

$si = get-view ServiceInstance

$scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager

$spec = New-Object VMware.Vim.ScheduledTaskSpec

$spec.Name = "Snapshot",$_.VMname -join ' '

$spec.Description = "Take a snapshot of $($vm.Name)"

$spec.Enabled = $true

$spec.Notification = $emailAddr

$spec.Scheduler = New-Object VMware.Vim.OnceTaskScheduler

$spec.Scheduler.runat = $snapTime

$spec.Action = New-Object VMware.Vim.MethodAction

$spec.Action.Name = "CreateSnapshot_Task"

@($snapName,$snapDescription,$snapMemory,$snapQuiesce) | %{

    $arg = New-Object VMware.Vim.MethodActionArgument

    $arg.Value = $_

    $spec.Action.Argument += $arg

}

$scheduledTaskManager.CreateObjectScheduledTask($vm.ExtensionData.MoRef, $spec)

0 Kudos
LucD
Leadership
Leadership

Replace these 2 lines

$snapTime = Get-Date "31/10/16 23:00"

(Get-Date '11:30:00').ToUniversalTime()

with this line

$snapTime = (Get-Date '11:30:00').ToUniversalTime()


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
khalil1974
Enthusiast
Enthusiast

thank you so much, sorry if i did not make it clear but we need to specify the date also as there was be various dates, can i use to schedule the snapshot for today at 11:30pm

$snapTime = (Get-Date '20/07/20 23:30:00').ToUniversalTime()

View solution in original post

0 Kudos
LucD
Leadership
Leadership

Yes, that is correct


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
khalil1974
Enthusiast
Enthusiast

Thank you sir, all working

0 Kudos