交换用户名,转发地址和帐户状态

时间:2017-06-20 16:00:09

标签: powershell active-directory exchange-server

我正在尝试从exchange powershell导出用户列表及其转发地址,以及该帐户是否已禁用或启用。我不得不使用两个命令并手动将每个转发地址复制到AD电子表格中的每个用户。有没有办法将这些命令合并为一个,所以除了删除显示ou路径的单个列之外,我不需要对它进行任何手动操作(如果它不包含在第一个位置会很好)?

交换脚本:

$fwds = get-mailbox -organizationalunit "OU=25 - Preferred Bank,OU=Hosted Exchange Customers,DC=DMZ,DC=local" -resultsize unlimited | Where-Object { $_.ForwardingAddress -ne $null } | sort Name | select Name, ForwardingAddress

foreach ($fwd in $fwds) {$fwd | add-member -membertype noteproperty -name "ContactAddress" -value (get-Recipient $fwd.ForwardingAddress).PrimarySmtpAddress}

$fwds | Export-Csv c:\Users\x\Desktop\forwards.csv -NoTypeInformation

AD脚本:

get-aduser -searchbase "OU=25 - Preferred Bank,OU=Hosted Exchange Customers,DC=DMZ,DC=local" | select name,enabled

尼克在添加-expand时会给出以下内容:

Select-Object : Cannot process argument because the value of argument "obj" is null. Change the value of argument "obj"
 to a non-null value.
At C:\Users\x\Desktop\try_2.ps1:9 char:59
+ $fwd = get-mailbox -Identity $user.samaccountname | select <<<<  -expand ForwardingAddress
    + CategoryInfo          : InvalidArgument: (:) [Select-Object], PSArgumentNullException
    + FullyQualifiedErrorId : ArgumentNull,Microsoft.PowerShell.Commands.SelectObjectCommand

2 个答案:

答案 0 :(得分:0)

这样的东西?

#Get users
$users = get-aduser -searchbase "OU=25 - Preferred Bank,OU=Hosted Exchange Customers,DC=DMZ,DC=local" | select samaccountname, name,enabled

#Loop through users to get info
foreach ($user in $users)
{

#Get the forwarding address on the mailbox
$fwd = get-mailbox -Identity $user.samaccountname | select -expand ForwardingAddress
if($fwd)
{ 

#Get the SMTP of the forwarding address if it exists
$PrimarySMTP =  (get-Recipient $fwd | select  PrimarySMTPAddress).PrimarySMTPAddress
}
else{$PrimarySMTP = ""}

#Create custom object to export CSV with all info.
    New-Object -TypeName PSCustomObject -Property @{
                User= $User.Name
                Enabled = $user.Enabled
                ForwardingAddress= $PrimarySMTP}  | Export-Csv c:\Users\x\Desktop\forwards.csv -NoTypeInformation -append
    }

答案 1 :(得分:0)

好吧,所以我最终得到了这个。我认为我的问题可能是我使用了加载广告模块的exchange powershell,而不仅仅是加载了交换和广告模块的powershell。

#Get users
$users = get-aduser -searchbase "OU=ou,OU=Hosted Exchange Customers,DC=DMZ,DC=local" -Filter * | select samaccountname, name, enabled

#Loop through users to get info
$output = foreach ($user in $users)
{
    #Get the forwarding address on the mailbox
    $fwd = (get-mailbox -Identity $user.samaccountname -ErrorAction silentlycontinue).forwardingaddress

    if($fwd)
    {
    #Get the SMTP of the forwarding address if it exists
    $PrimarySMTP =  (get-Recipient $fwd).PrimarySMTPAddress
    }
    else{$PrimarySMTP = ""}

    #Create custom object to export CSV with all info.
    New-Object -TypeName PSCustomObject -Property @{
        User= $User.Name
        Enabled = $user.Enabled
        ForwardingAddress= $PrimarySMTP}
}

#$output

$output | Export-Csv "C:\users\%username%\desktop\25_quarterly_report.csv" -NoTypeInformation