使用 powershell 的 export-csv 导出特定字段

Tim*_*im 0 powershell active-directory

我正在尝试使用 Powershell 从 Active Directory 2003 导出一些用户信息的列表。

到目前为止,我可以使用以下内容显示特定的属性列表:

if ( (Get-PSSnapin -Name Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue) -eq $null )
{
    Add-PsSnapin Quest.ActiveRoles.ADManagement
}

get-qaduser -company "Company","Consultant" `
    -enabled -DontUseDefaultIncludedProperties -IncludedProperties 'givenName','sn','telephoneNumber','mail','company' `
    | sort-object `
    | format-table givenname,sn,company,telephonenumber,mail `
Run Code Online (Sandbox Code Playgroud)

但是,如果我尝试将输出通过管道传输到 export-csv,而不是 format-table,我将获得每个域对象的所有属性。

如果我执行以下操作:

    | format-table givenname,sn,company,telephonenumber,mail `
    | export-csv -Delimiter `t -NoTypeInformation -Path c:\scripts\adexport.csv
Run Code Online (Sandbox Code Playgroud)

我得到了没有数据的正确行数,除了一些标识符(这是有道理的 - 因为对象没有被传递到 export-csv,只是一行文本)。

如何将特定字段导出到文件?

我正在使用 Quest 的 ActiveDirectory powershell CMDLET ( http://www.quest.com/powershell/activeroles-server.aspx ) 从 AD 获取数据。

Mat*_*ttB 8

您想使用Select-Objectcmdlet 代替Format-Table. 这应该适合你:

| select-object givenname,sn,company,telephonenumber,mail `
| export-csv -Delimiter `t -NoTypeInformation -Path c:\scripts\adexport.csv
Run Code Online (Sandbox Code Playgroud)