summaryrefslogtreecommitdiffstats
path: root/lib/ansible/module_utils/powershell/Ansible.ModuleUtils.Legacy.psm1
blob: f0cb440f6870a36a92f98fbe38ed6f87ad4cc513 (plain)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# Copyright (c), Michael DeHaan <michael.dehaan@gmail.com>, 2014, and others
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause)

Set-StrictMode -Version 2.0
$ErrorActionPreference = "Stop"

Function Set-Attr($obj, $name, $value) {
    <#
    .SYNOPSIS
    Helper function to set an "attribute" on a psobject instance in PowerShell.
    This is a convenience to make adding Members to the object easier and
    slightly more pythonic
    .EXAMPLE
    Set-Attr $result "changed" $true
#>

    # If the provided $obj is undefined, define one to be nice
    If (-not $obj.GetType) {
        $obj = @{ }
    }

    Try {
        $obj.$name = $value
    }
    Catch {
        $obj | Add-Member -Force -MemberType NoteProperty -Name $name -Value $value
    }
}

Function Exit-Json($obj) {
    <#
    .SYNOPSIS
    Helper function to convert a PowerShell object to JSON and output it, exiting
    the script
    .EXAMPLE
    Exit-Json $result
#>

    # If the provided $obj is undefined, define one to be nice
    If (-not $obj.GetType) {
        $obj = @{ }
    }

    if (-not $obj.ContainsKey('changed')) {
        Set-Attr -obj $obj -name "changed" -value $false
    }

    Write-Output $obj | ConvertTo-Json -Compress -Depth 99
    Exit
}

Function Fail-Json($obj, $message = $null) {
    <#
    .SYNOPSIS
    Helper function to add the "msg" property and "failed" property, convert the
    PowerShell Hashtable to JSON and output it, exiting the script
    .EXAMPLE
    Fail-Json $result "This is the failure message"
#>

    if ($obj -is [hashtable] -or $obj -is [psobject]) {
        # Nothing to do
    }
    elseif ($obj -is [string] -and $null -eq $message) {
        # If we weren't given 2 args, and the only arg was a string,
        # create a new Hashtable and use the arg as the failure message
        $message = $obj
        $obj = @{ }
    }
    else {
        # If the first argument is undefined or a different type,
        # make it a Hashtable
        $obj = @{ }
    }

    # Still using Set-Attr for PSObject compatibility
    Set-Attr -obj $obj -name "msg" -value $message
    Set-Attr -obj $obj -name "failed" -value $true

    if (-not $obj.ContainsKey('changed')) {
        Set-Attr -obj $obj -name "changed" -value $false
    }

    Write-Output $obj | ConvertTo-Json -Compress -Depth 99
    Exit 1
}

Function Add-Warning($obj, $message) {
    <#
    .SYNOPSIS
    Helper function to add warnings, even if the warnings attribute was
    not already set up. This is a convenience for the module developer
    so they do not have to check for the attribute prior to adding.
#>

    if (-not $obj.ContainsKey("warnings")) {
        $obj.warnings = @()
    }
    elseif ($obj.warnings -isnot [array]) {
        throw "Add-Warning: warnings attribute is not an array"
    }

    $obj.warnings += $message
}

Function Add-DeprecationWarning($obj, $message, $version = $null) {
    <#
    .SYNOPSIS
    Helper function to add deprecations, even if the deprecations attribute was
    not already set up. This is a convenience for the module developer
    so they do not have to check for the attribute prior to adding.
#>
    if (-not $obj.ContainsKey("deprecations")) {
        $obj.deprecations = @()
    }
    elseif ($obj.deprecations -isnot [array]) {
        throw "Add-DeprecationWarning: deprecations attribute is not a list"
    }

    $obj.deprecations += @{
        msg = $message
        version = $version
    }
}

Function Expand-Environment($value) {
    <#
    .SYNOPSIS
    Helper function to expand environment variables in values. By default
    it turns any type to a string, but we ensure $null remains $null.
#>
    if ($null -ne $value) {
        [System.Environment]::ExpandEnvironmentVariables($value)
    }
    else {
        $value
    }
}

