Solved

Powershell Scheduled Task does not run

Posted on 2010-11-16
16
2,224 Views
Last Modified: 2012-06-27
Running the following command to check for new mailboxes created previous day, disable the ActiveSync, IMAP4 and POP3, then enable ActiveSync if they are a member of the SG "ASAllowed". This works fine on its own (without the addPSSnapin and Set Execution policy lines) :-

#Run Daily to disable ActiveSync, POP3 and IMAP4 on all mailboxes created

#previous day on TestDB

#Adding Exchange Snap In to execute Exchange CmdLets in this script
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin

#Set execution policy to allow scripts to be run in Powershell
set-executionpolicy RemoteSigned

get-mailboxdatabase testdb |get-mailbox | Get-User -ResultSize Unlimited | Where

{($_.WhenCreated -gt (get-date).adddays(-1))} | Set-CASMailbox –

ActiveSyncEnabled:$false –POPEnabled:$false -IMAPEnabled:$False

#Run daily to re-enable ActiveSync on ALL mailboxes on TestDB which are members

#of the ASAllowed SG


#Function that enables ActiveSync for members of each group function
function EnableActiveSync { param($enableGroup)

   # Assign all members of the group to a dynamic array
   $allUsers = Get-DistributionGroupMember -Identity $enableGroup
   
   # Loop through the array
   foreach ($member in $allUsers) {

      # Set ActiveSync for each member of the array
      $member | Set-CASMailbox –ActiveSyncEnabled $true
   }
}


#DistributionGroups that allow ActiveSync
EnableActiveSync 'ASAllowed'

I have saved this as ActiveSyncTest.PS1

I have created a batch file (ASTest.bat) with the following line :-

powershell "<path>\ActiveSyncTest.PS1"

Running directly on E2K10 server, schedule the task to run and open ASTask.bat but it simply does not execute - no errors just does not run. It is running under a dom admin account, and has execute at highest level of permissions tick box.

I cannot find any log file as there used to be to check this, but history does not show the task as having run. The task shows the time today it should have run, and it never enters running state.

Anyone any ideas what could be wrong and how to troubleshoot please ?

Thanks
0
Comment
Question by:TheGeezer2010
  • 9
  • 7
16 Comments
 
LVL 27

Accepted Solution

by:
KenMcF earned 200 total points
ID: 34146893
It may be haning on this line waiting for a response
Can you comment the line out and test?

set-executionpolicy RemoteSigned

0
 
LVL 11

Author Comment

by:TheGeezer2010
ID: 34147308
OK so it now runs - it does not do what it says on the tin though !! I have increased this to 200 to investigate why the function does not re-enable ActiveSync on those members of the ASAllowed SG.

Thanks to KenMcF for fixing scheduling part, can I ask Ken why this needs to be omitted when everything I have read says you must include this to allow scripts to run ?

Thanks again
0
 
LVL 27

Assisted Solution

by:KenMcF
KenMcF earned 200 total points
ID: 34147454
You only need to set the execution policy once per server unless you need to change it for some reason.
You could also set through a regisrty key instead of through the cmdlet.

http://www.jamssupport.com/KnowledgebaseArticle50027.aspx
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 27

Expert Comment

by:KenMcF
ID: 34147548
Can you try to modify your function

function EnableActiveSync { param($enableGroup)

   # Assign all members of the group to a dynamic array
   $allUsers = Get-DistributionGroupMember -Identity $enableGroup
   
   # Loop through the array
   foreach ($member in $allUsers) {

      # Set ActiveSync for each member of the array
      get-casmailbox $member | Set-CASMailbox –ActiveSyncEnabled $true
   }
}
0
 
LVL 11

Author Comment

by:TheGeezer2010
ID: 34147722
Will try that tomorrow and thanks a million.
0
 
LVL 11

Author Comment

by:TheGeezer2010
ID: 34153427
OK tried that but it looks as if it cannot accept the get-casmailbox $member - output error is (have replaced AD details with placeholders but the accounts mentioned are all the members of the ASAllowed SG). Looks like not quite right yet ? :-

Cannot process argument transformation on parameter 'Identity'. Cannot convert
the "<domain>/<OU>/<Target_Account>" value of type "Microsoft.Exchange.Data.Directo
ry.Management.ReducedRecipient" to type "Microsoft.Exchange.Configuration.Tasks
.MailboxIdParameter".
    + CategoryInfo          : InvalidData: (:) [Get-CASMailbox], ParameterBind
   in...mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-CASMail
   box
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 34153704
Try to replace the line with get-casmailbox with one of these

get-casmailbox $member.samaccountname | Set-CASMailbox –ActiveSyncEnabled $true
or
get-casmailbox $member.DistinguishedName | Set-CASMailbox –ActiveSyncEnabled $true

0
 
LVL 11

Author Comment

by:TheGeezer2010
ID: 34154210
Both complete (although script does not seem to exit) but still do not re-enable the ActiveSync on the account.
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 34154224
Are you getting any errors?

can you try this for a single account

get-casmailbox USERNAME | Set-CASMailbox –ActiveSyncEnabled $true
0
 
LVL 11

Author Comment

by:TheGeezer2010
ID: 34154398
Bear in mind that username, SAMAccountName and display name are all the same for the account, this works fine. It also works on UPN. I have tried referencing the $member.upn and again, completes but does not seem to exit the command, and of course, the ActiveSync is still disabled.
0
 
