【问题标题】:Get Active Directory Attributes for Users on Legacy Exchange Servers获取旧版 Exchange 服务器上用户的 Active Directory 属性
【发布时间】:2011-01-28 18:06:39
【问题描述】:

我想为我们的 Exchange 2003 服务器上的用户创建一个 CSV 文件,并包含他们 AD 帐户的一些属性。特别是,我想为具有 RecipientTypeDetails = LegacyMailbox 的用户提取某些 AD 值。

我尝试了几种不同的方法来定位和过滤这些用户(ldapfilter、filter、objectAttribute 等),但收效甚微。用于 PowerGUI 的 Exchange 2003 PowerPack 很有帮助,但权限问题和使用 Exchange_Mailbox 类并不是我想要克服的挑战。

我终于能够创建一个工作脚本,但它非常慢。我在下面创建的脚本目前正在运行,尽管它预计需要大约 4 个多小时才能完成。我正在寻找提高脚本效率或以其他方式更快地获取这些数据的建议。这是脚本:

$ADproperties = 'City','Company','department','Description','DistinguishedName','DisplayName','FirstName','l','LastName','msExchHomeServerName','NTAccountName','ParentContainer','physicaldeliveryofficename','SamAccountName','useraccountcontrol','UserPrincipalName'
get-user -ResultSize Unlimited -ignoredefaultscope -RecipientTypeDetails LegacyMailbox | foreach {Get-QADUser $_.name -DontUseDefaultIncludedProperties -IncludedProperties $ADproperties} | select $ADproperties | epcsv C:\UserListBuilder\exchUsers.csv -notype

您能提供的任何帮助将不胜感激!

【问题讨论】:

    标签: powershell scripting active-directory exchange-server


    【解决方案1】:

    我使用我已经创建的脚本解决了这个挑战,该脚本将 CSV 文件与 AD 中的属性合并。基本上,我使用 Get-Mailbox 生成所有 Exchange 2003 用户的 CSV 列表,然后使用该列表作为 Get-QADuser 的输入,以提取我需要的 AD 属性,并且无法与其他 cmdlet 一起提取。 Merge-Object 和 Export-CSV 功能是从 Internet 上的其他用户那里找到的,这两个功能都非常方便。以下是脚本的副本:

    Function Merge-Object($Base, $Additional)
    {
    ForEach ($Property in $($Additional | Get-Member -Type Property, NoteProperty))
    {
        $Base | Add-Member -MemberType NoteProperty -Name $Property.Name -Value $Additional.$($Property.Name) -ErrorAction SilentlyContinue
    }
    Return $Base
    }
    Function Export-CSV
    {
    [CmdletBinding(DefaultParameterSetName='Delimiter',
    SupportsShouldProcess=$true, ConfirmImpact='Medium')]
    param(
    [Parameter(Mandatory=$true, ValueFromPipeline=$true,
            ValueFromPipelineByPropertyName=$true)]
    [System.Management.Automation.PSObject]
    ${InputObject},
    
    [Parameter(Mandatory=$true, Position=0)]
    [Alias('PSPath')]
    [System.String]
    ${Path},
    
    #region -Append (added by Dmitry Sotnikov)
    [Switch]
    ${Append},
    #endregion 
    
    [Switch]
    ${Force},
    
    [Switch]
    ${NoClobber},
    
    [ValidateSet('Unicode','UTF7','UTF8','ASCII','UTF32','BigEndianUnicode','Default','OEM')]
    [System.String]
    ${Encoding},
    
    [Parameter(ParameterSetName='Delimiter', Position=1)]
    [ValidateNotNull()]
    [System.Char]
    ${Delimiter},
    
    [Parameter(ParameterSetName='UseCulture')]
    [Switch]
    ${UseCulture},
    
    [Alias('NTI')]
    [Switch]
    ${NoTypeInformation})
    
    begin
    {
    # This variable will tell us whether we actually need to append
    # to existing file
    $AppendMode = $false
    
    try {
    $outBuffer = $null
    if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
    {
        $PSBoundParameters['OutBuffer'] = 1
    }
    $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Export-Csv',
        [System.Management.Automation.CommandTypes]::Cmdlet)
    
    
        #String variable to become the target command line
        $scriptCmdPipeline = ''
    
        # Add new parameter handling
        #region Dmitry: Process and remove the Append parameter if it is present
        if ($Append) {
    
            $PSBoundParameters.Remove('Append') | Out-Null
    
    if ($Path) {
    if (Test-Path $Path) {        
        # Need to construct new command line
        $AppendMode = $true
    
        if ($Encoding.Length -eq 0) {
        # ASCII is default encoding for Export-CSV
        $Encoding = 'ASCII'
        }
    
        # For Append we use ConvertTo-CSV instead of Export
        $scriptCmdPipeline += 'ConvertTo-Csv -NoTypeInformation '
    
        # Inherit other CSV convertion parameters
        if ( $UseCulture ) {
        $scriptCmdPipeline += ' -UseCulture '
        }
        if ( $Delimiter ) {
        $scriptCmdPipeline += " -Delimiter '$Delimiter' "
        } 
    
        # Skip the first line (the one with the property names) 
        $scriptCmdPipeline += ' | Foreach-Object {$start=$true}'
        $scriptCmdPipeline += '{if ($start) {$start=$false} else {$_}} '
    
        # Add file output
        $scriptCmdPipeline += " | Out-File -FilePath '$Path' -Encoding '$Encoding' -Append "
    
        if ($Force) {
        $scriptCmdPipeline += ' -Force'
        }
    
        if ($NoClobber) {
        $scriptCmdPipeline += ' -NoClobber'
        }   
    }
    }
    } 
    
    
    
    $scriptCmd = {& $wrappedCmd @PSBoundParameters }
    
    if ( $AppendMode ) {
    # redefine command line
    $scriptCmd = $ExecutionContext.InvokeCommand.NewScriptBlock(
        $scriptCmdPipeline
        )
    } else {
    # execute Export-CSV as we got it because
    # either -Append is missing or file does not exist
    $scriptCmd = $ExecutionContext.InvokeCommand.NewScriptBlock(
        [string]$scriptCmd
        )
    }
    
    # standard pipeline initialization
    $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
    $steppablePipeline.Begin($PSCmdlet)
    
    } catch {
    throw
    }
    
    }
    
    process
    {
    try {
        $steppablePipeline.Process($_)
    } catch {
        throw
    }
    }
    
    end
    {
    try {
        $steppablePipeline.End()
    } catch {
        throw
    }
    }
    <#
    
    .ForwardHelpTargetName Export-Csv
    .ForwardHelpCategory Cmdlet
    
    #>
    }
    ###################################################################################################################################
    #   Script
    ###################################################################################################################################
    # Get Start Time
    $startDTM = (Get-Date)
    $ADproperties = 'FirstName','LastName','userAccountControl','physicaldeliveryofficename','l','City','UserPrincipalName','NTAccountName','SamAccountName','ParentContainer','Description','msExchHomeServerName'
    #$CSVdirectory = "C:\UserListBuilder\CSV\Exch2003\*.*"  # Directory containing Exchange directory export CSV files, include *.*
    $csv = "C:\UserListBuilder\CSV\Exch2003\ex03.csv"
    $Outputfilename = "C:\UserListBuilder\Exchange2003-ADInfo.csv"
    
    # Create a file of the legacy mailboxes
    Get-Mailbox -ignoredefaultscope -ResultSize 'Unlimited' | where { $_.'RecipientTypeDetails' -eq [Microsoft.Exchange.Data.Directory.Recipient.RecipientTypeDetails]'LegacyMailbox' } | select 'DisplayName','SamAccountName','UserPrincipalName' | epcsv $csv -notype
    
    $CurrentFile = Import-Csv $csv
    
    foreach($Row in $CurrentFile)
    {
    $CurrentUser = $Row.'UserPrincipalName'
    
    $CurrentUserADinfo = Get-QADUser -identity "$CurrentUser" -DontUseDefaultIncludedProperties -IncludedProperties $ADproperties | select-object $ADproperties
    Merge-Object $Row $CurrentUserADinfo
    $Row | Export-CSV -Path $Outputfilename -Append -NoTypeInformation
    }
    
    # Get End Time
    $endDTM = (Get-Date)
    
    # Echo Time elapsed
    "Elapsed Time: $(($endDTM-$startDTM).totalseconds) seconds"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多