-----提供AD\Exchange\Lync\Sharepoint\CRM\SC\O365等微软产品实施及外包,QQ:185426445.电话18666943750ide
需求:找出每一个AD用户所对应的OU所在位置
ui
步骤1:2008R2的AD域环境,命令以下spa
Import-Module activedirectory3d
$user=Get-ADUser -Filter * -Properties * -SearchBase "dc=XXX,dc=com" |select -ExpandProperty samaccountnameblog
foreach ($i in $user) `图片
{字符串
$a=Get-ADUser $i -Properties * |% {$_.CanonicalName}get
$b=$a.Split("/")字符串处理
Get-ADUser $i -Properties * |select samaccountname,@{n="OU";e={$b[$b.count-2]}},CanonicalName 产品
}
或者:
Import-Module activedirectory
$user=Get-ADUser -Filter * -Properties * -SearchBase "dc=XXX,dc=com" |select -ExpandProperty samaccountname
foreach ($i in $user) `
{
$a=Get-ADUser $i -Properties * |% {$_.CanonicalName}
$b=$a.Split("/")
Get-ADUser $i -Properties * |select samaccountname,@{n="OU";e={$b[-2]}},CanonicalName
}
以下图:
步骤二、2012/2012R2的AD域环境,命令以下
Get-ADUser -Filter * -Properties * -SearchBase "dc=XXX,dc=com,dc=cn" |select samaccountname,@{n="path";e={$a=$_.CanonicalName -split "/";$a[-2]}},CanonicalName
以下图:
步骤三、找出每一个AD帐号对应的具体路径,命令以下
$user=Get-ADUser -Filter * -Properties * -SearchBase "dc=XXX,dc=com,dc=cn" |select -ExpandProperty samaccountname
foreach ($i in $user) `
{ `
$a=Get-ADUser $i -Properties * |% {$_.DistinguishedName}
$b=$a -split ","
$c=""
for ($j=1;$j -le $b.Count-2;$j++) {$c+=$b[$j]+","}
$c=$c+$b[-1]
Get-ADUser $i -Properties * |select Samaccountname,@{n="Path";e={$c}},DistinguishedName
}
步骤四、字符串处理简单实例,命令以下
#去除最后一个.后面的字符串,造成新的字符串
$a="www.baidu.com" -split "\." #.加上\,转义字符的特殊处理,后面发现不加也不影响,一样的结果
$b=$c="" #$b、$c初值都赋值为空
for ($i=0; $i -le $a.Count-3 ;$i++) {$b+=$a[$i]+"."} #取倒数第二个字符,并加上.
$b=$b+$a[-2] #加上倒数第二个字符串
$b
for ($i=0; $i -le $b.Count-1 ;$i++) {$c+=$b[$i]} #因字符串数目为1,因此只能取第一个值
$c