Sync-LocalGitFolderToAutomationAccount.ps1
<#PSScriptInfo
.VERSION 1.0 .GUID f8afb2ff-b757-4d17-95dd-8571166be69d .AUTHOR Azure Automation Team .COMPANYNAME Microsoft .COPYRIGHT .TAGS Azure Automation .LICENSEURI .PROJECTURI .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES #> <# .SYNOPSIS Syncs a local git folder or specific file under source control into an Azure Automation account. .DESCRIPTION Syncs a local git folder or specific file under source control into an Azure Automation account. All runbooks need to be in a single folder. This runbook needs to be run on a hybrid runbook worker where source control git folder is set up. It runs git pull -v to get the latest changes from the git repository .PARAMETER ResourceGroup Required The name of the resource group for this automation account .PARAMETER AutomationAccountName Required The name of the Automation account .PARAMETER GitPath Required The local folder or file on the hybrid runbook worker where git is set up to pull latest changes into .EXAMPLE .\Sync-LocalGitFolderToAutomationAccount.ps1 -GitPath "c:\finance\source\runbooks" -ResourceGroup Finance -AutomationAccountName FinanceAccount .NOTES AUTHOR: Azure Automation Team LASTEDIT: 2016.10.13 #> Param ( [Parameter(Mandatory=$true)] [String] $ResourceGroup, [Parameter(Mandatory=$true)] [String] $AutomationAccountName, [Parameter(Mandatory=$true)] [String] $GitPath ) # Sync local branch Push-Location if (Test-Path $GitPath -pathtype leaf) { Set-Location (Split-Path $GitPath -Parent) git pull -v } else { Set-Location $GitPath git pull -v } Pop-Location # Authenticate to Azure so we can upload the scripts $RunAsConnection = Get-AutomationConnection -Name "AzureRunAsConnection" Add-AzureRmAccount ` -ServicePrincipal ` -TenantId $RunAsConnection.TenantId ` -ApplicationId $RunAsConnection.ApplicationId ` -CertificateThumbprint $RunAsConnection.CertificateThumbprint | Write-Verbose Select-AzureRmSubscription -SubscriptionId $RunAsConnection.SubscriptionID | Write-Verbose $Files = Get-ChildItem -Path $GitPath -Filter *.ps1 foreach ($File in $Files) { Write-Output("Syncing " + $File.FullName) $AST = [System.Management.Automation.Language.Parser]::ParseFile($File.FullName, [ref]$null, [ref]$null); If ($AST.EndBlock.Extent.Text.ToLower().StartsWith("workflow")) { Write-Verbose "File is a PowerShell workflow" $Runbook = Import-AzureRmAutomationRunbook -Path $File.FullName -AutomationAccountName $AutomationAccountName -ResourceGroupName $ResourceGroup -Type PowerShellWorkflow -Force -Published } Else { Write-Verbose "File is a PowerShell script" $Runbook = Import-AzureRmAutomationRunbook -Path $File.FullName -AutomationAccountName $AutomationAccountName -ResourceGroupName $ResourceGroup -Type PowerShell -Force -Published } Write-Verbose $Runbook.State } |