Debian Codegraph @

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

raw
1 #!powershell
2
3 # Copyright: (c) 2017, Ansible Project
4 # Copyright: (c) 2018, Dag Wieers (@dagwieers) <[email protected]>
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 #AnsibleRequires -CSharpUtil Ansible.Basic
8 #Requires -Module Ansible.ModuleUtils.SID
9
10 $spec = @{
11 options = @{
12 process_name_exact = @{ type = 'list'; elements = 'str' }
13 process_name_pattern = @{ type = 'str' }
14 pid = @{ type = 'int'; default = 0 }
15 owner = @{ type = 'str' }
16 sleep = @{ type = 'int'; default = 1 }
17 pre_wait_delay = @{ type = 'int'; default = 0 }
18 post_wait_delay = @{ type = 'int'; default = 0 }
19 process_min_count = @{ type = 'int'; default = 1 }
20 state = @{ type = 'str'; default = 'present'; choices = @( 'absent', 'present' ) }
21 timeout = @{ type = 'int'; default = 300 }
22 }
23 mutually_exclusive = @(
24 @( 'pid', 'process_name_exact' ),
25 @( 'pid', 'process_name_pattern' ),
26 @( 'process_name_exact', 'process_name_pattern' )
27 )
28 required_one_of = @(
29 , @( 'owner', 'pid', 'process_name_exact', 'process_name_pattern' )
30 )
31 supports_check_mode = $true
32 }
33
34 $module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)
35
36 $process_name_exact = $module.Params.process_name_exact
37 $process_name_pattern = $module.Params.process_name_pattern
38 $process_id = $module.Params.pid # pid is a reserved variable in PowerShell, using process_id instead
39 $owner = $module.Params.owner
40 $sleep = $module.Params.sleep
41 $pre_wait_delay = $module.Params.pre_wait_delay
42 $post_wait_delay = $module.Params.post_wait_delay
43 $process_min_count = $module.Params.process_min_count
44 $state = $module.Params.state
45 $timeout = $module.Params.timeout
46
47 $module.Result.changed = $false
48 $module.Result.elapsed = 0
49 $module.Result.matched_processes = @()
50
51 # Validate the input
52 if ($state -eq "absent" -and $sleep -ne 1) {
53 $module.Warn("Parameter 'sleep' has no effect when waiting for a process to stop.")
54 }
55
56 if ($state -eq "absent" -and $process_min_count -ne 1) {
57 $module.Warn("Parameter 'process_min_count' has no effect when waiting for a process to stop.")
58 }
59
60 if ($owner -and ("IncludeUserName" -notin (Get-Command -Name Get-Process).Parameters.Keys)) {
61 $module.FailJson("This version of Powershell does not support filtering processes by 'owner'.")
62 }
63
64 Function Get-FilteredProcess {
65 [cmdletbinding()]
66 Param(
67 [String]
68 $Owner,
69 $ProcessNameExact,
70 $ProcessNamePattern,
71 [int]
72 $ProcessId
73 )
74
75 $FilteredProcesses = @()
76
77 try {
78 $Processes = Get-Process -IncludeUserName
79 $SupportsUserNames = $true
80 }
81 catch [System.Management.Automation.ParameterBindingException] {
82 $Processes = Get-Process
83 $SupportsUserNames = $false
84 }
85
86 foreach ($Process in $Processes) {
87
88 # If a process name was specified in the filter, validate that here.
89 if ($ProcessNamePattern) {
90 if ($Process.ProcessName -notmatch $ProcessNamePattern) {
91 continue
92 }
93 }
94
95 # If a process name was specified in the filter, validate that here.
96 if ($ProcessNameExact -and $ProcessNameExact -notcontains $Process.ProcessName) {
97 continue
98 }
99
100 # If a PID was specified in the filter, validate that here.
101 if ($ProcessId -and $ProcessId -ne 0) {
102 if ($ProcessId -ne $Process.Id) {
103 continue
104 }
105 }
106
107 # If an owner was specified in the filter, validate that here.
108 if ($Owner) {
109 if (-not $Process.UserName) {
110 continue
111 }
112 elseif ((Convert-ToSID($Owner)) -ne (Convert-ToSID($Process.UserName))) {
113 # NOTE: This is rather expensive
114 continue
115 }
116 }
117
118 if ($SupportsUserNames -eq $true) {
119 $FilteredProcesses += @{ name = $Process.ProcessName; pid = $Process.Id; owner = $Process.UserName }
120 }
121 else {
122 $FilteredProcesses += @{ name = $Process.ProcessName; pid = $Process.Id }
123 }
124 }
125
126 return , $FilteredProcesses
127 }
128
129 $module_start = Get-Date
130 Start-Sleep -Seconds $pre_wait_delay
131
132 if ($state -eq "present" ) {
133
134 # Wait for a process to start
135 do {
136
137 $Processes = Get-FilteredProcess -Owner $owner -ProcessNameExact $process_name_exact -ProcessNamePattern $process_name_pattern -ProcessId $process_id
138 $module.Result.matched_processes = $Processes
139
140 if ($Processes.count -ge $process_min_count) {
141 break
142 }
143
144 if (((Get-Date) - $module_start).TotalSeconds -gt $timeout) {
145 $module.Result.elapsed = ((Get-Date) - $module_start).TotalSeconds
146 $module.FailJson("Timed out while waiting for process(es) to start")
147 }
148
149 Start-Sleep -Seconds $sleep
150
151 } while ($true)
152
153 }
154 elseif ($state -eq "absent") {
155
156 # Wait for a process to stop
157 $Processes = Get-FilteredProcess -Owner $owner -ProcessNameExact $process_name_exact -ProcessNamePattern $process_name_pattern -ProcessId $process_id
158 $module.Result.matched_processes = $Processes
159
160 if ($Processes.count -gt 0 ) {
161 try {
162 # This may randomly fail when used on specially protected processes (think: svchost)
163 Wait-Process -Id $Processes.pid -Timeout $timeout
164 }
165 catch [System.TimeoutException] {
166 $module.Result.elapsed = ((Get-Date) - $module_start).TotalSeconds
167 $module.FailJson("Timeout while waiting for process(es) to stop")
168 }
169 }
170
171 }
172
173 Start-Sleep -Seconds $post_wait_delay
174 $module.Result.elapsed = ((Get-Date) - $module_start).TotalSeconds
175
176 $module.ExitJson()