Function Get-AnsibleParam {
    <#
    .SYNOPSIS
    Helper function to get an "attribute" from a psobject instance in PowerShell.
    This is a convenience to make getting Members from an object easier and
    slightly more pythonic
    .EXAMPLE
    $attr = Get-AnsibleParam $response "code" -default "1"
    .EXAMPLE
    Get-AnsibleParam -obj $params -name "State" -default "Present" -ValidateSet "Present","Absent" -resultobj $resultobj -failifempty $true
    Get-AnsibleParam also supports Parameter validation to save you from coding that manually
    Note that if you use the failifempty option, you do need to specify resultobject as well.
#>
    param (
        $obj,
        $name,
        $default = $null,
        $resultobj = @{},
        $failifempty = $false,
        $emptyattributefailmessage,
        $ValidateSet,
        $ValidateSetErrorMessage,
        $type = $null,
        $aliases = @()
    )
    # Check if the provided Member $name or aliases exist in $obj and return it or the default.
    try {

        $found = $null
        # First try to find preferred parameter $name
        $aliases = @($name) + $aliases

        # Iterate over aliases to find acceptable Member $name
        foreach ($alias in $aliases) {
            if ($obj.ContainsKey($alias)) {
                $found = $alias
                break
            }
        }

        if ($null -eq $found) {
            throw
        }
        $name = $found

        if ($ValidateSet) {

            if ($ValidateSet -contains ($obj.$name)) {
                $value = $obj.$name
            }
            else {
                if ($null -eq $ValidateSetErrorMessage) {
                    #Auto-generated error should be sufficient in most use cases
                    $ValidateSetErrorMessage = "Get-AnsibleParam: Argument $name needs to be one of $($ValidateSet -join ",") but was $($obj.$name)."
                }
                Fail-Json -obj $resultobj -message $ValidateSetErrorMessage
            }
        }
        else {
            $value = $obj.$name
        }
    }
    catch {
        if ($failifempty -eq $false) {
            $value = $default
        }
        else {
            if (-not $emptyattributefailmessage) {
                $emptyattributefailmessage = "Get-AnsibleParam: Missing required argument: $name"
            }
            Fail-Json -obj $resultobj -message $emptyattributefailmessage
        }
    }

    # If $null -eq $value, the parameter was unspecified by the user (deliberately or not)
    # Please leave $null-values intact, modules need to know if a parameter was specified
    if ($null -eq $value) {
        return $null
    }

    if ($type -eq "path") {
        # Expand environment variables on path-type
        $value = Expand-Environment($value)
        # Test if a valid path is provided
        if (-not (Test-Path -IsValid $value)) {
            $path_invalid = $true
            # could still be a valid-shaped path with a nonexistent drive letter
            if ($value -match "^\w:") {
                # rewrite path with a valid drive letter and recheck the shape- this might still fail, eg, a nonexistent non-filesystem PS path
                if (Test-Path -IsValid $(@(Get-PSDrive -PSProvider Filesystem)[0].Name + $value.Substring(1))) {
                    $path_invalid = $false
                }
            }
            if ($path_invalid) {
                Fail-Json -obj $resultobj -message "Get-AnsibleParam: Parameter '$name' has an invalid path '$value' specified."
            }
        }
    }
    elseif ($type -eq "str") {
        # Convert str types to real Powershell strings
        $value = $value.ToString()
    }
    elseif ($type -eq "bool") {
        # Convert boolean types to real Powershell booleans
        $value = $value | ConvertTo-Bool
    }
    elseif ($type -eq "int") {
        # Convert int types to real Powershell integers
        $value = $value -as [int]
    }
    elseif ($type -eq "float") {
        # Convert float types to real Powershell floats
        $value = $value -as [float]
    }
    elseif ($type -eq "list") {
        if ($value -is [array]) {
            # Nothing to do
        }
        elseif ($value -is [string]) {
            # Convert string type to real Powershell array
            $value = $value.Split(",").Trim()
        }
        elseif ($value -is [int]) {
            $value = @($value)
        }
        else {
            Fail-Json -obj $resultobj -message "Get-AnsibleParam: Parameter '$name' is not a YAML list."
        }
        # , is not a typo, forces it to return as a list when it is empty or only has 1 entry
        return , $value
    }

    return $value
}

