| 1 |
#!powershell |
| 2 |
|
| 3 |
# Copyright: (c) 2017, Ansible Project |
| 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 |
$params = Parse-Args -arguments $args -supports_check_mode $true |
| 9 |
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false |
| 10 |
$managers = Get-AnsibleParam -obj $params -name "permitted_managers" -type "list" -default $null |
| 11 |
$communities = Get-AnsibleParam -obj $params -name "community_strings" -type "list" -default $null |
| 12 |
$action_in = Get-AnsibleParam -obj $params -name "action" -type "str" -default "set" -ValidateSet @("set", "add", "remove") |
| 13 |
$action = $action_in.ToLower() |
| 14 |
|
| 15 |
$result = @{ |
| 16 |
failed = $False |
| 17 |
changed = $False |
| 18 |
community_strings = [System.Collections.ArrayList]@() |
| 19 |
permitted_managers = [System.Collections.ArrayList]@() |
| 20 |
} |
| 21 |
|
| 22 |
# Make sure lists are modifyable |
| 23 |
[System.Collections.ArrayList]$managers = $managers |
| 24 |
[System.Collections.ArrayList]$communities = $communities |
| 25 |
[System.Collections.ArrayList]$indexes = @() |
| 26 |
|
| 27 |
# Type checking |
| 28 |
# You would think that "$null -ne $managers" would work, but it doesn't. |
| 29 |
# A proper type check is required. If a user provides an empty list then $managers |
| 30 |
# is still of the correct type. If a user provides no option then $managers is $null. |
| 31 |
If ($managers -Is [System.Collections.ArrayList] -And $managers.Count -gt 0 -And $managers[0] -IsNot [String]) { |
| 32 |
Fail-Json $result "Permitted managers must be an array of strings" |
| 33 |
} |
| 34 |
|
| 35 |
If ($communities -Is [System.Collections.ArrayList] -And $communities.Count -gt 0 -And $communities[0] -IsNot [String]) { |
| 36 |
Fail-Json $result "SNMP communities must be an array of strings" |
| 37 |
} |
| 38 |
|
| 39 |
$Managers_reg_key = "HKLM:\System\CurrentControlSet\services\SNMP\Parameters\PermittedManagers" |
| 40 |
$Communities_reg_key = "HKLM:\System\CurrentControlSet\services\SNMP\Parameters\ValidCommunities" |
| 41 |
|
| 42 |
ForEach ($idx in (Get-Item -LiteralPath $Managers_reg_key).Property) { |
| 43 |
$manager = (Get-ItemProperty -LiteralPath $Managers_reg_key).$idx |
| 44 |
If ($idx.ToLower() -eq '(default)') { |
| 45 |
continue |
| 46 |
} |
| 47 |
|
| 48 |
$remove = $False |
| 49 |
If ($managers -Is [System.Collections.ArrayList] -And $managers.Contains($manager)) { |
| 50 |
If ($action -eq "remove") { |
| 51 |
$remove = $True |
| 52 |
} |
| 53 |
Else { |
| 54 |
# Remove manager from list to add since it already exists |
| 55 |
$managers.Remove($manager) |
| 56 |
} |
| 57 |
} |
| 58 |
ElseIf ($action -eq "set" -And $managers -Is [System.Collections.ArrayList]) { |
| 59 |
# Will remove this manager since it is not in the set list |
| 60 |
$remove = $True |
| 61 |
} |
| 62 |
|
| 63 |
If ($remove) { |
| 64 |
$result.changed = $True |
| 65 |
Remove-ItemProperty -LiteralPath $Managers_reg_key -Name $idx -WhatIf:$check_mode |
| 66 |
} |
| 67 |
Else { |
| 68 |
# Remember that this index is in use |
| 69 |
$indexes.Add([int]$idx) | Out-Null |
| 70 |
$result.permitted_managers.Add($manager) | Out-Null |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
ForEach ($community in (Get-Item -LiteralPath $Communities_reg_key).Property) { |
| 75 |
If ($community.ToLower() -eq '(default)') { |
| 76 |
continue |
| 77 |
} |
| 78 |
|
| 79 |
$remove = $False |
| 80 |
If ($communities -Is [System.Collections.ArrayList] -And $communities.Contains($community)) { |
| 81 |
If ($action -eq "remove") { |
| 82 |
$remove = $True |
| 83 |
} |
| 84 |
Else { |
| 85 |
# Remove community from list to add since it already exists |
| 86 |
$communities.Remove($community) |
| 87 |
} |
| 88 |
} |
| 89 |
ElseIf ($action -eq "set" -And $communities -Is [System.Collections.ArrayList]) { |
| 90 |
# Will remove this community since it is not in the set list |
| 91 |
$remove = $True |
| 92 |
} |
| 93 |
|
| 94 |
If ($remove) { |
| 95 |
$result.changed = $True |
| 96 |
Remove-ItemProperty -LiteralPath $Communities_reg_key -Name $community -WhatIf:$check_mode |
| 97 |
} |
| 98 |
Else { |
| 99 |
$result.community_strings.Add($community) | Out-Null |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
If ($action -eq "remove") { |
| 104 |
Exit-Json $result |
| 105 |
} |
| 106 |
|
| 107 |
# Add managers that don't already exist |
| 108 |
$next_index = 0 |
| 109 |
If ($managers -Is [System.Collections.ArrayList]) { |
| 110 |
ForEach ($manager in $managers) { |
| 111 |
While ($True) { |
| 112 |
$next_index = $next_index + 1 |
| 113 |
If (-Not $indexes.Contains($next_index)) { |
| 114 |
$result.changed = $True |
| 115 |
New-ItemProperty -LiteralPath $Managers_reg_key -Name $next_index -Value "$manager" -WhatIf:$check_mode | Out-Null |
| 116 |
$result.permitted_managers.Add($manager) | Out-Null |
| 117 |
break |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
# Add communities that don't already exist |
| 124 |
If ($communities -Is [System.Collections.ArrayList]) { |
| 125 |
ForEach ($community in $communities) { |
| 126 |
$result.changed = $True |
| 127 |
New-ItemProperty -LiteralPath $Communities_reg_key -Name $community -PropertyType DWord -Value 4 -WhatIf:$check_mode | Out-Null |
| 128 |
$result.community_strings.Add($community) | Out-Null |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
Exit-Json $result |