-
Notifications
You must be signed in to change notification settings - Fork 1
/
sample5.ps1
139 lines (111 loc) · 3.24 KB
/
sample5.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
Return "Haven't you figured out this is a walkthrough by now?!"
Function Get-EventLogData5 {
[cmdletbinding()]
Param(
[Alias("eventlog")]
[string]$LogName,
[alias("type")]
[string[]]$EntryType="Error",
[Alias("Cutoff")]
[Datetime]$After,
[string]$Computername = $env:COMPUTERNAME
)
$list = Get-eventlog -list -ComputerName $Computername
if ($list.logdisplayname -notcontains $Logname) {
Write-warning "Oops: $Logname doesn't look like a valid log."
#bail out
Return
}
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)
}
Try {
get-Eventlog @psboundparameters
}
Catch {
Throw $_
}
}
Get-EventLogData5 -LogName foo
Get-EventLogData5 -LogName system -EntryType errror
Function Get-EventLogData6 {
[cmdletbinding()]
Param(
[Parameter(Position=0,Mandatory,HelpMessage="Enter the name of an event log")]
[Alias("eventlog")]
[ValidateSet("System","Application","Security","Windows PowerShell")]
[string]$LogName,
[alias("type")]
[ValidateSet("Error","Warning","Information","FailureAudit","SuccessAudit")]
[string[]]$EntryType="Error",
[Alias("Cutoff")]
[ValidateScript({$_ -le (Get-Date)})]
[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)
}
Try {
get-Eventlog @psboundparameters
}
Catch {
Throw $_
}
}
Get-eventlogdata6 foo
Get-EventLogData6 -LogName Application -EntryType Error,Warning -After (Get-Date).AddDays(-3)
#handle your own validation failures
#you can have multiple validations
Function Get-EventLogData7 {
[cmdletbinding()]
Param(
[Parameter(Position=0,Mandatory,HelpMessage="Enter the name of an event log")]
[Alias("eventlog")]
[ValidateSet("System","Application","Security","Windows PowerShell")]
[ValidateScript({
$list = "System","Application","Security","Windows PowerShell"
if ($list -contains $_) {
$True
}
else {
Throw "You entered an incorrect log name. Valid entries are $($list -join ',')"
}
})]
[string]$LogName,
[alias("type")]
[ValidateSet("Error","Warning","Information","FailureAudit","SuccessAudit")]
[string[]]$EntryType="Error",
[Alias("Cutoff")]
[ValidateScript({$_ -le (Get-Date)})]
[Datetime]$After,
[Alias("cn")]
[ValidateNotNullorEmpty()]
[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)
}
Try {
get-Eventlog @psboundparameters
}
Catch {
Throw $_
}
}
Get-EventLogData7 -LogName foo