loopspowershellexchange-serveractivesync

Exchange Powershell script looping


I have a script that I use often at work to display all active sync devices on a users account. I needed a list of Activesync enabled or disabled users, so I edited the first script. Now however, it is looping numerous times, and it loops even more times if I put -hidetableheadders. What do I have wrong in this code that is causing the loop? I will put the code I need help with first, and I will put the working code I copied from below that.

I believe the issue lies in the "foreach" block, but no matter what I change in it, even removing it, the file is empty.

Activesync enabled code:

#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

Original Code:

#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

Here is the output of the file, for an example for Matt. I have shortened the first set, but included the whole return for the second. The log I get when I run the command, returns the full list for every user in the OU from what I can tell, not just those that are in this list. Is this due to having the ou selection twice in the command?

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

I edited the command, and got this return, this is what tipped me off that it was returning the results once for every user, I just need the results once. I am including the entire result set to show, the user Sharla isn't in the returned list below it.

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

Taking Matt's advice I added back in the line about $user.samaccountname. This broke the output, so I had to and it in, but now it is giving me excatly what I needed.

#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
            }
    } 

Solution

  • This might not be the whole issue but it is certainly a large part of it

    $aSyncUser = Get-CASMailbox -organizationalunit "OU=$OU,OU=Hosted Exchange Customers,DC=ThisIsMyDC,DC=local" | where { $_.ActiveSyncEnabled -eq 'True'} | ft name, activesyncenabled -autosize
    
    1. You are not getting a single user. You are getting all users in that OU at every pass. I can't imagine that is what you were going for.
    2. Your use of Format-Table is just asking for issues down the road. Have a look at my answer on that topic here How can I store output from Format-Table for later use

    I needed a list of Activesync enabled or disabled users

    So all users then? Not sure why you search for CAS-Mailboxes outside the loop to search again in a different OU either inside your loop.