Debian Codegraph @

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

raw
1 #!powershell
2
3 # Copyright: (c) 2020, Brian Scholer <@briantist>
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 #AnsibleRequires -CSharpUtil Ansible.Basic
7 #AnsibleRequires -PowerShell ..module_utils._Tls
8 #Requires -Module @{ ModuleName = 'PowerShellGet' ; ModuleVersion = '1.6.0' }
9
10 $spec = @{
11 supports_check_mode = $true
12 options = @{
13 name = @{
14 type = 'str'
15 required = $true
16 }
17 repository = @{ type = 'str' }
18 scope = @{
19 type = 'str'
20 choices = @('all_users', 'current_user')
21 default = 'all_users'
22 }
23 state = @{
24 type = 'str'
25 choices = @('present', 'absent', 'latest')
26 default = 'present'
27 }
28 required_version = @{ type = 'str' }
29 minimum_version = @{ type = 'str' }
30 maximum_version = @{ type = 'str' }
31 source_username = @{ type = 'str' }
32 source_password = @{
33 type = 'str'
34 no_log = $true
35 }
36 allow_prerelease = @{
37 type = 'bool'
38 default = $false
39 }
40 }
41
42 mutually_exclusive = @(
43 @('required_version', 'minimum_version'),
44 @('required_version', 'maximum_version')
45 )
46
47 required_together = @(
48 , @('source_username', 'source_password')
49 )
50 }
51
52 $module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)
53 $state = $module.Params.state
54
55 Enable-TlsProtocol
56
57 function Get-SplattableParameter {
58 [CmdletBinding(DefaultParameterSetName = 'All')]
59 [OutputType([hashtable])]
60 param(
61 [Parameter(Mandatory = $true)]
62 [Ansible.Basic.AnsibleModule]
63 $Module ,
64
65 [Parameter(ParameterSetName = 'ForCommand')]
66 [Alias('For')]
67 [String]
68 $ForCommand ,
69
70 [Parameter(ParameterSetName = 'WithCommand', ValueFromPipeline = $true)]
71 [Alias('Command')]
72 [System.Management.Automation.CommandInfo]
73 $WithCommand
74 )
75
76 Process {
77 $ret = @{}
78
79 $cmd = switch ($PSCmdlet.ParameterSetName) {
80 'WithCommand' { $WithCommand }
81 'ForCommand' { Get-Command -Name $ForCommand }
82 }
83
84 if ($cmd) {
85 $commons = [System.Management.Automation.Cmdlet]::CommonParameters + [System.Management.Automation.Cmdlet]::CommonOptionalParameters
86 $validParams = $cmd.Parameters.GetEnumerator() | ForEach-Object -Process {
87 if ($commons -notcontains $_.Key) {
88 $_.Key
89 }
90 }
91 }
92
93 switch -Wildcard ($Module.Params.Keys) {
94 '*' {
95 $value = $Module.Params[$_]
96 if ($null -eq $value) {
97 continue
98 }
99
100 $key = $_.Replace('_', '')
101 if ($validParams -and $validParams -notcontains $key) {
102 continue
103 }
104 }
105
106 'scope' { $value = $value.Replace('_', '') }
107 'source_username' { continue } # handled in password block
108 'source_password' {
109 $key = 'Credential'
110 $secure = ConvertTo-SecureString -String $value -AsPlainText -Force
111 $value = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Module.Params.source_username, $secure
112 }
113 '*_version' {
114 if ($Module.Params.state -eq 'latest') {
115 $Module.FailJson("version options can't be used with 'state': 'latest'")
116 }
117 }
118 'repository' {
119 if ($Module.Params.state -eq 'absent') {
120 $Module.FailJson("'repository' is not valid with 'state': 'absent'")
121 }
122 }
123
124 '*' { $ret[$key] = $value }
125 }
126
127 $ret
128 }
129 }
130
131 $pGet, $pUninstall, $pFind, $pInstall = Get-Command -Name @(
132 'Get-InstalledScript'
133 , 'Uninstall-Script'
134 , 'Find-Script'
135 , 'Install-Script'
136 ) | Get-SplattableParameter -Module $module
137
138 $existing = Get-InstalledScript @pGet -ErrorAction SilentlyContinue
139 $existing = if ($existing) {
140 $existing | Group-Object -Property Name -AsHashTable -AsString
141 }
142 else {
143 @{}
144 }
145
146 if ($state -eq 'absent') {
147 if ($existing.Count) {
148 try {
149 $module.Result.changed = $true
150 if (-not $module.CheckMode) {
151 $existing.Values | Uninstall-Script -Force -ErrorAction Stop
152 }
153 }
154 catch {
155 $module.FailJson("Error uninstalling scripts.", $_)
156 }
157 }
158 }
159 else {
160 # state is 'present' or 'latest'
161 try {
162 $remote = Find-Script @pFind -ErrorAction Stop
163 }
164 catch {
165 $module.FailJson("Error searching for scripts.", $_)
166 }
167
168 try {
169 $toInstall = $remote | Where-Object -FilterScript {
170 -not $existing.ContainsKey($_.Name) -or (
171 $state -eq 'latest' -and
172 ($_.Version -as [version]) -gt ($existing[$_.Name].Version -as [version])
173 )
174 }
175
176 if (($toInstall | Group-Object -Property Name -NoElement | Where-Object -Property Count -gt 1)) {
177 $module.FailJson("Multiple scripts found. Please choose a specific repository.")
178 }
179
180 $module.Result.changed = $toInstall -as [bool]
181
182 if ($toInstall -and -not $module.CheckMode) {
183 $toInstall | Install-Script -Scope:$pInstall.scope -Force -ErrorAction Stop
184 }
185 }
186 catch {
187 $module.FailJson("Error installing scripts.", $_)
188 }
189 }
190
191 $module.ExitJson()