Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DSC_Computer] Problem with Delete-ADSIObject #414

Closed
rcarpenter79 opened this issue Jul 17, 2023 · 0 comments · Fixed by #415
Closed

[DSC_Computer] Problem with Delete-ADSIObject #414

rcarpenter79 opened this issue Jul 17, 2023 · 0 comments · Fixed by #415
Labels
bug The issue is a bug.

Comments

@rcarpenter79
Copy link
Contributor

Problem description

The constructor for 'System.DirectoryServices.DirectoryEntry' in Delete-ADSIObject is using the wrong variable.
It is using $DomainName as the first argument instead of $Path

Verbose logs

This causes the machine to be added to the domain but the rename fails when there is an existing computer object in AD.

When running the code for Delete-ADSIObject directly DeleteTree() returns an unspecified error has occurred.

DSC configuration

# 'ComputerName' must exist in 'DomainName' for this problem to occur

Computer 'NameAndDomain'
{
    Name       = 'ComputerName'
    DomainName = 'DomainName'
    Credential = $Credential
}

Suggested solution

Replace $DomainName with $Path

function Delete-ADSIObject
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [ValidateScript( { $_ -imatch "LDAP://*" })]
        [System.String]
        $Path,

        [Parameter(Mandatory = $true)]
        [System.Management.Automation.PSCredential]
        $Credential
    )

    $params = @{
        TypeName     = 'System.DirectoryServices.DirectoryEntry'
        ArgumentList = @(
            $DomainName,
            $Credential.UserName
            $Credential.GetNetworkCredential().password
        )
        ErrorAction  = 'Stop'
    }
    $adsiObj = New-Object @params

    $adsiObj.DeleteTree()
}

Operating system the target node is running

Server 2022

PowerShell version and build the target node is running

Name                           Value
----                           -----
PSVersion                      5.1.20348.1850
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.20348.1850
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

ComputerManagementDsc version

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     9.0.0      ComputerManagementDsc
@rcarpenter79 rcarpenter79 changed the title [DSC_Computer] [DSC_Computer] Problem with Delete-ADSIObject Jul 17, 2023
rcarpenter79 pushed a commit to rcarpenter79/ComputerManagementDsc that referenced this issue Jul 17, 2023
@johlju johlju added bug The issue is a bug. in progress The issue is being actively worked on by someone. labels Jul 17, 2023
@johlju johlju removed the in progress The issue is being actively worked on by someone. label Jul 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug The issue is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants