Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 将可分辨名称转换为SamAccountName而不使用Get ADUser_Powershell_Active Directory - Fatal编程技术网

Powershell 将可分辨名称转换为SamAccountName而不使用Get ADUser

Powershell 将可分辨名称转换为SamAccountName而不使用Get ADUser,powershell,active-directory,Powershell,Active Directory,我的机器具有PowerShell v2.0的原始版本,因此Get ADUser将无法工作。我正在尝试将manager属性从它的discriminatedname转换为它的SamAccountName $searcher=[adsisearcher](samaccountname=$env:USERNAME) $searcher.FindOne().Properties.manager 如果我有获得ADUser,这将非常有效: (get aduser(get aduser$user-Proper

我的机器具有PowerShell v2.0的原始版本,因此
Get ADUser
将无法工作。我正在尝试将
manager
属性从它的
discriminatedname
转换为它的
SamAccountName

$searcher=[adsisearcher](samaccountname=$env:USERNAME)
$searcher.FindOne().Properties.manager
如果我有
获得ADUser
,这将非常有效:

(get aduser(get aduser$user-Properties manager).manager).samaccountName


获取当前用户的
管理器
属性(可分辨名称),使用
路径名
对象将其转义,使用
[ADSI]
类型加速器将其绑定,并检索管理器的
sAMAccountName
属性。

您可以使用可分辨名称检索将为您提供
sAMAccountName的用户对象:

$searcher=[adsisearcher](samaccountname=$env:USERNAME)
$manager=[adsi]('LDAP://'+$searcher.FindOne().Properties.manager)
$manager.sAMAccountName
如果可分辨名称包含需要转义的特殊字符,您也可以使用额外的AdiseArcher:

$searcher=[adsisearcher](samaccountname=$env:USERNAME)
$manager=$searcher.FindOne().Properties.manager
$searcher2=[adsisearcher](区分名称=$manager)
$searcher2.FindOne().Properties.sAMAccountName

从远程服务器管理工具(RSAT)安装AD模块将允许您在PowerShell 2.0中使用
Get ADUser
。使用
[adsisearcher]
加速器(如图所示)通常比公认的答案更可取,因为您没有实例化不需要的com对象,它使用常规语法对AD进行ADSI搜索,而无需显式声明其他常量。非编程背景的PowerShell用户更容易访问它。
$ADS_ESCAPEDMODE_ON = 2
$ADS_SETTYPE_DN = 4
$ADS_FORMAT_X500_DN = 7
$Pathname = new-object -comobject "Pathname"
[Void] $Pathname.GetType().InvokeMember("EscapedMode", "SetProperty", $null, $Pathname, $ADS_ESCAPEDMODE_ON)

$searcher = [ADSISearcher] "(sAMAccountname=$Env:USERNAME)"
$managerDN = $searcher.FindOne().Properties["manager"]
if ( $managerDN ) {
  [Void] $Pathname.GetType().InvokeMember("Set", "InvokeMethod", $null, $Pathname, @($managerDN[0], $ADS_SETTYPE_DN))
  $escapedDN = $Pathname.GetType().InvokeMember("Retrieve", "InvokeMethod", $null, $Pathname, $ADS_FORMAT_X500_DN)
  ([ADSI] "LDAP://$escapedDN").Properties["sAMAccountName"][0]
}