0

I am having a lot of trouble getting this code to work in a TS environment.

From a Windows environment, it works great. Just about any variation you will find with a google search of this code works fine in Windows. However in a Task Sequence these variations just produce different error messages for why the computer can't be moved.

  1. Unspecified Error
  2. The specified domain either doesn't exist or couldn't be contacted
  3. Instance of an object not set to an object

I feel that something is happening when we get to the psbase.MoveTo() method call. Up to that point, it can print something out that looks like a reasonable object. In other words, they aren't null or something.

Then psbase.MoveTo() says no.

Example code.

$logFile = "MoveComputerLog.txt"

# Domain Credentials 
$account = "domain\osdaccount"
$password = "thepassword"

function logMessage {
    param ([string]$logstring)

    Write-Host $logstring

    Add-content $logFile -value $logstring
}


$computerName = "COMPUTERNAME"


logMessage "computerName: $computerName"

$root = "LDAP://sweet.domain.com"
$domain = New-Object System.DirectoryServices.DirectoryEntry($root, $account, $password)
$search = New-Object System.DirectoryServices.DirectorySearcher($domain)

$search.filter = "(&(objectClass=computer)(name=$computerName))"

$result = $search.findall() 


$computerDN = $result.Properties.Item("DistinguishedName") 

logMessage "DN: $computerDn"


$computer = New-Object System.DirectoryServices.DirectoryEntry("LDAP://$computerDN", $account, $password)

logMessage "Computer: $computer"


$destination = "LDAP://ou=here,ou=goes,ou=it,dc=sweet,dc=domain,dc=com"
$ou = New-Object System.DirectoryServices.DirectoryEntry($destination, $account, $password) 


try {
    # "The specified domain couldn't be connected or doesn't exist."
    $computer.psbase.MoveTo($ou.Path)

} catch {
    Write-Host "Encountered error while moving $computerName"

    logMessage $error[0]
}
Regolith
  • 2,944
  • 9
  • 33
  • 50
jgnovak-dev
  • 495
  • 3
  • 11

1 Answers1

0

Here is the definition, ADSI is important:

PS C:\> $computer.psbase.MoveTo

OverloadDefinitions
-------------------
void MoveTo(adsi newParent)
void MoveTo(adsi newParent, string newName)  

You can try this, rigth after $result = $search.findall() :

$computer = [ADSI]$result.path
$computer.psbase.Moveto( [ADSI]LDAP://ou=here,ou=goes,ou=it,dc=sweet,dc=domain,dc=com )
  • Thank you, but getting same error. The code I posted in my question works in Windows. In a PE environment, something is different though. – jgnovak-dev Nov 03 '17 at 12:07