forked from scriptrunner/ActionPacks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-VMHVHistoryTasks.ps1
More file actions
131 lines (111 loc) · 4.76 KB
/
Copy pathGet-VMHVHistoryTasks.ps1
File metadata and controls
131 lines (111 loc) · 4.76 KB
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
131
#Requires -Version 4.0
# Requires -Modules VMware.PowerCLI
<#
.SYNOPSIS
Retrieves information about the history tasks
.DESCRIPTION
.NOTES
This PowerShell script was developed and optimized for ScriptRunner. The use of the scripts requires ScriptRunner.
The customer or user is authorized to copy the script from the repository and use them in ScriptRunner.
The terms of use for ScriptRunner do not apply to this script. In particular, AppSphere AG assumes no liability for the function,
the use and the consequences of the use of this freely available script.
PowerShell is a product of Microsoft Corporation. ScriptRunner is a product of AppSphere AG.
© AppSphere AG
.COMPONENT
Requires Module VMware.PowerCLI
.LINK
https://github.com/scriptrunner/ActionPacks/tree/master/VMware/Tasks
.Parameter VIServer
Specifies the IP address or the DNS name of the vSphere server to which you want to connect
.Parameter VICredential
Specifies a PSCredential object that contains credentials for authenticating with the server
.Parameter MonthsAgo
Specifies how many months back
.Parameter DaysAgo
Specifies how many days back
.Parameter HoursAgo
Specifies how many hours back
.Parameter MinutesAgo
Specifies how many minutes back
.Parameter MaxResult
Specifies the maximum number of tasks are retrieved
.Parameter Properties
List of properties to expand, comma separated e.g. Description,Name. Use * for all properties
#>
[CmdLetBinding()]
Param(
[Parameter(Mandatory = $true,ParameterSetName = "Months ago")]
[Parameter(Mandatory = $true,ParameterSetName = "Days ago")]
[Parameter(Mandatory = $true,ParameterSetName = "Hours ago")]
[Parameter(Mandatory = $true,ParameterSetName = "Minutes ago")]
[string]$VIServer,
[Parameter(Mandatory = $true,ParameterSetName = "Months ago")]
[Parameter(Mandatory = $true,ParameterSetName = "Days ago")]
[Parameter(Mandatory = $true,ParameterSetName = "Hours ago")]
[Parameter(Mandatory = $true,ParameterSetName = "Minutes ago")]
[pscredential]$VICredential,
[Parameter(Mandatory = $true,ParameterSetName = "Months ago")]
[int32]$MonthsAgo,
[Parameter(Mandatory = $true,ParameterSetName = "Days ago")]
[int32]$DaysAgo,
[Parameter(Mandatory = $true,ParameterSetName = "Hours ago")]
[int32]$HoursAgo,
[Parameter(Mandatory = $true,ParameterSetName = "Minutes ago")]
[int32]$MinutesAgo,
[Parameter(ParameterSetName = "Months ago")]
[Parameter(ParameterSetName = "Days ago")]
[Parameter(ParameterSetName = "Hours ago")]
[Parameter(ParameterSetName = "Minutes ago")]
[ValidateRange(1,100)]
[int32]$MaxResult = 20,
[Parameter(ParameterSetName = "Months ago")]
[Parameter(ParameterSetName = "Days ago")]
[Parameter(ParameterSetName = "Hours ago")]
[Parameter(ParameterSetName = "Minutes ago")]
[string]$Properties = "EntityName,Description,State,Cancelled,StartTime,QueueTime,CompleteTime,Name,DescriptionId"
)
Import-Module VMware.PowerCLI
try{
if([System.String]::IsNullOrWhiteSpace($Properties) -eq $true){
$Properties = "*"
}
$Script:vmServer = Connect-VIServer -Server $VIServer -Credential $VICredential -ErrorAction Stop
$sView = Get-View ServiceInstance -Server $Script:vmServer
$taskMgr = Get-View $sView.Content.TaskManager -Server $Script:vmServer
$Script:tFilter = New-Object VMware.Vim.TaskFilterSpec
$Script:tFilter.Time = New-Object VMware.Vim.TaskFilterSpecByTime
$Script:tFilter.Time.timeType = [vmware.vim.taskfilterspectimeoption]::startedTime
if($PSCmdlet.ParameterSetName -eq "Months ago"){
$Script:tFilter.Time.beginTime = (Get-Date).AddMonths(-$MonthsAgo)
}
elseif($PSCmdlet.ParameterSetName -eq "Days ago"){
$Script:tFilter.Time.beginTime = (Get-Date).AddDays(-$DaysAgo)
}
elseif($PSCmdlet.ParameterSetName -eq "Hours ago"){
$Script:tFilter.Time.beginTime = (Get-Date).AddHours(-$HoursAgo)
}
elseif($PSCmdlet.ParameterSetName -eq "Minutes ago"){
$Script:tFilter.Time.beginTime = (Get-Date).AddMinutes(-$MinutesAgo)
}
$Script:tCollector = Get-View ($taskMgr.CreateCollectorForTasks($Script:tFilter))
$null = $Script:tCollector.RewindCollector
$Script:Output = $Script:tCollector.ReadNextTasks($MaxResult) | Select-Object $Properties.Split(",") | `
Sort-Object -Property StartTime -Descending
if($SRXEnv) {
$SRXEnv.ResultMessage = $Script:Output
}
else{
Write-Output $Script:Output
}
}
catch{
throw
}
finally{
if($null -ne $Script:tCollector){
$Script:tCollector.DestroyCollector()
}
if($null -ne $Script:vmServer){
Disconnect-VIServer -Server $Script:vmServer -Force -Confirm:$false
}
}