Debian Codegraph @

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

raw
1 #!powershell
2
3 # Copyright: (c) 2015, Peter Mounce <[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 Find-InstalledCommand {
11 [CmdletBinding()]
12 param(
13 [Parameter(Mandatory = $true, Position = 0)] [string] $command
14 )
15 $installed = get-command $command -erroraction Ignore
16 write-verbose "$installed"
17 if ($installed) {
18 return $installed
19 }
20 return $null
21 }
22
23 Function Find-WebPiCmd {
24 [CmdletBinding()]
25 param()
26 $p = Find-InstalledCommand "webpicmd.exe"
27 if ($null -ne $p) {
28 return $p
29 }
30 $a = Find-InstalledCommand "c:\programdata\chocolatey\bin\webpicmd.exe"
31 if ($null -ne $a) {
32 return $a
33 }
34 Throw "webpicmd.exe is not installed. It must be installed (use chocolatey)"
35 }
36
37 Function Test-IsInstalledFromWebPI {
38 [CmdletBinding()]
39
40 param(
41 [Parameter(Mandatory = $true, Position = 0)]
42 [string]$package
43 )
44
45 $results = &$executable /list /listoption:installed
46
47 if ($LastExitCode -ne 0) {
48 $result.webpicmd_error_cmd = $cmd
49 $result.webpicmd_error_log = "$results"
50
51 Throw "Error checking installation status for $package"
52 }
53 Write-Verbose "$results"
54
55 if ($results -match "^$package\s+") {
56 return $true
57 }
58
59 return $false
60 }
61
62 Function Install-WithWebPICmd {
63 [CmdletBinding()]
64
65 param(
66 [Parameter(Mandatory = $true, Position = 0)]
67 [string]$package
68 )
69
70 $results = &$executable /install /products:$package /accepteula /suppressreboot
71
72 if ($LastExitCode -ne 0) {
73 $result.webpicmd_error_cmd = $cmd
74 $result.webpicmd_error_log = "$results"
75 Throw "Error installing $package"
76 }
77
78 write-verbose "$results"
79
80 if ($results -match "Install of Products: SUCCESS") {
81 $result.changed = $true
82 }
83 }
84
85 $result = @{
86 changed = $false
87 }
88
89 $params = Parse-Args $args
90
91 $package = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $true
92
93 Try {
94 $script:executable = Find-WebPiCmd
95 if ((Test-IsInstalledFromWebPI -package $package) -eq $false) {
96 Install-WithWebPICmd -package $package
97 }
98
99 Exit-Json $result
100 }
101 Catch {
102 Fail-Json $result $_.Exception.Message
103 }