title: Exception calling "FindOne" with "0" argument(s): "The specified directory service attribute or value does not exist
body: <p>I am attempting to validate the OU of a remote computer in an Invoke session. The code works outside of the script block, but tanks when run within the script block on the remote machine... o_O</p>
  
  <p>This works:</p>
  
  <pre><code>$rootDse = New-Object System.DirectoryServices.DirectoryEntry("LDAP://RootDSE")
  $Domain = $rootDse.DefaultNamingContext 
  $root = New-Object System.DirectoryServices.DirectoryEntry("LDAP://$Domain") 
  $ComputerName = $env:COMPUTERNAME 
  $searcher = New-Object System.DirectoryServices.DirectorySearcher($root) 
  $searcher.Filter = "(&amp;(objectClass=computer)(name=$ComputerName))" 
  [System.DirectoryServices.SearchResult]$result = $searcher.FindOne() 
  $dn = $result.Properties["distinguishedName"] 
  $ouResult = $dn.Substring($ComputerName.Length + 4) 
  
  $ouResult 
  </code></pre>
  
  <p>And this does not (returns error in title)</p>
  
  <pre><code>#$VMname = Read-Host -Prompt "What server do you want to validate? "
  $VMname = "ObsfucatedHostNameHere"
  #$Domain = Read-Host -Prompt "What domain is it in (Please specify complete domain)? "
  $Domain = "ObsfucatedDomainNameHere.com"
  
  Invoke-Command -ComputerName "$VMname.$Domain" -Credential $Cred -ScriptBlock { param($VMname)
  
  $rootDse = New-Object System.DirectoryServices.DirectoryEntry("LDAP://RootDSE")
  $Domain = $rootDse.DefaultNamingContext 
  $root = New-Object System.DirectoryServices.DirectoryEntry("LDAP://$Domain") 
  $ComputerName = $env:COMPUTERNAME 
  $searcher = New-Object System.DirectoryServices.DirectorySearcher($root) 
  $searcher.Filter = "(&amp;(objectClass=computer)(name=$ComputerName))" 
  [System.DirectoryServices.SearchResult]$result = $searcher.FindOne() 
  $dn = $result.Properties["distinguishedName"] 
  $ouResult = $dn.Substring($ComputerName.Length + 4) 
  
  $ouResult 
  }
  </code></pre>