-
Notifications
You must be signed in to change notification settings - Fork 0
/
listDeletedRunbook.ps1
108 lines (81 loc) · 3.07 KB
/
listDeletedRunbook.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
<#
.SYNOPSIS
This PowerShell script lists deleted runbooks in an Azure Automation account.
.DESCRIPTION
This PowerShell script is designed to list deleted runbooks in an Azure Automation account.
.PARAMETER subscriptionId
Required. Subscription of the Azure Automation account in which the runbook needs to be listed.
.PARAMETER resourceGroupName
Required. The name of the resource group of the Azure Automation account.
.PARAMETER automationAccountName
Required. The name of the Azure Automation account in which the runbook needs to be listed.
.NOTES
AUTHOR: Azure Automation Team
LASTEDIT: Dec 7, 2023
#>
param (
[Parameter(Mandatory = $true)]
[string]$subscriptionId,
[Parameter(Mandatory = $true)]
[string]$resourceGroupName,
[Parameter(Mandatory = $true)]
[string]$automationAccountName
)
# Function to log in to Azure
function Login-AzAccount {
try
{
# This script requires system identity enabled for the automation account with 'Automation Contributor' role assignment on the identity.
"Logging in to Azure..."
Connect-AzAccount -Identity
}
catch {
Write-Error -Message $_.Exception
throw $_.Exception
}
}
# Base URL of the listDeletedRunbook API
$apiUrl = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Automation/automationAccounts/$automationAccountName/listDeletedRunbooks?api-version=2023-05-15-preview"
# Function to retrieve all deleted runbooks from the API
function Get-AllDeletedRunbooks {
$allRunbooks = @()
do {
try {
# Call the API
$response = Invoke-RestMethod -Uri $apiUrl -Method Post -Headers $Headers -ErrorAction Stop
# Add the current set of runbooks to the collection
$allRunbooks += $response.value
# Check for the presence of NextLink
if ($response.nextLink) {
$apiUrl = $response.nextLink
} else {
$apiUrl = $null
}
} catch {
Write-Error "Failed to retrieve deleted runbooks. Response: $_"
break
}
} while ($apiUrl)
return $allRunbooks
}
# Main script
# Login to Azure
Login-AzAccount
# Get the user token
$userToken = (Get-AzAccessToken).Token
$Headers = @{
"Content-Type" = "application/json"
"Authorization" = "Bearer $($userToken)"
}
# Retrieve all runbooks
$allDeletedRunbooks = Get-AllDeletedRunbooks
Write-Output "Below are deleted runbook names under automation account $automationAccountName"
# Loop through each runbook, get the location and run the restore function
foreach ($runbookName in $allDeletedRunbooks) {
if ($allDeletedRunbooks.Count -gt 0) {
$runbook = $runbookName.name
Write-Output $runbook
} else {
Write-Error "Cannot find any runbook in the deleted runbooks. Runbooks deleted within 30 days can be restored only."
}
}