使用“0”参数调用 "FindOne" 的异常:“指定的目录服务属性或值不存在

Exception calling "FindOne" with "0" argument(s): "The specified directory service attribute or value does not exist

我正在尝试在调用 session 中验证远程计算机的 OU。该代码在脚本块之外工作,但是当 运行 在远程计算机上的脚本块内时会出现故障... o_O

这个有效:

$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 = "(&(objectClass=computer)(name=$ComputerName))" 
[System.DirectoryServices.SearchResult]$result = $searcher.FindOne() 
$dn = $result.Properties["distinguishedName"] 
$ouResult = $dn.Substring($ComputerName.Length + 4) 

$ouResult 

这不是(returns 标题错误)

#$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 = "(&(objectClass=computer)(name=$ComputerName))" 
[System.DirectoryServices.SearchResult]$result = $searcher.FindOne() 
$dn = $result.Properties["distinguishedName"] 
$ouResult = $dn.Substring($ComputerName.Length + 4) 

$ouResult 
}

在有机会仔细检查后,这是双跳问题的一个例子。当您使用 Invoke-Command 时,您无法使用 Invoke-Command 中的凭据连接到另一台服务器。但是有一些解决方法。最好的是 Resourced-Base constrained Delegation,它 Ashley McGlone has several articles on 与其他变通方法一起使用,但是如果它不起作用,您可以将您想要使用的凭据捆绑起来并将它们传递到脚本块中。

这里有一个适用于您的代码的示例:

#$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"

$Cred = (Get-Credential)
$Username = $Cred.UserName
$Password = $Cred.GetNetworkCredential().Password

Invoke-Command -ComputerName "$VMname.$Domain" -Credential $Cred -ScriptBlock { 
    $rootDse = New-Object System.DirectoryServices.DirectoryEntry("LDAP://RootDSE",$using:Username,$using:Password)
    $Domain = $rootDse.DefaultNamingContext 
    $root = New-Object System.DirectoryServices.DirectoryEntry("LDAP://$Domain",$using:Username,$using:Password) 
    $ComputerName = $env:COMPUTERNAME 
    $searcher = New-Object System.DirectoryServices.DirectorySearcher($root) 
    $searcher.Filter = "(&(objectClass=computer)(name=$ComputerName))" 
    [System.DirectoryServices.SearchResult]$result = $searcher.FindOne() 
    $dn = $result.Properties["distinguishedName"] 
    $dn.Substring($ComputerName.Length + 4) 
}

我可能还会进行其他一些清理更改。设置参数后您没有使用 VMname,所以我退出了 param($VMname)。 (请注意,如果您使用的是 PowerShell 2,则需要使用参数而不是 $using 的范围。而且我也放弃了 $ouResult,因为您可以直接输出而无需定义一个额外的步骤额外变量。