Powershell 脚本用于导出 OU 中的用户的电子邮件地址

Powershell 脚本用于导出 OU 中的用户的电子邮件地址

因此,我们的 DC 上有大量 OU,它们的格式都相同,以 6 位数字开头,数字后面是名称。我想知道是否有办法编写 Powershell 脚本,在其中用 6 位数字填写 csv 文件,然后让它显示该 OU 中所有用户的电子邮件地址。

谢谢

答案1

尝试类似下面的操作(您需要调整 OrganizationalUnit 部分,以使其适合您的环境):

CLS
Write-Host "Please wait ... working ..."

$Users = Get-Mailbox -OrganizationalUnit "DC=int,DC=contoso,DC=com" -ResultSize unlimited

$OutPutFile1="C:\temp\ExportFile_pipe_separated.csv"

#Build header from export:
$OutputHeaderString = "UserAlias|UserDisplayName|UserItemCount|UserTotalItemSize (in MB)|UserLastLogonTime|Database|ServerName|OrganizationalUnit|ActiveSyncStatus|PrimarySmtpAddress"
$OutputHeaderString | Out-File $OutPutFile1 -Append
Write-Host "UserAlias|UserDisplayName|UserItemCount|UserTotalItemSize (in MB)|UserLastLogonTime|Database|ServerName|OrganizationalUnit|ActiveSyncStatus|PrimarySmtpAddress"

# Do the following for each user
foreach ($User in $Users)
{

    # Get the user's alias and display name
    $UserAlias = $User.Alias
    $UserDisplayName = $User.DisplayName
    $UsersMailDB = $User.Database
    $UsersServerName = $User.ServerName


    # Get the user's mailbox statistics on ItemCount
    $UserItemCount = Get-MailboxStatistics $UserDisplayName | Select-Object ItemCount
    $UserItemCount = $UserItemCount.ItemCount
    $UserOrgUnit = $User.OrganizationalUnit

    # Get the users active sync status
    $UserActiveSyncStatusTEMP = Get-CASMailbox -Identity $UserDisplayName | Select-Object ActiveSyncEnabled
    $UserActiveSyncStatus = $UserActiveSyncStatusTEMP.ActiveSyncEnabled

    # Get PrimarySmtpAddress
    $UserPrimarySmtpAddress = $User.PrimarySmtpAddress

    # Get the user's mailbox statistics on TotalItemSize, format to just MB / GB
    $UserTotalItemSize = Get-MailboxStatistics $UserDisplayName | Select-Object TotalItemSize
    $UserTotalItemSize = $UserTotalItemSize.TotalItemSize.Value.toMB()
    #$UserTotalItemSize = $UserTotalItemSize.TotalItemSize.Value.toGB()

    # Get the user's mailbox statistics on LastLogonTime
    $UserLastLogonTime = Get-MailboxStatistics $UserDisplayName | Select-Object LastLogonTime
    $UserLastLogonTime = $UserLastLogonTime.LastLogonTime

        # Combine results into a pipe-delimited string
        $OutputString = $UserAlias + "|" + $UserDisplayName + "|" + $UserItemCount + "|" + $UserTotalItemSize + "|" + $UserLastLogonTime + "|" + $UsersMailDB + "|" + $UsersServerName + "|" + $UserOrgUnit + "|" + $UserActiveSyncStatus + "|" + $UserPrimarySmtpAddress

        # Write the results to a .csv file
        $OutputString | Out-File $OutPutFile1 -Append

        # Display the results on the screen to show progress
        Write-Host $OutputString

}

然后,您可以将内容保存为 *.ps1 并运行它,这样就会生成一个 *.csv 文件,然后您可以在 Excel 中导入该文件并进行一些转换。

它基于来自的原始代码这里

答案2

如果数字是唯一的,那么这种方法是可行的,但并不完全满足您对 csv 输入的要求。我将把它留给您的研究部分,因为我同意 @Colyn1337 的观点。

$TheInput = Read-Host -Prompt "What are we looking for today?"
$OU = Get-ADOrganizationalUnit -Filter {Name -like $TheInput} -SearchScope Subtree
Get-ADUser -Filter * -Properties * -SearchBase $OU | Select-Object Name, mail

相关内容