| 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 |
#AnsibleRequires -PowerShell Ansible.ModuleUtils.AddType |
| 8 |
|
| 9 |
$spec = @{ |
| 10 |
options = @{ |
| 11 |
blob_path = @{ |
| 12 |
type = 'str' |
| 13 |
} |
| 14 |
domain_server = @{ |
| 15 |
type = 'str' |
| 16 |
} |
| 17 |
identity = @{ |
| 18 |
type = 'str' |
| 19 |
} |
| 20 |
name = @{ |
| 21 |
type = 'str' |
| 22 |
} |
| 23 |
path = @{ |
| 24 |
type = 'str' |
| 25 |
} |
| 26 |
provision_root_ca_certs = @{ |
| 27 |
type = 'bool' |
| 28 |
default = $false |
| 29 |
} |
| 30 |
} |
| 31 |
mutually_exclusive = @( |
| 32 |
, @('identity', 'name') |
| 33 |
) |
| 34 |
required_one_of = @( |
| 35 |
, @('identity', 'name') |
| 36 |
) |
| 37 |
supports_check_mode = $true |
| 38 |
} |
| 39 |
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec) |
| 40 |
|
| 41 |
$adParams = @{} |
| 42 |
if ($module.Params.domain_server) { |
| 43 |
$adParams.Server = $module.Params.domain_server |
| 44 |
} |
| 45 |
|
| 46 |
$module.Result.blob = $null |
| 47 |
|
| 48 |
Add-CSharpType -AnsibleModule $module -References @' |
| 49 |
using System; |
| 50 |
using System.ComponentModel; |
| 51 |
using System.Runtime.InteropServices; |
| 52 |
|
| 53 |
namespace microsoft.ad.domain_join |
| 54 |
{ |
| 55 |
[StructLayout(LayoutKind.Sequential)] |
| 56 |
internal struct NETSETUP_PROVISIONING_PARAMS |
| 57 |
{ |
| 58 |
internal const int NETSETUP_PROVISIONING_PARAMS_CURRENT_VERSION = 0x00000001; |
| 59 |
|
| 60 |
public int dwVersion; |
| 61 |
[MarshalAs(UnmanagedType.LPWStr)] |
| 62 |
public string lpDomain; |
| 63 |
[MarshalAs(UnmanagedType.LPWStr)] |
| 64 |
public string lpHostName; |
| 65 |
[MarshalAs(UnmanagedType.LPWStr)] |
| 66 |
public string lpMachineAccountOU; |
| 67 |
[MarshalAs(UnmanagedType.LPWStr)] |
| 68 |
public string lpDCName; |
| 69 |
public int dwProvisionOptions; |
| 70 |
public IntPtr aCertTemplateNames; |
| 71 |
public int cCertTemplateNames; |
| 72 |
public IntPtr aMachinePolicyNames; |
| 73 |
public int cMachinePolicyNames; |
| 74 |
public IntPtr aMachinePolicyPaths; |
| 75 |
public int cMachinePolicyPaths; |
| 76 |
public IntPtr lpNetbiosName; |
| 77 |
public IntPtr lpSiteName; |
| 78 |
public IntPtr lpPrimaryDNSDomain; |
| 79 |
} |
| 80 |
|
| 81 |
[Flags] |
| 82 |
public enum ProvisionOptions |
| 83 |
{ |
| 84 |
NETSETUP_PROVISION_DOWNLEVEL_PRIV_SUPPORT = 0x00000001, |
| 85 |
NETSETUP_PROVISION_REUSE_ACCOUNT = 0x00000002, |
| 86 |
NETSETUP_PROVISION_USE_DEFAULT_PASSWORD = 0x00000004, |
| 87 |
NETSETUP_PROVISION_SKIP_ACCOUNT_SEARCH = 0x00000008, |
| 88 |
NETSETUP_PROVISION_ROOT_CA_CERTS = 0x00000010, |
| 89 |
NETSETUP_PROVISION_PERSISTENTSITE = 0x00000020, |
| 90 |
} |
| 91 |
|
| 92 |
public static class Native |
| 93 |
{ |
| 94 |
[DllImport("Netapi32.dll", EntryPoint = "NetCreateProvisioningPackage")] |
| 95 |
private static extern int NativeNetCreateProvisioningPackage( |
| 96 |
ref NETSETUP_PROVISIONING_PARAMS pProvisioningParams, |
| 97 |
ref IntPtr ppPackageBinData, |
| 98 |
out int ppdwPackageBinDataSize, |
| 99 |
IntPtr ppPackageTextData); |
| 100 |
|
| 101 |
public static byte[] NetCreateProvisioningPackage(string domain, string hostName, string machineAccountOU, |
| 102 |
string dcName, ProvisionOptions options) |
| 103 |
{ |
| 104 |
domain = String.IsNullOrWhiteSpace(domain) ? null : domain; |
| 105 |
hostName = String.IsNullOrWhiteSpace(hostName) ? null : hostName; |
| 106 |
machineAccountOU = String.IsNullOrWhiteSpace(machineAccountOU) ? null : machineAccountOU; |
| 107 |
dcName = String.IsNullOrWhiteSpace(dcName) ? null : dcName; |
| 108 |
|
| 109 |
NETSETUP_PROVISIONING_PARAMS p = new NETSETUP_PROVISIONING_PARAMS() |
| 110 |
{ |
| 111 |
dwVersion = NETSETUP_PROVISIONING_PARAMS.NETSETUP_PROVISIONING_PARAMS_CURRENT_VERSION, |
| 112 |
lpDomain = domain, |
| 113 |
lpHostName = hostName, |
| 114 |
lpMachineAccountOU = machineAccountOU, |
| 115 |
lpDCName = dcName, |
| 116 |
dwProvisionOptions = (int)options, |
| 117 |
}; |
| 118 |
|
| 119 |
IntPtr outBuffer = IntPtr.Zero; |
| 120 |
int outBufferLength = 0; |
| 121 |
int res = NativeNetCreateProvisioningPackage(ref p, ref outBuffer, out outBufferLength, IntPtr.Zero); |
| 122 |
if (res != 0) |
| 123 |
{ |
| 124 |
throw new Win32Exception(res); |
| 125 |
} |
| 126 |
|
| 127 |
byte[] data = new byte[outBufferLength]; |
| 128 |
Marshal.Copy(outBuffer, data, 0, data.Length); |
| 129 |
|
| 130 |
return data; |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
'@ |
| 135 |
|
| 136 |
$identity = if ($module.Params.identity) { |
| 137 |
$module.Params.identity |
| 138 |
} |
| 139 |
else { |
| 140 |
$path = $module.Params.path |
| 141 |
if (-not $path) { |
| 142 |
$GUID_COMPUTERS_CONTAINER_W = 'AA312825768811D1ADED00C04FD8D5CD' |
| 143 |
$defaultNamingContext = (Get-ADRootDSE @adParams -Properties defaultNamingContext).defaultNamingContext |
| 144 |
|
| 145 |
$path = Get-ADObject @ADParams -Identity $defaultNamingContext -Properties wellKnownObjects | |
| 146 |
Select-Object -ExpandProperty wellKnownObjects | |
| 147 |
Where-Object { $_.StartsWith("B:32:$($GUID_COMPUTERS_CONTAINER_W):") } | |
| 148 |
ForEach-Object Substring 38 |
| 149 |
} |
| 150 |
|
| 151 |
"CN=$($Module.Params.name -replace ',', '\,'),$path" |
| 152 |
} |
| 153 |
|
| 154 |
try { |
| 155 |
$computer = Get-ADComputer -Identity $identity @adParams |
| 156 |
} |
| 157 |
catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] { |
| 158 |
$msg = "Failed to find domain computer account '$identity': $($_.Exception.Message)" |
| 159 |
$module.FailJson($msg, $_) |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
# The name expected by NetCreateProvisioningPackage is the sAMAccountName but |
| 164 |
# without the trailing $. |
| 165 |
$computerName = $computer.SamAccountName.Substring(0, |
| 166 |
$computer.SamAccountName.Length - 1) |
| 167 |
$computerPath = @($computerName.DistinguishedName -split '[^\\],', 2)[-1] |
| 168 |
|
| 169 |
$flags = [microsoft.ad.domain_join.ProvisionOptions]::NETSETUP_PROVISION_REUSE_ACCOUNT |
| 170 |
if ($module.Params.provision_root_ca_certs) { |
| 171 |
$flags = $flags -bor [microsoft.ad.domain_join.ProvisionOptions]::NETSETUP_PROVISION_ROOT_CA_CERTS |
| 172 |
} |
| 173 |
|
| 174 |
$domainInfo = Get-ADDomain @adParams |
| 175 |
|
| 176 |
if ($module.Params.blob_path -and (Test-Path -LiteralPath $module.Params.blob_path)) { |
| 177 |
$module.ExitJson() |
| 178 |
} |
| 179 |
|
| 180 |
if (-not $Module.CheckMode) { |
| 181 |
$blob = [microsoft.ad.domain_join.Native]::NetCreateProvisioningPackage( |
| 182 |
$domainInfo.DNSRoot, |
| 183 |
$computerName, |
| 184 |
$computerPath, |
| 185 |
$adParams.Server, |
| 186 |
$flags) |
| 187 |
} |
| 188 |
else { |
| 189 |
$blob = New-Object -TypeName System.Byte[] -ArgumentList 0 |
| 190 |
} |
| 191 |
|
| 192 |
$module.Result.changed = $true |
| 193 |
|
| 194 |
if ($module.Params.blob_path) { |
| 195 |
if (-not $Module.CheckMode) { |
| 196 |
[System.IO.File]::WriteAllBytes($module.Params.blob_path, $blob) |
| 197 |
} |
| 198 |
} |
| 199 |
else { |
| 200 |
$module.Result.blob = [System.Convert]::ToBase64String($blob) |
| 201 |
} |
| 202 |
|
| 203 |
$module.ExitJson() |