Skip to content

Instantly share code, notes, and snippets.

@Ba4bes
Created February 8, 2019 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ba4bes/5a2d2f89989b06ca812c71daa847cabd to your computer and use it in GitHub Desktop.
Save Ba4bes/5a2d2f89989b06ca812c71daa847cabd to your computer and use it in GitHub Desktop.
Create and link a runbookschedule
function New-AzureRmAutomationRunbookschedule {
<#
.SYNOPSIS
This function creates a onetime runbookschedule and registers it to a runbook.
.DESCRIPTION
A unique name is created with a parameter and a number.
This name is used for a new schedule.
The schedule will be registered to a runbook.
.PARAMETER AutomationAccount
The name of the Automation Account where the runbook is and the schedule will be.
.PARAMETER ScheduleName
The name for the schedule, which will be used as a prefix.
.PARAMETER RunbookName
The name of the runbook that needs to be scheduled.
.PARAMETER Date
The date and time the runbook will run.
.PARAMETER Parameters
The parameters that need to be passed to the runbook.
.EXAMPLE
$Date = (Get-Date).AddDays(5)
$Parameters = @{
Parameter = $true
}
New-RunbookSchedule -AutomationAccount "Aut01" -ScheduleName "Schedule" -Runbookname "start-script" -Datum $Date -Parameters $parameters
.NOTES
Support voor whatif.
The schedulename will be created by a loop that looks for the lowest name-number-combination not in use.
#>
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')]
Param(
[Parameter(Mandatory = $true)]
[String]$AutomationAccount,
[Parameter(Mandatory = $true)]
[String]$ScheduleName,
[Parameter(Mandatory = $true)]
[String]$RunbookName,
[Parameter(Mandatory = $true)]
[DateTime]$Date,
[Parameter(Mandatory = $false)]
[Hashtable]$Parameters
)
#Requires -modules AzureRM
# A schedulename needs to be unique in the automation account.
$i = 0
$AutomationAccountRG = (Get-AzureRmResource -Name $AutomationAccount).ResourceGroupName
do {
# Add to $i untill a schedulename is found that does not exist.
$i++
$UniqueName = "$ScheduleName $i"
# Check if the schedule exists
$ScheduleCheck = Get-AzureRmAutomationSchedule -Name $UniqueName -ResourceGroupName $AutomationAccountRG -AutomationAccountName $AutomationAccount -ErrorAction SilentlyContinue
}
while ($ScheduleCheck)
Write-verbose "New Schedulename: $UniqueName"
# Create a new schedule
Try {
if ($PSCmdlet.ShouldProcess(
("New Schedule {0} will be created for {1}" -f $UniqueName, $Date),
("Do you want to create the schedule {0} for date {1}?" -f $UniqueName, $Date),
"Create Schedule"
)
) {
New-AzureRmAutomationSchedule -Name $UniqueName -ResourceGroupName $AutomationAccountRG -AutomationAccountName $AutomationAccount -StartTime $Date -OneTime -ErrorAction Stop
Write-Output "Schedule $UniqueName has been created"
}
}
Catch {
Write-Error $_
Write-Error "Schedule $UniqueName could not be created."
return
}
# Register the schedule to the runbook
Try {
if ($PSCmdlet.ShouldProcess(
("Schedule {0} will be registered to runbook {1}" -f $UniqueName, $RunbookName),
("Do you want to register {0} tor runbook {1}?" -f $UniqueName, $RunbookName),
"Schedule koppelen")) {
Register-AzureRmAutomationScheduledRunbook -ResourceGroupName $AutomationAccountRG -AutomationAccountName $AutomationAccount -RunbookName $RunbookName -ScheduleName $UniqueName -Parameters $Parameters -ErrorAction Stop
Write-Output "Schedule $UniqueName registered to $RunbookName"
}
}
Catch {
Write-Error $_
Write-Error "Schedule $UniqueName could not be registered."
return
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment