Skip to content

Commit

Permalink
feat(win_domain_computer,win_domain_object_info): add activedirectory…
Browse files Browse the repository at this point in the history
… module import (#509)

* feat(win_domain_computer,win_domain_object_info): add activedirectory module import

* rename changelogs fragments

* FailJson

* Import ActiveDirectory Module in PowerShell instance
  • Loading branch information
gotit96 authored May 1, 2023
1 parent 678da20 commit 9435769
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/509-import-activedirectory-module.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
minor_changes:
- win_domain_computer - Add ActiveDirectory module import
- win_domain_object_info - Add ActiveDirectory module import
10 changes: 9 additions & 1 deletion plugins/modules/win_domain_computer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#Requires -Module Ansible.ModuleUtils.Legacy
#Requires -Module Ansible.ModuleUtils.ArgvParser
#Requires -Module Ansible.ModuleUtils.CommandUtil

#Requires -Module ActiveDirectory

# ------------------------------------------------------------------------------
$ErrorActionPreference = "Stop"
Expand Down Expand Up @@ -50,6 +50,14 @@ if ($null -ne $domain_server) {
$extra_args.Server = $domain_server
}

# attempt import of module
try {
Import-Module ActiveDirectory
}
catch {
Fail-Json -obj $result -message "The ActiveDirectory module failed to load properly: $($_.Exception.Message)"
}

If ($state -eq "present") {
$dns_hostname = Get-AnsibleParam -obj $params -name "dns_hostname" -failifempty $true -resultobj $result
$ou = Get-AnsibleParam -obj $params -name "ou" -failifempty $true -resultobj $result
Expand Down
1 change: 1 addition & 0 deletions plugins/modules/win_domain_computer.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
- When using the ODJ BLOB to join a computer to the domain, it must be written out to a file.
- The file must be UTF-16 encoded (in PowerShell this encoding is called C(Unicode)), and it must end in a null character. See examples.
- The C(djoin.exe) part of the offline domain join process will not use I(domain_server), I(domain_username), or I(domain_password).
- This must be run on a host that has the ActiveDirectory powershell module installed.
seealso:
- module: ansible.windows.win_domain
- module: ansible.windows.win_domain_controller
Expand Down
13 changes: 12 additions & 1 deletion plugins/modules/win_domain_object_info.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#AnsibleRequires -CSharpUtil Ansible.Basic
#Requires -Module Ansible.ModuleUtils.AddType
#Requires -Module ActiveDirectory

$spec = @{
options = @{
Expand Down Expand Up @@ -150,6 +151,14 @@ Function ConvertTo-OutputValue {
}
}

# attempt import of module
try {
Import-Module ActiveDirectory
}
catch {
$module.FailJson("The ActiveDirectory module failed to load properly: $($_.Exception.Message)", $_)
}

<#
Calling Get-ADObject that returns multiple objects with -Properties * will only return the properties that were set on
the first found object. To counter this problem we will first call Get-ADObject to list all the objects that match the
Expand Down Expand Up @@ -200,7 +209,9 @@ try {
# We run this in a custom PowerShell pipeline so that users of this module can't use any of the variables defined
# above in their filter. While the cmdlet won't execute sub expressions we don't want anyone implicitly relying on
# a defined variable in this module in case we ever change the name or remove it.
$ps = [PowerShell]::Create()
$iss = [InitialSessionState]::CreateDefault()
$iss.ImportPSModule("ActiveDirectory")
$ps = [PowerShell]::Create($iss)
$null = $ps.AddCommand('Get-ADObject').AddParameters($commonParams).AddParameters($getParams)
$null = $ps.AddCommand('Select-Object').AddParameter('Property', @('DistinguishedName', 'ObjectGUID'))

Expand Down
1 change: 1 addition & 0 deletions plugins/modules/win_domain_object_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
- The C(sAMAccountType_AnsibleFlags) and C(userAccountControl_AnsibleFlags) return property is something set by the
module itself as an easy way to view what those flags represent. These properties cannot be used as part of the
I(filter) or I(ldap_filter) and are automatically added if those properties were requested.
- This must be run on a host that has the ActiveDirectory powershell module installed.
author:
- Jordan Borean (@jborean93)
'''
Expand Down

0 comments on commit 9435769

Please sign in to comment.