Get-RunbookTime.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
125
126
127
128
129
130

<#PSScriptInfo
 
.VERSION 1.0
 
.GUID af8eddfb-10b4-4cc8-ae3d-fad0a52bd26f
 
.AUTHOR Azure Automation Team
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>


<#
 
.SYNOPSIS
    Finds the amount in seconds that each published runbook ran for over a period of time. This runbook must be run from the
    Azure Automation Service.
 
.DESCRIPTION
    Finds the amount in seconds that each published runbook ran over a period of time. It returns a hashtable of the runbook name
    and job time in seconds.
    This runbook must be run from the Azure Automation Service.
 
 
.PARAMETER RunbookName
    Optional. The name of the runbook to calculate the time ran. If not specified, all runbooks are calculated in the automation account.
 
.PARAMETER StartTime
    Optional. The start time on when to calculate the time ran from. If not specified, jobs over the last 90 days are used.
    
.EXAMPLE
   .\Get-RunbookTime.ps1 -RunbookName Finance
 
.EXAMPLE
    .\Get-RunbookTime.ps1 -StartTime "10/10/2016"
 
.EXAMPLE
    .\Get-RunbookTime.ps1 -RunbookName Finance -StartTime "10/10/2016"
 
.NOTES
    AUTHOR: Azure Automation Team
    LASTEDIT: October 11th, 2016
#>


Param(
[Parameter(Mandatory=$false)]
[String] $RunbookName,

[Parameter(Mandatory=$false)]
[DateTime] $StartTime
)

# Authenticate to Azure so we can upload the runbooks
$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 


# Find the automation account and resource group that this job is running in
$AutomationResource = Find-AzureRmResource -ResourceType Microsoft.Automation/AutomationAccounts
foreach ($Automation in $AutomationResource)
{
    $Job = Get-AzureRmAutomationJob -ResourceGroupName $Automation.ResourceGroupName -AutomationAccountName $Automation.Name -Id $PSPrivateMetadata.JobId.Guid -ErrorAction SilentlyContinue

    if (!([string]::IsNullOrEmpty($Job)))
    {
        $AutomationAccount = $Job.AutomationAccountName
        $AutomationResourceGroup = $Job.ResourceGroupName
        break;
    }
}

if ([string]::IsNullOrEmpty($RunbookName))
{
    $Runbooks = Get-AzureRmAutomationRunbook -ResourceGroupName $AutomationResourceGroup -AutomationAccountName $AutomationAccount
}
else
{
    $Runbooks = Get-AzureRmAutomationRunbook -ResourceGroupName $AutomationResourceGroup -AutomationAccountName $AutomationAccount -Name $RunbookName
}

$JobTimePerRunbook = @{}
foreach ($Runbook in $Runbooks)
{

    $TotalTime = 0
    if ([string]::IsNullOrEmpty($StartTime))
    {
        $Jobs =  Get-AzureRmAutomationJob -ResourceGroupName $AutomationResourceGroup -AutomationAccountName $AutomationAccount -RunbookName $Runbook.Name
    }
    else
    {
        $Jobs =  Get-AzureRmAutomationJob -ResourceGroupName $AutomationResourceGroup -AutomationAccountName $AutomationAccount -RunbookName $Runbook.Name -StartTime $StartTime
    }

    foreach ($Job in $Jobs)
    {
       $TotalTime = $TotalTime + ($Job.EndTime -$Job.StartTime).Seconds
    } 
    $JobTimePerRunbook.Add($Runbook.Name,$TotalTime)
}
# Return sorted list based on number of seconds used
$JobTimePerRunbook.GetEnumerator() | Sort-Object {$_.Value} -Descending