-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathGet-VMHVHistoryTasks.ps1
140 lines (120 loc) · 5.34 KB
/
Get-VMHVHistoryTasks.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
131
132
133
134
135
136
137
138
139
140
#Requires -Version 5.0
# Requires -Modules VMware.VimAutomation.Core
<#
.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, ScriptRunner Software GmbH 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 ScriptRunner Software GmbH.
© ScriptRunner Software GmbH
.COMPONENT
Requires Module VMware.VimAutomation.Core
.LINK
https://github.com/scriptrunner/ActionPacks/tree/master/VMware/Tasks
.Parameter VIServer
[sr-en] IP address or the DNS name of the vSphere server to which you want to connect
[sr-de] IP Adresse oder DNS des vSphere Servers
.Parameter VICredential
[sr-en] PSCredential object that contains credentials for authenticating with the server
[sr-de] Benutzerkonto für die Ausführung
.Parameter MonthsAgo
[sr-en] How many months back
[sr-de] Monate zurück
.Parameter DaysAgo
[sr-en] How many days back
[sr-de] Tage zurück
.Parameter HoursAgo
[sr-en] How many hours back
[sr-de] Stunden zurück
.Parameter MinutesAgo
[sr-en] How many minutes back
[sr-de] Minuten zurück
.Parameter MaxResult
[sr-en] Maximum number of tasks are retrieved
[sr-de] Maximales Result
.Parameter Properties
[sr-en] List of properties to expand. Use * for all properties
[sr-de] Liste der zu anzuzeigenden Eigenschaften. Verwenden Sie * für alle Eigenschaften
#>
[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")]
[VaildateSet('*','EntityName','Description','State','Cancelled','StartTime','QueueTime','CompleteTime','Name','DescriptionId')]
[string[]]$Properties = @('EntityName','Description','State','Cancelled','StartTime','QueueTime','CompleteTime','Name','DescriptionId')
)
Import-Module VMware.VimAutomation.Core
try{
if($Properties -contains '*'){
$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
$result = $Script:tCollector.ReadNextTasks($MaxResult) | Select-Object $Properties | `
Sort-Object -Property StartTime -Descending
if($SRXEnv) {
$SRXEnv.ResultMessage = $result
}
else{
Write-Output $result
}
}
catch{
throw
}
finally{
if($null -ne $Script:tCollector){
$Script:tCollector.DestroyCollector()
}
if($null -ne $Script:vmServer){
Disconnect-VIServer -Server $Script:vmServer -Force -Confirm:$false
}
}