-
Notifications
You must be signed in to change notification settings - Fork 6
/
Import-AutomationRunbook.ps1
124 lines (112 loc) · 4.52 KB
/
Import-AutomationRunbook.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
function Import-AutomationRunbook {
<#
.SYNOPSIS
Import an Automationrunbook from a blobstorage for PowerShell 7.XX
.DESCRIPTION
Import an Automationrunbook from a blobstorage for PowerShell 7.XX
.PARAMETER RunbookName
Name of the runbook to import in the storage blob & automation account
.PARAMETER RuntimeVersionRunbook
Either 7.1 or 7.2
.PARAMETER ResourceGroup
Resource Group where the Automation Account is located
.PARAMETER AutomationAccountName
Automation Account to upload to
.PARAMETER StorageAccountName
Storage Account where the runbook is located
.PARAMETER StorageContainer
Storage Container where the runbook is located
.PARAMETER SASToken
SAS Token for the Storage Account (Container)
Microsoft documentation:
https://learn.microsoft.com/en-us/azure/ai-services/translator/document-translation/how-to-guides/create-sas-tokens?tabs=Containers
Unfortunately we must have a SAS token as there is no other way to authenticate to the blobstorage via Invoke-AzRestMethod.
.EXAMPLE
Import-AutomationRunbook -RunbookName 'MyRunbook' -RuntimeVersionRunbook '7.2' -AutomationAccountName 'MyAutomationAccount' -StorageAccountName 'MyStorageAccount' -StorageContainer 'MyStorageContainer' -SASToken '?sv=2020-08-04&ss=bfqt&srt=sco&sp=rwdlacupitfx&se=2021-10-31T00:00:00Z&st=2021-10-01T00:00:00Z&spr=https&sig=MySignature'
.NOTES
Author: Bas Wijdenes
Site: https://baswijdenes.com
#>
[CmdletBinding()]
param (
[parameter(mandatory = $true)]
[string]$RunbookName,
[parameter(mandatory = $true)]
[validateset('7.1', '7.2')]
[string]$RuntimeVersionRunbook,
[parameter(mandatory = $true)]
[string]$ResourceGroup,
[parameter(mandatory = $true)]
[string]$AutomationAccountName,
[parameter(mandatory = $true)]
[string]$StorageAccountName,
[parameter(mandatory = $true)]
[string]$StorageContainer,
[parameter(mandatory = $true)]
[string]$SASToken
)
begin {
switch ($RuntimeVersionRunbook) {
'7.1' {
$ApiVersion = '2019-06-01'
}
'7.2' {
$ApiVersion = '2022-06-30-preview'
}
}
if ($Runbookname -like '*.ps1') {
$RunbookName = $RunbookName -replace '.ps1', ''
}
Write-Verbose "Importing runbook $($RunbookName) in $($AutomationAccountName)"
$DirectBlobUri = "https://$($StorageAccountName).blob.core.windows.net/$($StorageContainer)/{0}$SASToken" -f "$($RunbookName).ps1"
if ($RuntimeVersionRunbook -eq '7.1') {
$JsonPayLoad = @{
Properties = @{
RunbookType = 'PowerShell7'
publishContentLink = @{
uri = $DirectBlobUri
}
state = 'Published'
}
location = 'westeurope'
} | ConvertTo-Json
}
elseif ($RuntimeVersionRunbook -eq '7.2') {
$JsonPayLoad = @{
Properties = @{
RunbookType = 'PowerShell'
runtime = 'PowerShell-7.2'
publishContentLink = @{
uri = $DirectBlobUri
}
state = 'Published'
}
location = 'westeurope'
} | ConvertTo-Json
}
}
process {
$InvokeAzRestMethodSplat = @{
Method = 'PUT'
ResourceGroupName = $($ResourceGroup)
ResourceProviderName = 'Microsoft.Automation'
ResourceType = 'automationAccounts'
Name = "$($AutomationAccountName)/runbooks/$($RunbookName)"
ApiVersion = $ApiVersion
PayLoad = $JsonPayLoad
}
try {
$InvokeAzRestMethodSplat.Method = 'PUT'
$Invoke = Invoke-AzRestMethod @InvokeAzRestMethodSplat
}
catch {
throw "Failed to create runbook $($RunbookName): $($_.Exception.Message)"
}
if (-not(($Invoke.StatusCode -eq 200) -or ($Invoke.StatusCode -eq 201))) {
throw "Failed to create runbook $($RunbookName) (Failed by StatusCode: $($Invoke.StatusCode)) Content: $($Invoke.Content)"
}
}
end {
return "Uploaded $RunbookName"
}
}