win_domain_membership: added better error handling and basic tests (#30674)

This commit is contained in:
Jordan Borean 2017-09-22 05:59:42 +10:00 committed by GitHub
commit fb628acb6e
4 changed files with 99 additions and 3 deletions

View file

@ -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.