首页 > 系统服务 > 详细

Powershell管理系列(三十三)PowerShell操作之查询AD账号对应的OU存放位置

时间:2016-10-08 02:42:07      阅读:859      评论:0      收藏:0      [点我收藏+]

-----提供AD\Exchange\Lync\Sharepoint\CRM\SC\O365等微软产品实施及外包,QQ:185426445.电话18666943750


需求:找出每个AD用户所对应的OU所在位置

步骤1:2008R2的AD域环境,命令如下

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[$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   

}

如下图:

技术分享


步骤2、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 

如下图:

技术分享


步骤3、找出每个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 

}

技术分享


本文出自 “周平的微软技术交流平台” 博客,请务必保留此出处http://yuntcloud.blog.51cto.com/1173839/1859125

Powershell管理系列(三十三)PowerShell操作之查询AD账号对应的OU存放位置

原文:http://yuntcloud.blog.51cto.com/1173839/1859125

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!