LVL 11

Author Comment

by:TheGeezer2010
ID: 34165727
OK this is REALLY frustrating now, the code itself runs fine and does what it says, disable all AS, IMAP, POP3 on all accounts in specified DBase, then re-enable AS for all members of the specified group irrespective of when account was created and outputs all mailboxes on specified DBase which have AS enabled- all works great when run interactively :-

#Run Daily to disable ActiveSync, POP3 and IMAP4 on all mailboxes created

#previous day on TestDB

#Adding Exchange Snap In to execute Exchange CmdLets in this script
#Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin

#Set execution policy to allow scripts to be run in Powershell
#set-executionpolicy RemoteSigned

get-mailboxdatabase testdb |get-mailbox | Get-User -ResultSize Unlimited |

Where {($_.WhenCreated -gt (get-date).adddays(-1))} | Set-CASMailbox –

ActiveSyncEnabled:$false –POPEnabled:$false -IMAPEnabled:$False

#Run daily to re-enable ActiveSync on ALL mailboxes on TestDB which are

members #of the ASAllowed SG


#assign members of target DG to array

$allmembers = get-distributiongroupmember -identity asallowed | get-mailbox

#loop through array

foreach ($member in $allmembers) {

#set activesync enabled for each member of the array

$member.Alias | set-CASMailbox -ActiveSyncEnabled:$True

}

#check for enabled members


get-mailboxdatabase testdb | get-mailbox | Get-CASMailbox -resultsize

unlimited | Where-Object {$_.ActiveSyncEnabled -eq "True" } | Select-object

SamAccountName,ActiveSyncEnabled

I have saved this script as ActiveSyncTest.ps1
I have created a batch file (ASTest.bat) which runs following command :-
powershell.exe -command "ActiveSyncTest.ps1"

I schedule this task. Even if I run the task manually I get this spiel :-

The term 'get-mailboxdatabase' is not recognized as the name of a cmdlet, funct
ion, script file, or operable program. Check the spelling of the name, or if a
path was included, verify that the path is correct and try again.
At C:\BPTest\ActiveSyncTest.PS1:9 char:20
+ get-mailboxdatabase <<<<  testdb |get-mailbox | Get-User -ResultSize Unlimite
d | Where {($_.WhenCreated -gt (get-date).adddays(-1))} | Set-CASMailbox -Activ
eSyncEnabled:$false -POPEnabled:$false -IMAPEnabled:$False
    + CategoryInfo          : ObjectNotFound: (get-mailboxdatabase:String) [],
    CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

The term 'get-distributiongroupmember' is not recognized as the name of a cmdle
t, function, script file, or operable program. Check the spelling of the name,
or if a path was included, verify that the path is correct and try again.
At C:\BPTest\ActiveSyncTest.PS1:16 char:42
+ $allmembers = get-distributiongroupmember <<<<  -identity asallowed | get-mai
lbox
    + CategoryInfo          : ObjectNotFound: (get-distributiongroupmember:Str
   ing) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

The term 'set-CASMailbox' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
At C:\BPTest\ActiveSyncTest.PS1:24 char:31
+ $member.Alias | set-CASMailbox <<<<  -ActiveSyncEnabled:$True
    + CategoryInfo          : ObjectNotFound: (set-CASMailbox:String) [], Comm
   andNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

The term 'get-mailboxdatabase' is not recognized as the name of a cmdlet, funct
ion, script file, or operable program. Check the spelling of the name, or if a
path was included, verify that the path is correct and try again.
At C:\BPTest\ActiveSyncTest.PS1:31 char:20
+ get-mailboxdatabase <<<<  testdb | get-mailbox | Get-CASMailbox -resultsize u
nlimited | Where-Object {$_.ActiveSyncEnabled -eq "True" } | Select-object SamA
ccountName,ActiveSyncEnabled
    + CategoryInfo          : ObjectNotFound: (get-mailboxdatabase:String) [],
    CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
This is being run directly on the Exchange mailbox server so what is the problem ?? Why will this thing not run as a scheduled task when called from a bath file but runs fine when invoked interactively ?? Help before I lose my marbles please !!!
0
 
LVL 27

Assisted Solution

by:KenMcF
KenMcF earned 200 total points
ID: 34165795
Take a look at this line
#Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin

it should not be commented out.
0
 
LVL 11

Author Comment

by:TheGeezer2010
ID: 34166657
Hi Ken

If I DON'T comment this line, the script does not run correctly and does not do what it is supposed to do.
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 34166679
That command imports the Exchange cmdlets so you are able to run them. What happens to the script when this line line is not commented out?
0
 
LVL 11

Author Comment

by:TheGeezer2010
ID: 34167381
It runs interactively perfectly. If I leave it in and run it interactively, the script runs but does nothing.
0
 
LVL 11

Author Closing Comment

by:TheGeezer2010
ID: 34334813
I explained at the start that this was Exchange 2010, but the answers I received were for Exchange 2007 - most apply equally, but in this case, the pss module was incorrect.
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Email Header Detail 12 55
Powershell scheduled task with arguments 7 34
Problem to setup GUI 11 33
Xenapp 6.5 Powershell question 9 19
Scam emails are a huge burden for many businesses. Spotting one is not always easy. Follow our tips to identify if an email you receive is a scam.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
how to add IIS SMTP to handle application/Scanner relays into office 365.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question