ReRegisterAutomationSchedule-MS-Mgmt.ps1

<#PSScriptInfo
.VERSION 1.0
.GUID 626fa5aa-75a6-461b-997d-2a282781fe8f
.AUTHOR Azure Automation Team
.COMPANYNAME Microsoft
.COPYRIGHT
.TAGS Azure Automation
.LICENSEURI
.PROJECTURI
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
#>


<#
.SYNOPSIS
  Unlink and re-link all Azure Automation schedules with their respective runbooks
   
.DESCRIPTION
  This runbook will iterate through all Azure Automation schedules in the resource group, subscription ID, and Automation account specified
  in the parameters. For each, it will unlink and then re-link with the same parameters and runbooks associations. By doing the relinking of schedules,
  the runbook will use the latest module versions that are imported in the Azure Automation account.
 
.EXAMPLE
  .\ReRegisterAutomationSchedule-MS-Mgmt.ps1 -ResourceGroup [resourcegroupname] -TargetSubscriptionId [subscriptionID] -AutomationAccount [automationaccount]
 
.NOTES
  AUTHOR: Azure Automation Team
  LASTEDIT: 2016-10-12
#>
  

param (
    [Parameter (Mandatory= $false)]
    [string] $resourcegroup,

    [Parameter (Mandatory= $false)]
    [string] $targetSubscriptionId,

    [Parameter (Mandatory= $false)]
    [string] $automationaccount
)

$ErrorActionPreference = "Stop"

if ((-not ($targetSubscriptionId)) -or (-not ($resourcegroup)) -or (-not ($automationaccount)))
{
    return "Exiting: Input is null"
}

#
# Initialize the Azure subscription we will be working against for AzureRM resources
#
Write-Verbose "Authenticating ARM RunAs account"
$connectionName = "AzureRunAsConnection"
# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection=Get-AutomationConnection -Name $connectionName         
Write-Verbose "Logging in to Azure..."
Add-AzureRmAccount `
    -ServicePrincipal `
    -TenantId $servicePrincipalConnection.TenantId `
    -ApplicationId $servicePrincipalConnection.ApplicationId `
    -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName 
Select-AzureRMSubscription -SubscriptionId $targetSubscriptionId -TenantId $servicePrincipalConnection.TenantId
$currentSubscription = Get-AzureRMSubscription -SubscriptionId $targetSubscriptionId -TenantId $servicePrincipalConnection.TenantId
Write-Verbose "Found ARM subscription [$($currentSubscription.SubscriptionName)] ($($currentSubscription.SubscriptionId))"


$sList = Get-AzureRmAutomationScheduledRunbook -AutomationAccount $automationaccount -ResourceGroup $resourcegroup
foreach ($s in $sList)
{
    $sItem = Get-AzureRmAutomationScheduledRunbook -AutomationAccount $s.AutomationAccountName -ResourceGroup $s.ResourceGroupName -JobScheduleId $s.JobScheduleId
    
    Unregister-AzureRmAutomationScheduledRunbook -ResourceGroup $sItem.ResourceGroupName -AutomationAccount $sItem.AutomationAccountName -RunbookName $sItem.RunbookName -ScheduleName $sItem.ScheduleName -Force
    Register-AzureRmAutomationScheduledRunbook -ResourceGroup $sItem.ResourceGroupName -AutomationAccount $sItem.AutomationAccountName -RunbookName $sItem.RunbookName -ScheduleName $sItem.ScheduleName -Parameters $sItem.Parameters
    Write-Verbose "Completed re-registering schedule $($sItem.ScheduleName)"
}