将AD用户属性导出为CSV

时间:2014-01-06 10:55:49

标签: sharepoint powershell active-directory

我想询问客户端的AD以查看哪些用户在SharePoint 2013中的用户配置文件同步之前缺少属性值(如电话号码),该脚本可以正常运行,但现在我需要添加一些“魔术”才能创建我的csv文件...我在下面添加了一条评论,表明我觉得这个“神奇”应该去哪里了!

 # get Users and groups

 #Get the User from AD
 $domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
 $root = $domain.GetDirectoryEntry()
 $search = [System.DirectoryServices.DirectorySearcher]$root
 #$search.Filter = "(&(objectCategory=User)(samAccountName=$userName))"

 # get a list of the users but not the sp_ service accounts.
 $search.Filter = "(&(objectCategory=User) (!(name=SP_*)) )"
 $search.SearchScope ="subtree"

 # determine the properties we want back

 $colPropList = "name", "jobTitle", "telephoneNumber","mail", "department"   ,  "thumbnailPhoto"
 foreach ($i in $colPropList){$search.PropertiesToLoad.Add($i)}


 $result = $search.FindAll()




 if ($result -ne $null)
 {

    foreach ( $entry in $result )
    {

       # this works though I might have incorrect names for some of the properties
       $user = $entry.Properties;
       $user.name
       $user.department
       $user.jobTitle
       $user.telephoneNumber
       $user.mail
       $user.thumbnailPhoto


       *# !!!!!!This is where I need help!!!!!             
       # as my $user is effectively an object then I should be able to to use it to create a an object with Add-Member 
       # Do I breaker down the $user properties and create another object with name values ???* 

       foreach ($o in $user) 
       {
          Add-Member -InputObject $psObject -MemberType NoteProperty -Name $o -Value $o
       } 

} 


$psObject | Export-Csv c:\dev\aduserList.csv -NoTypeInformation

}

2 个答案:

答案 0 :(得分:1)

我不熟悉directorysearcher / adsi,但如果您要迁移到SharePoint 2013,我猜你也有一台装有PowerShell的计算机。 在这种情况下,如果您拥有带有Active Directory Web服务的2008 DC或2003,则应使用Microsofts ActiveDirectory模块(安装在服务器上并通过RSAT安装到客户端)。

您也可以使用Quest ADRoles Module

PowerShell cmdlet更易于用于AD管理。然后,您可以将脚本缩短为一行(这是Microsoft的ActiveDirectory模块):

Get-ADUser -LDAPFilter "(!(name=SP_*))" -Properties Name, Title, OfficePhone, Mail, Department, thumbnailPhoto | Select-Object Name, Title, OfficePhone, Mail, Department, thumbnailPhoto | Export-Csv c:\dev\aduserList.csv -NoTypeInformation

我不确定thumbnailphoto部分是否有效,因为之前我没有使用过该属性。

答案 1 :(得分:0)

这样的事情应该有效:

$search.FindAll() | select -Expand Properties |
  select @{n='name';e={$_.name}},
         @{n='department';e={$_.department}},
         @{n='jobTitle';e={$_.jobtitle}},
         @{n='telephoneNumber';e={$_.telephonenumber}},
         @{n='mail';e={$_.mail}},
         @{n='thumbnailPhoto';e={$_.thumbnailphoto}} |
  Export-Csv c:\dev\aduserList.csv -NoTypeInformation

请注意,计算属性expression@{n='name';e={expression}}部分中使用的属性必须小写:

@{n='thumbnailPhoto';e={$_.thumbnailphoto}}

使用Get-ADUser模块中的ActiveDirectory cmdlet作为Frode F.建议是获取所需信息的更方便的方法,但它需要安装AD PowerShell模块使用它的计算机,以及在DC上安装和运行AD Web服务。