-
Notifications
You must be signed in to change notification settings - Fork 1
/
sample2.ps1
133 lines (102 loc) · 2.78 KB
/
sample2.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
#requires -version 5.0
Return "This is a walkthrough demo"
Function Get-EventLogData {
[cmdletbinding()]
Param(
[string]$Eventlog,
[string[]]$Type="Error",
[Datetime]$Cutoff,
[string]$Computername = $env:COMPUTERNAME
)
Write-Verbose "Getting $($entrytype -join ',') entries from $eventlog on $computername after $cutoff"
Try {
get-Eventlog -LogName $Eventlog -EntryType $Type -After $Cutoff -Computername $Computername -erroraction Stop
}
Catch {
Throw $_
}
}
Get-eventlogdata -Eventlog system -cutoff (Get-Date).AddDays(-7) -Verbose |
Select -first 3 TimeGenerated,Source,EntryType,Message
Function Get-EventLogData2 {
[cmdletbinding()]
Param(
[string]$Eventlog,
[string[]]$Type="Error",
[Datetime]$Cutoff,
[string]$Computername = $env:COMPUTERNAME
)
Write-Verbose "Getting $($entrytype -join ',') entries from $eventlog on $computername after $after"
#hash table of parameters to splat
$params = @{
LogName = $Eventlog
EntryType = $Type
After = $Cutoff
Computername = $Computername
erroraction = 'Stop'
}
Try {
get-Eventlog @params
}
Catch {
Throw $_
}
}
Get-eventlogdata2 -Eventlog system -cutoff (Get-Date).AddDays(-3) -Verbose |
Select -first 3 TimeGenerated,Source,EntryType,Message
#using PSBoundParameters
Function Get-EventLogData3 {
[cmdletbinding()]
Param(
[Alias("eventlog")]
[string]$LogName,
[alias("type")]
[string[]]$EntryType="Error",
[Alias("Cutoff")]
[Datetime]$After,
[string]$Computername = $env:COMPUTERNAME
)
Write-Verbose "Getting $($entrytype -join ',') entries from $Logname on $computername after $after"
write-verbose ($psboundparameters | out-string)
#hash table of parameters to splat
$psboundparameters.add("ErrorAction","Stop")
Try {
get-Eventlog @psboundparameters
}
Catch {
Throw $_
}
}
#this doesn't quite work - notice the entrytype
Get-eventlogdata3 -Eventlog system -cutoff (Get-Date).AddDays(-3) -Verbose |
Select -first 3 TimeGenerated,Source,EntryType,Message
Function Get-EventLogData4 {
[cmdletbinding()]
Param(
[Alias("eventlog")]
[string]$LogName,
[alias("type")]
[string[]]$EntryType="Error",
[Alias("Cutoff")]
[Datetime]$After,
[string]$Computername = $env:COMPUTERNAME
)
Write-Verbose "Getting $($entrytype -join ',') entries from $Logname on $computername after $after"
write-verbose ($psboundparameters | out-string)
#hash table of parameters to splat
$psboundparameters.add("ErrorAction","Stop")
#add the default value
if (-Not $psboundparameters.ContainsKey("EntryType")) {
$psboundparameters.add("EntryType",$EntryType)
}
#you can also remove entries from PSBoundParameters
Try {
get-Eventlog @psboundparameters
}
Catch {
Throw $_
}
}
#note that I can still use aliases
Get-eventlogdata4 -Eventlog system -cutoff (Get-Date).AddDays(-3) -Verbose |
Select -first 3 TimeGenerated,Source,EntryType,Message