Debian Codegraph @

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

raw
1 #!powershell
2
3 # Copyright: (c) 2019, Thomas Moore (@tmmruk) <[email protected]>
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
8 $spec = @{
9 options = @{
10 state = @{ type = "str"; choices = "enabled", "disabled", "default"; required = $true }
11 adapter_names = @{ type = "list"; elements = "str"; required = $false }
12 }
13 supports_check_mode = $true
14 }
15
16 $module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)
17 $module.Result.reboot_required = $false
18
19 $state = $module.Params.state
20 $adapter_names = $module.Params.adapter_names
21
22 switch ( $state ) {
23 'default' { $netbiosoption = 0 }
24 enabled { $netbiosoption = 1 }
25 disabled { $netbiosoption = 2 }
26 }
27
28 if (-not $adapter_names) {
29 # Target all network adapters on the system
30 $get_params = @{
31 ClassName = 'Win32_NetworkAdapterConfiguration'
32 Filter = 'IPEnabled=true'
33 Property = @('MacAddress', 'TcpipNetbiosOptions')
34 }
35 $target_adapters_config = Get-CimInstance @get_params
36 }
37 else {
38 $get_params = @{
39 Class = 'Win32_NetworkAdapter'
40 Filter = ($adapter_names | ForEach-Object -Process { "NetConnectionId='$_'" }) -join " OR "
41 KeyOnly = $true
42 }
43 $target_adapters_config = Get-CimInstance @get_params | Get-CimAssociatedInstance -ResultClass 'Win32_NetworkAdapterConfiguration'
44 if (($target_adapters_config | Measure-Object).Count -ne $adapter_names.Count) {
45 $module.FailJson("Not all of the target adapter names could be found on the system. No configuration changes have been made. $adapter_names")
46 }
47 }
48
49 foreach ($adapter in $target_adapters_config) {
50 if ($adapter.TcpipNetbiosOptions -ne $netbiosoption) {
51 if (-not $module.CheckMode) {
52 $result = Invoke-CimMethod -InputObject $adapter -MethodName SetTcpipNetbios -Arguments @{TcpipNetbiosOptions = $netbiosoption }
53 switch ( $result.ReturnValue ) {
54 0 { <# Success no reboot required #> }
55 1 { $module.Result.reboot_required = $true }
56 100 {
57 $msg = "DHCP not enabled on adapter $($adapter.MacAddress). Unable to set default. Try using disabled or enabled options instead."
58 $module.Warn($msg)
59 }
60 default {
61 $msg = "An error occurred while setting TcpipNetbios options on adapter $($adapter.MacAddress). Return code $($result.ReturnValue)."
62 $module.FailJson($msg)
63 }
64 }
65 }
66 $module.Result.changed = $true
67 }
68 }
69
70 $module.ExitJson()