Link to home
Create AccountLog in
Avatar of ahmshaik
ahmshaik

asked on

Customising Exchange Activesync Powershell script

Hi All,
We are seeing couple of activesync mailboxes getting disabled/enabled offen. This is because of users having admin access are changing adhocly.
Planning to have a script in place to enable/disable activesync and maintain a log to track of all changes with date and time.

Below is the script to enable/disable Activesync

$name=Read-Host Enter the SMTP address of the account to be ActiveSync ENABLED
$a = get-date
set-mailbox -id $name -CustomAttribute1 'Enable'
set-casmailbox -id $name -ActiveSyncEnabled $true -ActiveSyncMailboxPolicy 'Default - Default Policy + Encryption'
echo $a >> c:\temp\scripts\EAS\log.txt
get-casmailbox -id $name | ft -autosize PrimarySMTPAddress,ServerName,ActiveSyncMailboxPolicy,ActiveSyncEnabled | out-file -append c:\temp\scripts\EAS\log.txt
get-casmailbox -id $name | ft -autosize PrimarySMTPAddress,ServerName,ActiveSyncMailboxPolicy,ActiveSyncEnabled

Open in new window


But my requirement is
1. read a user to enable or disable activesync
2. Do u want to enable or disable active sync
3. If user wants to enable AS
   Then  below conditions
     if  AS is not enabled and if CA#1 is empty
         Enable ActiveSync and update CA#1 as Enable
    Else IF AS is enabled & CA#1 is empty/disable
         Inform like AS is already enabled for the user, but CA#1 is not updated/disable
         Do u want to update CA#1 -> If yes -> Update CA#1
    Else if AS is not enable & if CA#1 is updated as enable
         Inform like AS is not enabled for the user, but CA#1 is updated
         Do u want to enable Active sync
  Else
     inform both AS is enabled and CA#1 is updated.
4. If user wants to disable
     then the same logic but have to disable...
Please let me know the conditons to be added in the script and let me know if we can have some good output with
1. Admin info - who has updated the AS & CA
2. Date and time
3. User info - whose details got updated.

This will allow me to track admins who are enabling/disabling the requests and also will be very easy to manage AS.
ASKER CERTIFIED SOLUTION
Avatar of Rajitha Chimmani
Rajitha Chimmani
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of ahmshaik
ahmshaik

ASKER

Hi Rajitha,

Many thanks for developing a script for me based on my requirement. I will try to run it and get back to you if I get any errors.
Hi Rajitha,
I am getting the below error while executing the script.

Enter the SMTP address of the account to manage Active Sync: ahmed@test.com
Get-Date : Cannot bind parameter 'Date'. Cannot convert value "log.txt" to type "System.DateTime". Error: "The string was not recognized as a valid D
ateTime. There is a unknown word starting at index 0."
At C:\\Ahmed\Activesync.ps1:2 char:9
+ get-date <<<<  >> add-content log.txt
    + CategoryInfo          : InvalidArgument: (:) [Get-Date], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetDateCommand

Open in new window

change this line "get-date >> add-content log.txt" to "get-date | add-content log.txt"
Thanks for your quick response.

I am not getting any errors now. But when i enter emailaddress its accepting and script execution is completed. I believe it is not entering in any condition.

I even tried to give alias name,logon name as input but no Luck.
Try to print $Status and $CAValue within the script so that we can check if the commands work.
I got the below result.

$status = test.com/Users/ahmshaik
$cavalue = Shaik,AhmedAli (User)
what is this test.com/Users/ahmshaik? I thought you were giving email address. Try the below command individually

$Status = Get-CASMailbox -Identity "emailaddress"

I presume something wrong with the input.
 
Those variables should not be single valued. you must get a set of values.
Hi Rajitha,

I am giving email address but still getting some error.
Enter the SMTP address of the account to manage Active Sync: ahmed@test.com
Get-CASMailbox : The operation could not be performed because object 'emailaddress' could not be found on domain controlle
.
At C:\Ahmed\Activesync.ps1:3 char:25
+ $Status = Get-CASmailbox <<<<  -identity "emailaddress"
    + CategoryInfo          : InvalidData: (:) [Get-CASMailbox], ManagementObjectNotFoundException
    + FullyQualifiedErrorId : 6EB7296,Microsoft.Exchange.Management.RecipientTasks.GetCASMailbox


Shaik,AhmedAli (User)


Also just to confirm I tried running the below Cmlet and getting the below output.
I think we should get only email address here in the output.


[PS] C:\Ahmed>Get-CASMailbox -Identity ahmed@test.com

Name                       ActiveSyncEnabled OWAEnabled                 PopEnabled                ImapEnabled
----                       ----------------- ----------                 ----------                -----------
ahmshaik                    True              True                       True                      True
No... what you are getting is right. You should get the details of the CAS account.

In your first command..You must run it as below (i gave email address so that you can specify one)

$name=Read-Host "Enter the SMTP address of the account to manage Active Sync"
$Status = Get-CASmailbox -Identity $name
$Status
$CAValue = Get-Mailbox $Name
$CAValue
Hi Rajitha,

This Question is an escalated one as I have not got any response from any expert till certain time.

You have accepted my escalated query & given an awesome script for my custom requirment.

Thanks a lot.
I am feeling vey bad to give only 500 points.Wish to give u more and more points.
No problem AhmedAli and Thank You. As this was neglected question i get 200 more points..total 700 :)
Thats really gr8.

I am very happy for that.