【问题标题】:Export AD User Properties to CSV将 AD 用户属性导出到 CSV
【发布时间】:2014-01-23 18:27:10
【问题描述】:

我想询问客户的 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

}

【问题讨论】:

    标签: sharepoint powershell active-directory


    【解决方案1】:

    我不熟悉 directorysearcher / adsi,但如果您要迁移到 SharePoint 2013,我猜您还有一台装有 PowerShell 的计算机。 在这种情况下,如果您有 2008 DC 或 2003 带有 Active Directory Web 服务,您应该使用 Microsoft 的 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 部分是否有效,因为我之前没有使用过该属性。

    【讨论】:

    • technet.microsoft.com/en-us/magazine/gg413289.aspx 然后添加-WindowsFeature RSAT-AD-PowerShell,RSAT-AD-AdminCenter。导出的一切看起来都不错。再次感谢该语法..您为每个用户构建一个对象并管道到 Export-CSV
    • Np。在管理和导出/导入数据时,PowerShell cmdlet 是最佳选择:) 如果您认为这是正确答案,请使用答案旁边的复选标记将其标记为正确答案。 :)
    【解决方案2】:

    这样的事情应该可以工作:

    $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
    

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

    @{n='thumbnailPhoto';e={$_.<b>thumbnailphoto</b>}}

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

    【讨论】:

    • 谢谢,之前我在 AD 机器上运行过这个命令。幸运的是,通过在客户端的 SharePoint 开发机器上添加 Windows 功能,我能够按照 @FrodeF 的建议进行操作
    • 为什么我需要@{n='thumbnailPhoto';e={$_.thumbnailphoto}},定义属性名的第一位是值的下一位吗?
    • @westerdaled 是的。您需要绕道而行,因为select thumbnailphoto(或select thumbnailPhoto)不起作用。
    猜你喜欢
    • 2017-01-17
    • 2019-01-18
    • 2022-10-09
    • 1970-01-01
    • 2014-09-02
    • 1970-01-01
    • 2021-09-12
    • 2018-03-13
    • 1970-01-01
    相关资源
    最近更新 更多