Debian Codegraph @

ansible_collections/community/windows/plugins/modules/win_eventlog_entry.ps1

raw
1 #!powershell
2
3 # Copyright: (c) 2017, Andrew Saraceni <[email protected]>
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 #Requires -Module Ansible.ModuleUtils.Legacy
7
8 $ErrorActionPreference = "Stop"
9
10 function Test-LogExistence {
11 <#
12 .SYNOPSIS
13 Get information on a log's existence.
14 #>
15 param(
16 [String]$LogName
17 )
18
19 $log_exists = $false
20 $log = Get-EventLog -List | Where-Object { $_.Log -eq $LogName }
21 if ($log) {
22 $log_exists = $true
23 }
24 return $log_exists
25 }
26
27 function Test-SourceExistence {
28 <#
29 .SYNOPSIS
30 Get information on a source's existence.
31 #>
32 param(
33 [String]$LogName,
34 [String]$SourceName
35 )
36
37 $source_exists = [System.Diagnostics.EventLog]::SourceExists($SourceName)
38
39 if ($source_exists) {
40 $source_log = [System.Diagnostics.EventLog]::LogNameFromSourceName($SourceName, ".")
41 if ($source_log -ne $LogName) {
42 Fail-Json -obj $result -message "Source $SourceName does not belong to log $LogName and cannot be written to"
43 }
44 }
45
46 return $source_exists
47 }
48
49 $params = Parse-Args $args -supports_check_mode $true
50 $check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false
51
52 $log = Get-AnsibleParam -obj $params -name "log" -type "str" -failifempty $true
53 $source = Get-AnsibleParam -obj $params -name "source" -type "str" -failifempty $true
54 $event_id = Get-AnsibleParam -obj $params -name "event_id" -type "int" -failifempty $true
55 $message = Get-AnsibleParam -obj $params -name "message" -type "str" -failifempty $true
56 $entry_type = Get-AnsibleParam -obj $params -name "entry_type" -type "str" -validateset "Error", "FailureAudit", "Information", "SuccessAudit", "Warning"
57 $category = Get-AnsibleParam -obj $params -name "category" -type "int"
58 $raw_data = Get-AnsibleParam -obj $params -name "raw_data" -type "str"
59
60 $result = @{
61 changed = $false
62 }
63
64 $log_exists = Test-LogExistence -LogName $log
65 if (!$log_exists) {
66 Fail-Json -obj $result -message "Log $log does not exist and cannot be written to"
67 }
68
69 $source_exists = Test-SourceExistence -LogName $log -SourceName $source
70 if (!$source_exists) {
71 Fail-Json -obj $result -message "Source $source does not exist"
72 }
73
74 if ($event_id -lt 0 -or $event_id -gt 65535) {
75 Fail-Json -obj $result -message "Event ID must be between 0 and 65535"
76 }
77
78 $write_params = @{
79 LogName = $log
80 Source = $source
81 EventId = $event_id
82 Message = $message
83 }
84
85 try {
86 if ($entry_type) {
87 $write_params.EntryType = $entry_type
88 }
89 if ($category) {
90 $write_params.Category = $category
91 }
92 if ($raw_data) {
93 $write_params.RawData = [Byte[]]($raw_data -split ",")
94 }
95
96 if (!$check_mode) {
97 Write-EventLog @write_params
98 }
99 $result.changed = $true
100 $result.msg = "Entry added to log $log from source $source"
101 }
102 catch {
103 Fail-Json -obj $result -message $_.Exception.Message
104 }
105
106 Exit-Json -obj $result