Remove ActiveSync Device based upon DeviceUserAgent type

I have requirement, where in I need to keep devices only with 

DeviceUserAgent = RIM and WorxMail

and remove other devices in the existing ActiveSyncAlloedDevicesIDs. Not sure if the below mentioned shell commands can be used, as it is required to corrected.

Get-ActiveSyncDevice | where {$_.DeviceUserAgent -notcontains BB*, WorxMail*} Remove-ActiveSyncDevice Confirm:$True

Get-ActiveSyncDevice filter {DeviceUserAgent ne BB*, "WorxMail*" } | Remove-ActiveSyncDevice Confirm:$True

If there are any scripts which can do it please share.

May 4th, 2015 5:50am

Gautam, let me see if i understand this properly. Are you looking to keep any device that has RIM and WorxMail in their DeviceUserAgent name, and remove ALL other devices?
Free Windows Admin Tool Kit Click here and download it now
May 4th, 2015 9:27am

Yes Ross,

you are correct. The DeviceUserAgent looks like

RIM-5673.66........

WorxMailxxxxxxxx.x..x.x..xxx

so, anything i.e. RIM* and WorxMail* I would like to keep and rest all devices to be deleted.

May 4th, 2015 10:53am

Something like this should work for you, you may have to edit this based on your organization though.

Take a look and ill explain what this does...

$array = Get-ActiveSyncDevice | fl deviceuseragent

foreach ($device in $array)
{
if ($array -notcontains BB*, WorxMail*)
{
Remove-ActiveSyncDevice Confirm:$True
}
}

First this script will gather all the activesync devices in your organization and store them in a variable, and only look at the DeviceUserAgent attribute. Then it will loop through each entry and check to see if it contains BB* or WorxMail*, if it does not match this value it will run what is in the if statement, which is the Remove-ActiveSyncDevice command.

As mentioned, this can take a while depending on the size of your organization since it is gathering ALL devices using just the Get-ActiveSyncDevice cmdlet and looping through them. Let me know how this works for ya.

Free Windows Admin Tool Kit Click here and download it now
May 4th, 2015 12:55pm

Thank you Ross, instead of all the activesync devices in your organization, How can we do it for single user ? 

something similar to

Get-ActiveSyncDevice | where {$_.DeviceUserAgent -notcontains BB*, WorxMail*} Remove-ActiveSyncDevice Confirm:$True

or

Get-ActiveSyncDevice filter {DeviceUserAgent ne BB*, "WorxMail*" } | Remove-ActiveSyncDevice Confirm:$True

or anything of your own.

May 4th, 2015 1:46pm

the script errors:

 At line:4 char:24

+ if ($array -notcontains BB*, WorxMail*)

+                        ~

You must provide a value expression following the '-notcontains' operator.

At line:4 char:25

+ if ($array -notcontains BB*, WorxMail*)

+                         ~~~

Unexpected token 'BB*' in expression or statement.

At line:4 char:25

+ if ($array -notcontains BB*, WorxMail*)

+                         ~~~

Missing closing ')' after expression in 'if' statement.

At line:4 char:28

+ if ($array -notcontains BB*, WorxMail*)

+                            ~

Missing argument in parameter list.

At line:3 char:1

+ {

+ ~

Missing closing '}' in statement block.

At line:4 char:39

+ if ($array -notcontains BB*, WorxMail*)

+                                       ~

Unexpected token ')' in expression or statement.

At line:8 char:1

+ }

+ ~

Unexpected token '}' in expression or statement.

    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException

    + FullyQualifiedErrorId : ExpectedValueExpression

  


Free Windows Admin Tool Kit Click here and download it now
May 4th, 2015 1:59pm

If you would like to do it for a single user you would first have to get the mailbox stats for that user. Your bottom option is close, you just have to call the mailbox first. So it would look something like this..

$username = Read-Host "Please enter the username"

$device = Get-ActiveSyncDevice -Identity $username | fl DeviceUserAgent

if ($device -ne "BB*, WorxMail*")

{

Remove-ActiveSyncDevice $username - Confirm:$True

}

That will first get the properties of the user's DeviceUserAgent attribute, and then check against it using the comparison operator, it will then execute the code inside the brackets if the statement returns true, which in this case is saying to remove the activesync device. 

If you save this code as a .ps1 file you can run it as a script from powershell and save it for futur

May 4th, 2015 2:12pm

Sorry, i left out the quotes on the right side of the comparison operator in my post. Put quotes around BB*, WorxMail*, like this

"BB*, WorxMail*

Free Windows Admin Tool Kit Click here and download it now
May 4th, 2015 2:19pm

ok, I got it, the second script gives this error, I confirm from exchange control panel the user is valid 


 The mobile device USER-ALIAS cannot be found.

    + CategoryInfo          : NotSpecified: (:) [Get-ActiveSyncDevice], ManagementObjectNotFoundException

    + FullyQualifiedErrorId : [Server=MY-EXCHANGE-SERVER,RequestId=f04e1a74-07f4-4835-82ee-d8f60a3915ab,TimeStamp=5/4/2015 6

   :20:33 PM] [FailureCategory=Cmdlet-ManagementObjectNotFoundException] 582E983D,Microsoft.Exchange.Management.Tasks.G 

  etActiveSyncDevice

    + PSComputerName        : MY-EXCHANGE-SERVER.DOMAIN.COM

 

A positional parameter cannot be found that accepts argument '-'.

    + CategoryInfo          : InvalidArgument: (:) [Remove-ActiveSyncDevice], ParameterBindingException

    + FullyQualifiedErrorId : PositionalParameterNotFound,Remove-ActiveSyncDevice

    + PSComputerName        : MY-EXCHANGE-SERVER.DOMAIN.COM 

