Skip to content

Instantly share code, notes, and snippets.

@srpomeroy
Created February 24, 2017 04:16
Show Gist options
  • Save srpomeroy/2bf03ad0dc7d6f4dc061c24c1d2ad242 to your computer and use it in GitHub Desktop.
Save srpomeroy/2bf03ad0dc7d6f4dc061c24c1d2ad242 to your computer and use it in GitHub Desktop.
RightScale/Rightlink10/RSC Use PowerShell Job to schedule a snapshot by running a script in RightScale via RSC.
$errorActionPreference = "stop"
$scriptId = "12345678"
$rsc = 'C:\Program Files\RightScale\RightLink\rsc.exe'
$snapshot_Name = $ENV:SNAPSHOT_NAME
$snapshot_Save = $ENV:KEEP_SNAPSHOTS
$scheduledTask_Time =$ENV:SCHEDULED_TASK_TIME
$scheduledTask_Name = "RightScale Snapshot - $ENV:SNAPSHOT_NAME"
if(($ENV:ENABLE_SNAPSHOT -eq $true) -and (!($ENV:KEEP_SNAPSHOTS) -or !($ENV:SCHEDULED_TASK_TIME))) {
Write-Output "Error! Must provide value for KEEP_SNAPSHOTS and SCHEDULED_TASK_TIME!"
EXIT 1
}
$scriptBlock = @"
if(!(Test-Path -Path "$rsc")) {
Write-Output "Error! RSC.exe not found!"
EXIT 1
}
else {
Write-Output "Running RightScript"
& "$rsc" rl10 run_right_script /rll/run/right_script "right_script_id=$scriptId" "arguments=KEEP_SNAPSHOTS=text:$snapshot_Save" "arguments=SNAPSHOT_NAME=text:$snapshot_Name"
}
"@
$trigger = @{"Daily"=$true; "At" = $scheduledTask_Time}
$scheduledJob = @{"Name" = $scheduledTask_Name; "ScriptBlock" = ([scriptblock]::Create($scriptBlock))}
$currentJob = Get-ScheduledJob -Name $scheduledTask_Name -ErrorAction SilentlyContinue
if(($ENV:ENABLE_SNAPSHOT -eq $false) -and !($currentJob)) {
#Nothing to do, quit
Write-Output "Snapshot set to 'false' and job does not exist."
}
elseif(($ENV:ENABLE_SNAPSHOT -eq $false) -and $currentJob){
#Delete the job
Write-Output "Deleting job..."
$currentJob | Remove-Job
}
elseif(($ENV:ENABLE_SNAPSHOT -eq $true) -and !($currentJob)) {
#Create the job
Write-Output "Creating Scheduled Job '$scheduledTask_Name'..."
$scheduledTask_Trigger = New-JobTrigger @trigger
Register-ScheduledJob @scheduledJob -Trigger $scheduledTask_Trigger
}
elseif(($ENV:ENABLE_SNAPSHOT -eq $true) -and $currentJob){
#Job already exists, make sure it's correct
Write-Output "Updating Scheduled Job..."
$currentJob | Get-JobTrigger | Set-JobTrigger @trigger
$currentJob | Set-ScheduledJob @scheduledJob
}
Write-Output "Done configuring snapshots!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment