Exchange Bulk import from .csv file to remove deviceID from all users from Allow and then add their deviceID back to Allow

t0kyobanana 41 Reputation points
2021-04-15T23:17:28.247+00:00

Hello Genius Peeps,

Thought maybe I could turn here for some help! Does anyone know of a Powershell script I could run on our Exchange server for to the following:

1) We would initially want to clear all or current mobile devices from all the users

2) Then re-add their specific mobile device ID that we received from them to the ALLOW list

Is there some sort of .csv and powershell script I can create to make this process fast?

We found the following scripts to remove all from ALLOW and to add to ALLOW list

Clear all Allow
Set-CASMailbox -Identity username -ActiveSyncAllowedDeviceIDs $null

Add to Allow
Set-CASMailbox -Identity username -ActiveSyncAllowedDeviceIDs @{add='DeviceId'}

And maybe after running this script if there's also a quick way to bulk all the users again to show the results were made to the accounts? Using this command? Get-CASMailbox -Identity username | fl activesync*

Exchange Exchange Server Management
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-04-19T02:54:33.7+00:00

    Hi @t0kyobanana ,

    Well actually I don't know much about powershell:( So I could only give you this based on my understanding.

    Get-CASMailbox | Set-CASMailbox -ActiveSyncAllowedDeviceIDs $null  
    ##First clean all AllowedDeviceIDs for mailboxes.  
      
    Import-Csv C:\list.csv |   
    ForEach{  
    try {  
    Set-CASMailbox -Identity $_.UserName -ActiveSyncAllowedDeviceIDs $_.DeviceId -ErrorAction Stop  
    ##Import the csv file and run Ser-CASMailbox for every value(UserName and DeviceId), when this get an error, stop it and go for the next value.  
    Write-Output "Successfully Done $_" | Out-File "C:\added.csv" -Append  
    ##Write the message if the above cmdlets are correctly done.  
    }  
    catch [System.Exception]   
    {  
    Write-Output "$_" | Out-File "c:\error.csv" -Append  
    ##When the Set-CASMailbox cmdlet run into an error(system exception), write down the erorr message and export all these to a csv file.  
    }  
    Finally  
    {  
    }}  
    

    And I think you could begin with a test 3 users.csv file but not to modify the script to do this.

    Also note to run the script with Exchange Management Shell(Admin). CD Driver:\Ps1Path and .\Name.ps1

    Best regards,
    Lou


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 additional answers

Sort by: Most helpful
  1. Anonymous
    2021-04-16T02:28:41.51+00:00

    Hi @t0kyobanana ,

    Good day!

    If you wanna set all users' AllowedDeviceIDs to null, you could use:

    Get-CASMailbox | Set-CASMailbox -ActiveSyncAllowedDeviceIDs $null  
    

    And to add the DeviceIDs for a specific user, you can run this:

     Import-Csv C:\list.csv |  
     ForEach{  
     try {  
     Set-CASMailbox -Identity $_.UserName -ActiveSyncAllowedDeviceIDs $_.DeviceId -ErrorAction Stop  
     Write-Output "Successfully Done $_" | Out-File "C:\added.csv" -Append  
     }  
     catch [System.Exception]  
     {  
     Write-Output "$_" | Out-File "c:\error.csv" -Append  
     }  
     Finally  
     {  
     }}  
    

    The list.csv should be like:
    88400-image.png
    And the result:
    88442-image.png

    For the successfully added ones:
    88463-image.png

    If there are any error, you could found the error messages from the error.txt in the C:\ folder.
    88464-image.png

    Best regards,
    Lou


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  2. Anonymous
    2021-04-16T09:00:12.047+00:00

    Hi @t0kyobanana ,

    Yeah you can simply add this cmdlet to the top line of the script:

    Get-CASMailbox | Set-CASMailbox -ActiveSyncAllowedDeviceIDs $null  
      
    Import-Csv C:\list.csv |   
    ForEach{  
    try {  
    Set-CASMailbox -Identity $_.UserName -ActiveSyncAllowedDeviceIDs $_.DeviceId -ErrorAction Stop  
    Write-Output "Successfully Done $_" | Out-File "C:\added.csv" -Append  
    }  
    catch [System.Exception]   
    {  
    Write-Output "$_" | Out-File "c:\error.csv" -Append  
    }  
    Finally  
    {  
    }}  
    

    Then it will first remove and then add.

    And sorry I don't know how you wanna the confirmed result be like, the result(done or undone) will be listed as the added.csv and error.csv. You could tell me more if that doesn't meet your requirement.

    This could add multiply ID for one user, just need your list.csv be like this(separate them with commas):
    88497-image.png
    Or the txt:
    88498-image.png

    The result will be like:
    88554-image.png

    Best regards,
    Lou


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  3. t0kyobanana 41 Reputation points
    2021-04-20T17:46:34.33+00:00

    @Anonymous The script worked flawlessly!!! Thanks SO much! :) :) :) hehe I wouldn't mind seeking answers from you again in the future.

    1 person found this answer helpful.

  4. Anonymous
    2021-04-22T01:44:35.703+00:00

    Hi @t0kyobanana ,

    The script doesn't work for multiple DeviceId users?
    As I tested, the user test, it has three Ids and it could be added to the AllowedDeviceIDs successfully.

    90152-image.png
    And you should run the cmdlet like this:

    Set-CASMailbox -Identity username -ActiveSyncAllowedDeviceIDs DeviceID1,DeviceID2  
    

    90153-image.png

    Best regards,
    Lou

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.