summaryrefslogtreecommitdiff
path: root/lib/ansible/modules/windows
diff options
context:
space:
mode:
authorJordan Borean <jborean93@gmail.com>2017-09-22 05:59:42 +1000
committerGitHub <noreply@github.com>2017-09-22 05:59:42 +1000
commitfb628acb6e26d7f177e9a1c84a228d4415977a22 (patch)
treed302dc239dd9679ba45ada8f1f253852f9a576f1 /lib/ansible/modules/windows
parent59187358ee13d01edb8b798391a5b301079c94f3 (diff)
downloadansible-fb628acb6e26d7f177e9a1c84a228d4415977a22.tar.gz
win_domain_membership: added better error handling and basic tests (#30674)
Diffstat (limited to 'lib/ansible/modules/windows')
-rw-r--r--lib/ansible/modules/windows/win_domain_membership.ps122
1 files changed, 19 insertions, 3 deletions
diff --git a/lib/ansible/modules/windows/win_domain_membership.ps1 b/lib/ansible/modules/windows/win_domain_membership.ps1
index d19aa46d5c..28daeb00a5 100644
--- a/lib/ansible/modules/windows/win_domain_membership.ps1
+++ b/lib/ansible/modules/windows/win_domain_membership.ps1
@@ -125,7 +125,11 @@ Function Join-Domain {
}
$argstr = $add_args | Out-String
Write-DebugLog "calling Add-Computer with args: $argstr"
- $add_result = Add-Computer @add_args
+ try {
+ $add_result = Add-Computer @add_args
+ } catch {
+ Fail-Json -obj $result -message "failed to join domain: $($_.Exception.Message)"
+ }
Write-DebugLog ("Add-Computer result was \n{0}" -f $add_result | Out-String)
}
@@ -140,8 +144,16 @@ Function Set-Workgroup {
)
Write-DebugLog ("Calling JoinDomainOrWorkgroup with workgroup {0}" -f $workgroup_name)
+ try {
+ $swg_result = (Get-WmiObject -ClassName Win32_ComputerSystem).JoinDomainOrWorkgroup($workgroup_name)
+ } catch {
+ Fail-Json -obj $result -message "failed to call Win32_ComputerSystem.JoinDomainOrWorkgroup($workgroup_name): $($_.Exception.Message)"
+ }
- return (Get-WmiObject Win32_ComputerSystem).JoinDomainOrWorkgroup($workgroup_name)
+ if ($swg_result.ReturnValue -ne 0) {
+ Fail-Json -obj $result -message "failed to set workgroup through WMI, return value: $($swg_result.ReturnValue)"
+
+ return $swg_result}
}
Function Join-Workgroup {
@@ -155,7 +167,11 @@ Function Join-Workgroup {
$domain_cred = Create-Credential $domain_admin_user $domain_admin_password
# 2012+ call the Workgroup arg WorkgroupName, but seem to accept
- $rc_result = Remove-Computer -Workgroup $workgroup_name -Credential $domain_cred -Force
+ try {
+ $rc_result = Remove-Computer -Workgroup $workgroup_name -Credential $domain_cred -Force
+ } catch {
+ Fail-Json -obj $result -message "failed to remove computer from domain: $($_.Exception.Message)"
+ }
}
# we're already on a workgroup- change it.