May 4th, 2015 2:23pm

Attach a screen shot of your console throwing the error please.
Free Windows Admin Tool Kit Click here and download it now
May 4th, 2015 2:29pm

May 4th, 2015 11:40pm

Ok Gautam, thanks for that, i see what's happening here in your environment. Try this instead..

$email = Read-Host "Please enter the user's email address"

$device = Get-ActiveSyncDevice -mailbox $email | fl DeviceUserAgent

foreach ($item in $device)

{

if ($device -ne "BB*, WorxMail*")

{

Remove-ActiveSyncDevice $email - Confirm:$True

}

}

I added a few things for you. So you need to remember that each user could have more than one device used for activesync, for example someone could have an iPhone along with an iPad, thus creating two entries in their DeviceUserAgent field. So now what this script will do is capture all (if more than one) devices listed in the DeviceUserAgent field, store them in an array, and loop through them to see if it meets your criteria, which in this case is BB*, WorxMail*

The other thing i changed is the positional parameter next to the Get-ActiveSyncDevice cmdlet, instead of using the -Identity switch it will now use the -Mailbox switch, so just enter the users exact email address when prompted and that should find it for you. 

Let me know how this works for ya.

Free Windows Admin Tool Kit Click here and download it now
May 5th, 2015 8:11am

Thanks Ross for your contentious efforts, Unfortunately the script still errors out

aPS D:\> $email = Read-Host "Please enter the user's email address"

$device = Get-ActiveSyncDevice -mailbox $email | fl DeviceUserAgent

foreach ($item in $device)

{

if ($device -ne "BB*, WorxMail*")

{

Remove-ActiveSyncDevice $email - Confirm:$True

}

}

Please enter the user's email address: emailAddress@domain.com

WARNING: The Get-ActiveSyncDevice cmdlet will be removed in a future version of Exchange. Use the Get-MobileDevice cmdlet

instead. If you have any scripts that use the Get-ActiveSyncDevice cmdlet, update them to use the Get-MobileDevice cmdlet.

  For more information, see http://go.microsoft.com/fwlink/p/?LinkId=254711.

WARNING: The Get-ActiveSyncDevice cmdlet will be removed in a future version of Exchange. Use the Get-MobileDevice cmdlet

instead. If you have any scripts that use the Get-ActiveSyncDevice cmdlet, update them to use the Get-MobileDevice cmdlet.

  For more information, see http://go.microsoft.com/fwlink/p/?LinkId=254711.

A positional parameter cannot be found that accepts argument '-'.

    + CategoryInfo          : InvalidArgument: (:) [Remove-ActiveSyncDevice], ParameterBindingException

    + FullyQualifiedErrorId : PositionalParameterNotFound,Remove-ActiveSyncDevice

    + PSComputerName        : serverFQDN

A positional parameter cannot be found that accepts argument '-'.

    + CategoryInfo          : InvalidArgument: (:) [Remove-ActiveSyncDevice], ParameterBindingException

    + FullyQualifiedErrorId : PositionalParameterNotFound,Remove-ActiveSyncDevice

    + PSComputerName        : serverFQDN

A positional parameter cannot be found that accepts argument '-'.

    + CategoryInfo          : InvalidArgument: (:) [Remove-ActiveSyncDevice], ParameterBindingException

    + FullyQualifiedErrorId : PositionalParameterNotFound,Remove-ActiveSyncDevice

    + PSComputerName        : serverFQDN

A positional parameter cannot be found that accepts argument '-'.

    + CategoryInfo          : InvalidArgument: (:) [Remove-ActiveSyncDevice], ParameterBindingException

    + FullyQualifiedErrorId : PositionalParameterNotFound,Remove-ActiveSyncDevice

    + PSComputerName        : serverFQDN

A positional parameter cannot be found that accepts argument '-'.

    + CategoryInfo          : InvalidArgument: (:) [Remove-ActiveSyncDevice], ParameterBindingException

    + FullyQualifiedErrorId : PositionalParameterNotFound,Remove-ActiveSyncDevice

    + PSComputerName        : serverFQDN

A positional parameter cannot be found that accepts argument '-'.

    + CategoryInfo          : InvalidArgument: (:) [Remove-ActiveSyncDevice], ParameterBindingException

    + FullyQualifiedErrorId : PositionalParameterNotFound,Remove-ActiveSyncDevice

    + PSComputerName        : serverFQDN


May 5th, 2015 8:22am

Gautam, it looks like its just because of the way the forums formatted my copy/paste. Take a look at the switch after the Remove-ActiveSyncDevice, there is a space after the - Confirm:$True, remove the space so it reads as follows -Confirm:$True

You can also see this in the error, you will need to have some powershell knowledge in order to run this script, if it errors out on you again, actually take a look at the error message to see if you can decipher what the error is, if its a problem with the code i can certianly help, but if you see its just a small error like this you should be able to fix it yourself. Let me know how this works for ya,

Regards

Free Windows Admin Tool Kit Click here and download it now
May 5th, 2015 8:32am

May 5th, 2015 8:58am

Are you putting in the user's email address in the format of mail@mydomain.com??

Try just running the Get-ActiveSyncDevice command to find the user's mailbox like this...

Get-ActiveSyncDevice -Mailbox email@mydomain.com | fl DeviceUserAgent

when that command returns a value than you know your using the correct email address for that user, the code looks correct, it just looks like you are having issues with finding the mailbox based off the email address you are using.

Free Windows Admin Tool Kit Click here and download it now
May 5th, 2015 9:06am

Ross whats your email, drop an email to me on gautam200
May 5th, 2015 9:45am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics