2010-03-17 4 views
1

Je souhaite créer un fichier CSV des utilisateurs sur nos serveurs Exchange 2003 et inclure certains attributs de leur compte AD. En particulier, je voudrais tirer certaines valeurs AD pour les utilisateurs avec RecipientTypeDetails = LegacyMailbox.Obtenir les attributs Active Directory pour les utilisateurs sur les serveurs Exchange hérités

J'ai essayé quelques méthodes différentes pour le ciblage et le filtrage (ldapfilter, filter, objectAttribute, etc.) ces utilisateurs, avec peu de succès. Le PowerPack Exchange 2003 pour PowerGUI a été utile, mais les problèmes d'autorisations et l'utilisation de la classe Exchange_Mailbox ne sont pas des défis que je veux surmonter. J'ai finalement réussi à créer un script de travail, mais il est très lent. Le script que j'ai créé ci-dessous fonctionne actuellement, bien qu'il soit sur le point de prendre environ 4+ heures à compléter. Je suis à la recherche de suggestions pour améliorer l'efficacité de mon script ou obtenir ces données plus rapidement. Voici le script:

$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 

Toute aide que vous pouvez fournir sera grandement appréciée!

Répondre

0

J'ai résolu ce problème en utilisant un script que j'avais déjà créé et qui fusionne un fichier CSV avec des attributs de AD. Fondamentalement, j'ai utilisé Get-Mailbox pour générer une liste CSV de tous les utilisateurs d'Exchange 2003, puis utiliser cette liste comme entrée pour Get-QADuser pour extraire les attributs AD dont j'ai besoin, et ne pouvais pas tirer avec d'autres cmdlets. Les fonctions Merge-Object et Export-CSV ont été trouvées sur Internet par d'autres utilisateurs, deux fonctions très pratiques. Voici une copie du script:

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" 
Questions connexes