| 1 |
#!powershell |
| 2 |
|
| 3 |
# Copyright (c) 2022 Ansible Project |
| 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 |
dns_domain_name = @{ |
| 11 |
type = 'str' |
| 12 |
} |
| 13 |
domain_admin_user = @{ |
| 14 |
type = 'str' |
| 15 |
} |
| 16 |
domain_admin_password = @{ |
| 17 |
no_log = $true |
| 18 |
type = 'str' |
| 19 |
} |
| 20 |
domain_ou_path = @{ |
| 21 |
type = 'str' |
| 22 |
} |
| 23 |
domain_server = @{ |
| 24 |
type = 'str' |
| 25 |
} |
| 26 |
hostname = @{ |
| 27 |
type = 'str' |
| 28 |
} |
| 29 |
offline_join_blob = @{ |
| 30 |
type = "str" |
| 31 |
no_log = $true |
| 32 |
} |
| 33 |
reboot = @{ |
| 34 |
default = $false |
| 35 |
type = 'bool' |
| 36 |
} |
| 37 |
reboot_timeout = @{ |
| 38 |
default = 600 |
| 39 |
type = 'int' |
| 40 |
} |
| 41 |
state = @{ |
| 42 |
choices = 'domain', 'workgroup' |
| 43 |
required = $true |
| 44 |
type = 'str' |
| 45 |
} |
| 46 |
workgroup_name = @{ |
| 47 |
type = 'str' |
| 48 |
} |
| 49 |
} |
| 50 |
mutually_exclusive = @( |
| 51 |
@('offline_join_blob', 'domain_admin_user'), |
| 52 |
@('offline_join_blob', 'dns_domain_name'), |
| 53 |
@('offline_join_blob', 'domain_ou_path'), |
| 54 |
@('offline_join_blob', 'hostname') |
| 55 |
) |
| 56 |
required_if = @( |
| 57 |
@('state', 'domain', @('domain_admin_user', 'offline_join_blob'), $true), |
| 58 |
@('state', 'workgroup', @('workgroup_name', 'domain_admin_user', 'domain_admin_password')) |
| 59 |
) |
| 60 |
required_together = @( |
| 61 |
, @('domain_admin_user', 'domain_admin_password') |
| 62 |
) |
| 63 |
supports_check_mode = $true |
| 64 |
} |
| 65 |
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec) |
| 66 |
|
| 67 |
$module.Result.reboot_required = $false |
| 68 |
$module.Diff.before = @{} |
| 69 |
$module.Diff.after = @{} |
| 70 |
|
| 71 |
$dnsDomainName = $module.Params.dns_domain_name |
| 72 |
$domainCredential = if ($module.Params.domain_admin_user) { |
| 73 |
New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList @( |
| 74 |
$module.Params.domain_admin_user, |
| 75 |
(ConvertTo-SecureString -AsPlainText -Force -String $module.Params.domain_admin_password) |
| 76 |
) |
| 77 |
} |
| 78 |
$domainOUPath = $module.Params.domain_ou_path |
| 79 |
$domainServer = $module.Params.domain_server |
| 80 |
$hostname = $module.Params.hostname |
| 81 |
$state = $module.Params.state |
| 82 |
$workgroupName = $module.Params.workgroup_name |
| 83 |
|
| 84 |
Add-CSharpType -AnsibleModule $module -References @' |
| 85 |
using System; |
| 86 |
using System.ComponentModel; |
| 87 |
using System.Runtime.InteropServices; |
| 88 |
|
| 89 |
namespace microsoft.ad.membership |
| 90 |
{ |
| 91 |
[Flags] |
| 92 |
public enum ProvisionOptions |
| 93 |
{ |
| 94 |
None = 0, |
| 95 |
NETSETUP_PROVISION_ONLINE_CALLER = 0x40000000, |
| 96 |
} |
| 97 |
|
| 98 |
public static class Native |
| 99 |
{ |
| 100 |
[DllImport("Netapi32.dll", EntryPoint = "NetRequestOfflineDomainJoin")] |
| 101 |
private static extern int NativeNetRequestOfflineDomainJoin( |
| 102 |
IntPtr pProvisionBinData, |
| 103 |
int cbProvisionBinDataSize, |
| 104 |
int dwOptions, |
| 105 |
[MarshalAs(UnmanagedType.LPWStr)] string lpWindowsPath); |
| 106 |
|
| 107 |
public static void NetRequestOfflineDomainJoin(byte[] data, ProvisionOptions options, |
| 108 |
string windowsPath) |
| 109 |
{ |
| 110 |
IntPtr buffer = Marshal.AllocHGlobal(data.Length); |
| 111 |
try |
| 112 |
{ |
| 113 |
Marshal.Copy(data, 0, buffer, data.Length); |
| 114 |
|
| 115 |
int res = NativeNetRequestOfflineDomainJoin(buffer, data.Length, (int)options, windowsPath); |
| 116 |
if (res != 0) |
| 117 |
{ |
| 118 |
throw new Win32Exception(res); |
| 119 |
} |
| 120 |
} |
| 121 |
finally { |
| 122 |
Marshal.FreeHGlobal(buffer); |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
'@ |
| 128 |
|
| 129 |
Function Get-CurrentState { |
| 130 |
<# |
| 131 |
.SYNOPSIS |
| 132 |
Gets the current state of the host. |
| 133 |
#> |
| 134 |
[CmdletBinding()] |
| 135 |
param () |
| 136 |
|
| 137 |
$cs = Get-CimInstance -ClassName Win32_ComputerSystem -Property Domain, PartOfDomain, Workgroup |
| 138 |
$domainName = if ($cs.PartOfDomain) { |
| 139 |
try { |
| 140 |
[System.DirectoryServices.ActiveDirectory.Domain]::GetComputerDomain().Name |
| 141 |
} |
| 142 |
catch [System.Security.Authentication.AuthenticationException] { |
| 143 |
# This might happen if running as a local user on a host already |
| 144 |
# joined to the domain. Just try the Win32_ComputerSystem fallback |
| 145 |
# value. |
| 146 |
$cs.Domain |
| 147 |
} |
| 148 |
} |
| 149 |
else { |
| 150 |
$null |
| 151 |
} |
| 152 |
|
| 153 |
[PSCustomObject]@{ |
| 154 |
HostName = [System.Net.Dns]::GetHostName() |
| 155 |
PartOfDomain = $cs.PartOfDomain |
| 156 |
DnsDomainName = $domainName |
| 157 |
WorkgroupName = $cs.Workgroup |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
$currentState = Get-CurrentState |
| 162 |
|
| 163 |
$module.Diff.before = @{ |
| 164 |
dns_domain_name = $currentState.DnsDomainName |
| 165 |
hostname = $currentState.HostName |
| 166 |
state = if ($currentState.PartOfDomain) { 'domain' } else { 'workgroup' } |
| 167 |
workgroup_name = $currentState.WorkgroupName |
| 168 |
} |
| 169 |
if (-not $hostname) { |
| 170 |
$hostname = $currentState.HostName |
| 171 |
} |
| 172 |
|
| 173 |
if ($state -eq 'domain') { |
| 174 |
if ($module.Params.offline_join_blob) { |
| 175 |
# FUTURE: Read blob to see what domain it is for. |
| 176 |
if (-not $currentState.PartOfDomain) { |
| 177 |
try { |
| 178 |
if (-not $module.CheckMode) { |
| 179 |
[microsoft.ad.membership.Native]::NetRequestOfflineDomainJoin( |
| 180 |
[System.Convert]::FromBase64String($module.Params.offline_join_blob), |
| 181 |
"NETSETUP_PROVISION_ONLINE_CALLER", |
| 182 |
$env:SystemRoot) |
| 183 |
} |
| 184 |
} |
| 185 |
catch [System.ComponentModel.Win32Exception] { |
| 186 |
$msg = "Failed to perform offline domain join (0x{0:X8}): {1}" -f $_.Exception.NativeErrorCode, $_.Exception.Message |
| 187 |
$module.FailJson($msg, $_) |
| 188 |
} |
| 189 |
|
| 190 |
$module.Result.changed = $true |
| 191 |
$module.Result.reboot_required = $true |
| 192 |
} |
| 193 |
} |
| 194 |
else { |
| 195 |
if ($dnsDomainName -ne $currentState.DnsDomainName) { |
| 196 |
if ($currentState.PartOfDomain) { |
| 197 |
$module.FailJson("Host is already joined to '$($currentState.DnsDomainName)', switching domains is not implemented") |
| 198 |
} |
| 199 |
|
| 200 |
$joinParams = @{ |
| 201 |
ComputerName = '.' |
| 202 |
Credential = $domainCredential |
| 203 |
DomainName = $dnsDomainName |
| 204 |
Force = $true |
| 205 |
WhatIf = $module.CheckMode |
| 206 |
} |
| 207 |
if ($hostname -ne $currentState.HostName) { |
| 208 |
$renameParams = @{ |
| 209 |
NewName = $hostname |
| 210 |
WhatIf = $module.CheckMode |
| 211 |
Force = $true |
| 212 |
} |
| 213 |
Rename-Computer @renameParams |
| 214 |
# Update this now to reflect the new name |
| 215 |
$hostname = $currentState.HostName |
| 216 |
|
| 217 |
$module.Result.changed = $true |
| 218 |
$module.Result.reboot_required = $true |
| 219 |
|
| 220 |
# Note: AccountCreate is default behavior, but needs included when Options is set |
| 221 |
$joinParams.Options = 'AccountCreate,JoinWithNewName' |
| 222 |
} |
| 223 |
if ($domainOUPath) { |
| 224 |
$joinParams.OUPath = $domainOUPath |
| 225 |
} |
| 226 |
if ($domainServer) { |
| 227 |
$joinParams.Server = $domainServer |
| 228 |
} |
| 229 |
|
| 230 |
try { |
| 231 |
Add-Computer @joinParams |
| 232 |
} |
| 233 |
catch { |
| 234 |
$failMsg = [string]$_ |
| 235 |
|
| 236 |
# The error if the domain_ou_path does not exist is a bit |
| 237 |
# vague, we try to catch that specific error type and provide |
| 238 |
# a more helpful hint to what is wrong. As the exception does |
| 239 |
# not have an error code to check, we compare the Win32 error |
| 240 |
# code message with a localized variant for |
| 241 |
# ERROR_FILE_NOT_FOUND. .NET Framework does not end with . |
| 242 |
# whereas .NET 5+ does so we use regex to match both patterns. |
| 243 |
# https://github.com/ansible-collections/microsoft.ad/issues/88 |
| 244 |
$fileNotFound = [System.ComponentModel.Win32Exception]::new(2).Message |
| 245 |
if ($_.Exception.Message -match ".*$([Regex]::Escape($fileNotFound))\.?`$") { |
| 246 |
$failMsg += " Check domain_ou_path is pointing to a valid OU in the target domain." |
| 247 |
} |
| 248 |
|
| 249 |
$module.FailJson($failMsg, $_) |
| 250 |
} |
| 251 |
|
| 252 |
$module.Result.changed = $true |
| 253 |
$module.Result.reboot_required = $true |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
else { |
| 258 |
if ($workgroupName -ne $currentState.WorkgroupName) { |
| 259 |
if ($currentState.PartOfDomain) { |
| 260 |
$removeParams = @{ |
| 261 |
UnjoinDomainCredential = $domainCredential |
| 262 |
Workgroup = $workgroupName |
| 263 |
Force = $true |
| 264 |
WhatIf = $module.CheckMode |
| 265 |
} |
| 266 |
|
| 267 |
Remove-Computer @removeParams |
| 268 |
} |
| 269 |
elseif (-not $module.CheckMode) { |
| 270 |
try { |
| 271 |
$res = Get-CimInstance Win32_ComputerSystem | Invoke-CimMethod -MethodName JoinDomainOrWorkgroup -Arguments @{ |
| 272 |
Name = $workgroupName |
| 273 |
} |
| 274 |
} |
| 275 |
catch { |
| 276 |
$module.FailJson("Failed to set workgroup as '$workgroupName': $($_.Exception.Message)", $_) |
| 277 |
} |
| 278 |
|
| 279 |
if ($res.ReturnValue -ne 0) { |
| 280 |
$msg = [System.ComponentModel.Win32Exception]$res.ReturnValue |
| 281 |
$module.FailJson("Failed to set workgroup as '$workgroupName', return value: $($res.ReturnValue): $msg") |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
$module.Result.changed = $true |
| 286 |
$module.Result.reboot_required = $true |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
if ($hostname -ne $currentState.Hostname) { |
| 291 |
$renameParams = @{ |
| 292 |
DomainCredential = $domainCredential |
| 293 |
NewName = $hostname |
| 294 |
WhatIf = $module.CheckMode |
| 295 |
Force = $true |
| 296 |
} |
| 297 |
Rename-Computer @renameParams |
| 298 |
|
| 299 |
$module.Result.changed = $true |
| 300 |
$module.Result.reboot_required = $true |
| 301 |
} |
| 302 |
|
| 303 |
$module.Diff.after = @{ |
| 304 |
dns_domain_name = $dnsDomainName |
| 305 |
hostname = $hostname |
| 306 |
state = $state |
| 307 |
workgroup_name = $workgroupName |
| 308 |
} |
| 309 |
|
| 310 |
$module.ExitJson() |