DSCResources/xDSCDomainjoin/xDSCDomainjoin.psm1

function Get-TargetResource
{
  [CmdletBinding()]
  [OutputType([System.Collections.Hashtable])]
  param
  (
    [Parameter(Mandatory = $true)]
    [System.String]
    $Domain,

    [Parameter(Mandatory = $true)]
    [pscredential]$Credential,

    [string]$JoinOU
  )

  #Write-Verbose "Use this cmdlet to deliver information about command processing."

  #Write-Debug "Use this cmdlet to write debug information while troubleshooting."

  $convertToCimCredential = New-CimInstance -ClassName MSFT_Credential -Property @{ Username = [string]$Credential.Username; Password = [string]$null } -Namespace root/microsoft/windows/desiredstateconfiguration -ClientOnly

  $returnValue = @{
    Domain = (gwmi win32_computersystem).Domain
    JoinOU = $JoinOU
    Credential = [ciminstance]$convertToCimCredential
  }

  $returnValue
}


function Set-TargetResource
{
  [CmdletBinding()]
  param
  (
    [Parameter(Mandatory = $true)]
    [System.String]
    $Domain,

    [Parameter(Mandatory = $true)]
    [pscredential]$Credential,

    [string]$JoinOU
  )

  if ($JoinOU) {
    Add-Computer -DomainName $Domain -Credential $Credential -OUPath $JoinOU -Force -Restart
  }
  else {
    Add-Computer -DomainName $Domain -Credential $Credential -Force -Restart
  }


}


function Test-TargetResource
{
  [CmdletBinding()]
  [OutputType([System.Boolean])]
  param
  (
    [Parameter(Mandatory = $true)]
    [System.String]
    $Domain,

    [Parameter(Mandatory = $true)]
    [pscredential]$Credential,

    [string]$JoinOU
  )

  if ($Domain.ToLower() -eq (gwmi win32_computersystem).Domain) {
    return $true
    }
    else {
      return $false
    }
}

Export-ModuleMember -Function *-TargetResource