#Alias Get-attr-->Get-AnsibleParam for backwards compat. Only add when needed to ease debugging of scripts
If (-not(Get-Alias -Name "Get-attr" -ErrorAction SilentlyContinue)) {
    New-Alias -Name Get-attr -Value Get-AnsibleParam
}

Function ConvertTo-Bool {
    <#
    .SYNOPSIS
    Helper filter/pipeline function to convert a value to boolean following current
    Ansible practices
    .EXAMPLE
    $is_true = "true" | ConvertTo-Bool
#>
    param(
        [parameter(valuefrompipeline = $true)]
        $obj
    )

    process {
        $boolean_strings = "yes", "on", "1", "true", 1
        $obj_string = [string]$obj

        if (($obj -is [boolean] -and $obj) -or $boolean_strings -contains $obj_string.ToLower()) {
            return $true
        }
        else {
            return $false
        }
    }
}

Function Parse-Args {
    <#
    .SYNOPSIS
    Helper function to parse Ansible JSON arguments from a "file" passed as
    the single argument to the module.
    .EXAMPLE
    $params = Parse-Args $args
#>
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseSingularNouns", "", Justification = "Cannot change the name now")]
    param ($arguments, $supports_check_mode = $false)

    $params = New-Object psobject
    If ($arguments.Length -gt 0) {
        $params = Get-Content $arguments[0] | ConvertFrom-Json
    }
    Else {
        $params = $complex_args
    }
    $check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false
    If ($check_mode -and -not $supports_check_mode) {
        Exit-Json @{
            skipped = $true
            changed = $false
            msg = "remote module does not support check mode"
        }
    }
    return $params
}


Function Get-FileChecksum($path, $algorithm = 'sha1') {
    <#
    .SYNOPSIS
    Helper function to calculate a hash of a file in a way which PowerShell 3
    and above can handle
#>
    If (Test-Path -LiteralPath $path -PathType Leaf) {
        switch ($algorithm) {
            'md5' { $sp = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider }
            'sha1' { $sp = New-Object -TypeName System.Security.Cryptography.SHA1CryptoServiceProvider }
            'sha256' { $sp = New-Object -TypeName System.Security.Cryptography.SHA256CryptoServiceProvider }
            'sha384' { $sp = New-Object -TypeName System.Security.Cryptography.SHA384CryptoServiceProvider }
            'sha512' { $sp = New-Object -TypeName System.Security.Cryptography.SHA512CryptoServiceProvider }
            default { Fail-Json @{} "Unsupported hash algorithm supplied '$algorithm'" }
        }

        If ($PSVersionTable.PSVersion.Major -ge 4) {
            $raw_hash = Get-FileHash -LiteralPath $path -Algorithm $algorithm
            $hash = $raw_hash.Hash.ToLower()
        }
        Else {
            $fp = [System.IO.File]::Open($path, [System.IO.Filemode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::ReadWrite);
            $hash = [System.BitConverter]::ToString($sp.ComputeHash($fp)).Replace("-", "").ToLower();
            $fp.Dispose();
        }
    }
    ElseIf (Test-Path -LiteralPath $path -PathType Container) {
        $hash = "3";
    }
    Else {
        $hash = "1";
    }
    return $hash
}

Function Get-PendingRebootStatus {
    <#
    .SYNOPSIS
    Check if reboot is required, if so notify CA.
    Function returns true if computer has a pending reboot
#>
    $featureData = Invoke-CimMethod -EA Ignore -Name GetServerFeature -Namespace root\microsoft\windows\servermanager -Class MSFT_ServerManagerTasks
    $regData = Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" "PendingFileRenameOperations" -EA Ignore
    $CBSRebootStatus = Get-ChildItem "HKLM:\\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing" -ErrorAction SilentlyContinue |
        Where-Object { $_.PSChildName -eq "RebootPending" }
    if (($featureData -and $featureData.RequiresReboot) -or $regData -or $CBSRebootStatus) {
        return $True
    }
    else {
        return $False
    }
}

# this line must stay at the bottom to ensure all defined module parts are exported
Export-ModuleMember -Alias * -Function * -Cmdlet *