Exchange Powershell脚本循环

时间:2016-12-02 21:36:59

标签: loops powershell exchange-server activesync

我有一个脚本,我经常在工作中使用该脚本来显示用户帐户上的所有活动同步设备。我需要一个启用或禁用Activesync的用户列表,因此我编辑了第一个脚本。然而,现在,它循环多次,如果我放置-hidetableheadders,它会循环更多次。我在这个导致循环的代码中有什么问题?我将首先提供我需要帮助的代码,然后我将把我从下面复制的工作代码放入其中。

我认为问题在于“foreach”块,但无论我改变它,即使删除它,文件也是空的。

启用Activesync的代码:

#Settings for file ouput
$fLocation = "D:\Exchange Reports\"

#Read OU imput from console:
$OU = Read-Host -Prompt "Input the OU name to search: (0202 - Dev Bank)"

#lookup users based on the OU
$Ulist = Get-CASMailbox -organizationalunit "OU=$OU,OU=Hosted Exchange Customers,DC=CSIDMZ,DC=local" -ResultSize Unlimited


foreach ($user in $Ulist)
    {
        $aSyncUser = Get-CASMailbox -organizationalunit "OU=$OU,OU=Hosted Exchange Customers,DC=ThisIsMyDC,DC=local" | where { $_.ActiveSyncEnabled -eq 'True'} | ft name, activesyncenabled -autosize

        if ($aSyncUser -ne $null)
            {
                $deviceID = $aSyncUser | out-string
                $Content = "User: $user $deviceID"
                Add-Content $fName $Content
            }

    }

    write-host "The script completed successfully! The output file can be found at $fName" -ForeGroundColor Yellow

原始代码:

#Settings for file ouput
$fLocation = "D:\Exchange Reports\"

#Read OU imput from console:
$OU = Read-Host -Prompt "Input the OU name to search: (0202 - Dev Bank)"

#lookup users based on the OU
$Ulist = get-mailbox -OrganizationalUnit "OU=$OU,OU=Hosted Exchange Customers,DC=ThisIsMyDC,DC=local" -ResultSize Unlimited


foreach ($user in $Ulist)
    {
        $aSyncUser = get-activesyncdevice -mailbox $user.SAMAccountName -ErrorAction SilentlyContinue |ft DeviceID -HideTableHeaders

        if ($aSyncUser -ne $null)
            {
                $deviceID = $aSyncUser | out-string
                $Content = "User: $user $deviceID"
                Add-Content $fName $Content
            }
    }

    write-host "The script completed successfully! The output file can be found at $fName" -ForeGroundColor Yellow

这是文件的输出,以Matt为例。我缩短了第一组,但包括第二组的全部回报。我运行命令时得到的日志,从我可以告诉的内容中返回OU中每个用户的完整列表,而不仅仅是列表中的那些用户。这是因为在命令中选择了两次?

User: Microsoft.PowerShell.Commands.Internal.Format.FormatStartData 
Name             ActiveSyncEnabled
----             -----------------
Judy X Langford               True

User: Microsoft.PowerShell.Commands.Internal.Format.GroupStartData 
Name             ActiveSyncEnabled
----             -----------------
Judy X Langford               True

我编辑了命令,得到了这个回报,这就是让我知道它为每个用户返回一次结果的原因,我只需要一次结果。我包括要显示的整个结果集,用户Sharla不在它下面的返回列表中。

User: Domain.local/Hosted Exchange Customers/This is my OU/Sharla x Ryan
Name             ActiveSyncEnabled
----             -----------------
Judy X Langford               True
Sandy X Stuckey               True
0718 test                     True
Shelby D Hansche              True
Toni X Brooks                 True
Katie X Strain                True
Monica Minter                 True
Chloe X Mims                  True
Jay X Davis                   True
Aaron Calvit                  True
Joe Nichols                   True
Sherry X Rice                 True
Tracey X Wardlaw              True
Brad X Davis                  True
Chris X Lannom                True
Haley X Burleson              True
Cody X Deal                   True
Cris X Day                    True
Mitch X Stone                 True



User: Domain.local/Hosted Exchange Customers/This is my OU/Judy X Langford
Name             ActiveSyncEnabled
----             -----------------
Judy X Langford               True

根据Matt的建议,我在$ user.samaccountname中添加了回复。这打破了输出,所以我必须和它一起,但现在它给了我我需要的东西。

#lookup users based on the OU

Get-CASMailbox -OrganizationalUnit "OU=$OU,OU=Hosted Exchange Customers,DC=CSIDMZ,DC=local" -ResultSize unlimited | select name,activesyncenabled >>$fLocation+$OU+"_ActiveSyncEnabled.txt"

foreach ($user in $Ulist)
    {
        $aSyncUser = get-activesyncdevice -mailbox $user.SAMAccountName -ErrorAction SilentlyContinue |ft name, activesyncenabled -HideTableHeaders 

        if ($aSyncUser -ne $null)
            {
                $deviceID = $aSyncUser | out-string
                $Content = "User: $user $deviceID"
                Add-Content $fName $Content
            }
    } 

1 个答案:

答案 0 :(得分:0)

这可能不是整个问题,但肯定是它的很大一部分

$aSyncUser = Get-CASMailbox -organizationalunit "OU=$OU,OU=Hosted Exchange Customers,DC=ThisIsMyDC,DC=local" | where { $_.ActiveSyncEnabled -eq 'True'} | ft name, activesyncenabled -autosize
  1. 您没有获得单个用户。每次通过时,您都会在该OU中获得所有用户。我无法想象这就是你想要的。
  2. 您对Format-Table的使用只是要求解决问题。请在How can I store output from Format-Table for later use
  3. 查看我对该主题的回答
      

    我需要启用或禁用Activesync的用户列表

    那么所有用户呢?不确定为什么要在循环外搜索CAS-Mailbox,以便在循环内的其他OU中再次搜索。