Randomize script start time

There’s a script posted here by Tim that demonstrates a random start time. Looking that over might help you out.

1 Like

Hey, that’s me! And I have a ticket in right now about the API limit killing me with certain sites.

Here’s the problem with my script, and I’m awaiting answers to confirm. I believe the API calls are made when the script is executed, so even though my script has rand delay, if I’m PULLING platform variables, all the devices are pulling at the same time…

In addition, if I’m pulling more than one variable in a script, is each variable pull an individual API call?

The only negative I’ve heard repeated about Syncro that it doesn’t scale. I’ve disagreed in every way, EXCEPT the API call limit. It’s causing me to have to decide important tasks based on Syncro’s limitations, not security or functionality.

That’s a problem.

3 Likes

I have finished the random start script and will post a sanitized version here. The downside is I implemented it using a Windows Scheduled Task and am unable to update any Syncro values when the scheduled script actually runs. It does work well though.

1 Like

This powershell script schedules a random time over the course of a week to build and upload the Veeam ISO recovery media to S3. While the necessity of having a lot of Veeam ISOs is questionable, the template of this script should be usable for other long running processes that need run sometime during off hours.

The downside is there is no confirmation the script ran unless you want to drop a log file and have another script poll for that log to appear. There is an entry created in the Syncro asset custom field backup_iso with the expected scheduled time to run. I think adding something to log the actual run of the script and pull that back into Syncro is probably going to be in the next revision.

## Schedule a random time over a week to run the Veeam ISO recovery media creation and upload it to S3
## 4 Syncro platform variables & 1 Custom variable (asset_id, asset_name, customer_name_label, customer_id & backup_iso)

Import-Module $env:SyncroModule
## Packages required to upload to S3
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
Install-Module -Name AWSPowerShell -Force

## Get Veeam version
$file = Get-Item -Path "C:\Program Files\Veeam\Endpoint Backup\Veeam.EndPoint.Core.dll"
$version = $file.VersionInfo.ProductVersion

## Build filenames
$filename =  "VeeamRecovery_" + $customer_id + "_" + $asset_name + "_" + $version + ".iso"
## Local filesystem filename
$fullfilename = $env:temp + "\" + $filename
## S3 bucket folder filename
$keyfilename = "clients/" + $customer_name_label + "/" + $filename 

##===============================================================================
## Script to run at the scheduled time
$ScheduledScript = @' 
## Pass in values above this line
$args = "/createrecoverymediaiso /f:" + $fullfilename

## S3 Credentials - Write Only creds with no read access via wasabi policy
$AccessKey = "XXX"
$SecretKey = "YYY"
$Bucket = "bucket"
$Endpoint = "https://s3.us-east-2.wasabisys.com"
$Region = "us-east-2"

## Create the ISO file
Start-Process -Wait -WorkingDirectory $env:temp -FilePath "C:\Program Files\Veeam\Endpoint Backup\Veeam.EndPoint.Manager.exe" -ArgumentList $args

## Upload to S3 endpoint
Set-AWSCredential -AccessKey $AccessKey -SecretKey $SecretKey
Write-S3Object -BucketName $Bucket -File $fullfilename -Key $keyfilename -EndpointUrl $Endpoint -Region $Region

## Clean up ISO file and scheduled task
Start-Sleep -Seconds 1
Remove-Item $fullfilename -Force
if ($(Get-ScheduledTask -TaskName "VeeamISOUpload" -ErrorAction SilentlyContinue).TaskName -eq "VeeamISOUpload") {
    Unregister-ScheduledTask -TaskName "VeeamISOUpload" -Confirm:$False
}
'@
## End of script to run at the scheduled time
##===============================================================================


## Write this script to a file
$temp=[System.Environment]::GetEnvironmentVariable('TEMP','Machine')
$scriptfilename=$temp + "\VeeamISOUpload.ps1"
Remove-Item $scriptfilename
## Add variables (pass variables into scheduled script by placing them at the top of the PS1 file)
Add-Content -Path $scriptfilename -Value "`$fullfilename='$($fullfilename)'"
Add-Content -Path $scriptfilename -Value "`$keyfilename='$($keyfilename)'"
## Add the rest of the script
Add-Content -Path $scriptfilename -Value $ScheduledScript

## Set Maintenance Window
$mwhourstart=17
$mwhourslength=13
$allweekend="Yes"
$onlyweekend="No"

## Randomize start time in maintenance window
if ($onlyweekend -eq "Yes") { $randomdays = 2 } else { $randomdays = 7 }
$daysofweek = 'Saturday','Sunday','Monday','Tuesday','Wednesday','Thursday','Friday'
$randomday=$daysofweek[(Get-Random -Maximum $randomdays)] # Random day of week by name 
if (($allweekend -eq "Yes") -and ($randomday -eq "Saturday" -or $randomday -eq "Sunday")) { $mwhourstart=0; $mwhourslength=23 }
$randomhour=(Get-Random -Minimum $mwhourstart -Maximum ($mwhourstart+$mwhourslength))# Random hours between maintenance window start and start + length unless weekends
if ($randomhour -gt 23) { $randomhour -= 23 }   # Over 23:00 adjust to next day
$randomhour = $randomhour.toString('00')
$randommin=((Get-Random -Maximum 3) * 15).toString('00')        # Start on 15 minute intervals
$starttime="$($randomhour):$($randommin)"
Write-Host "$starttime $randomday" 

## Temp now+ (Used for test running the script a few minutes after starting in Syncro)
#$ScheduledTime=(get-date).AddHours(Get-Random -Maximum 5).ToString("HH:mm")
#Write-Host $ScheduledTime

## Schedule the script to run via Scheduled Task
$action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument $scriptfilename
$trigger =  New-ScheduledTaskTrigger -Weekly -DaysOfWeek $randomday -At $starttime 
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "VeeamISOUpload" -Description "Veeam Recovery ISO image upload" -RunLevel "Limited" -Force -User "System"

## Log in Syncro and put a note in a custom field for when it should run
Log-Activity -Message "Veeam recovery media scheduled to upload $($randomday) at $($starttime)" -EventName "Veeam recovery media"
Set-Asset-Field -Name "Backup ISO" -Value "Scheduled:$($randomday) at $($starttime) v$($version) $($keyfilename)"
1 Like

Just wanting to get a confirmation regarding the api call limit.

I have ~20 custom fields that I use to generate reports via powershell that I run once per month across ~400 assets. I want to confirm that pulling those custom fields in from syncro via the platform script variables does not count against the api call limit.

Thanks!