locked
Short PS script not working when called via scheduled task RRS feed

  • Question

  • Exchange 2010 SP2 RU3

    We have a script created to count the number of mailbox databases that are currently enabled for provisioning, and to generate an email should this number drop below our configured threshold.  We use a specific account to run the scheduled tasks from, and we have no problems running it under those credentials within the Exchange shell. We have other scheduled tasks set up similarly that work fine.

    get-mailboxdatabase 'mailstore_*' | where {$_.isexcludedfromprovisioning -eq $false} | ForEach-Object {$count++}

    if ($count -lt 4) { Send-MailMessage <etc. etc. etc.>} else { }

    We schedule the task via a BAT file, which contains the below command

    PowerShell.exe -executionpolicy bypass -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\V14\Bin\ExShell.psc1" -Command ". '<path to script file>'"

    Is it a problem with how the count is done?

    Tuesday, August 14, 2012 1:15 PM

Answers

  • This was being caused by a corrupt powershell profile file that someone else had copied/modified on the management server where the task was running from.
    • Marked as answer by davrion Saturday, September 20, 2014 6:36 PM
    Friday, September 14, 2012 6:24 PM

All replies

  • Hi @all,

    I would prefer that you run the task with highest permission! You can enable that when opening the task.

    Than I would run it as a ps1 file like this:

    PowerShell.exe -File <Path2File.ps1>

    In the File you should load the Exchange PSSnapin like this:

    If ((Get-PSSnapin -Name "Microsoft.Exchange.Management.PowerShell.E2010" -ErrorAction "SilentlyContinue") -eq $null) { 
    If ((Get-PSSnapin -Registered -Name "Microsoft.Exchange.Management.PowerShell.E2010" -ErrorAction "SilentlyContinue")) { 
    Write-Host "Load Exchange PSSnapin" -ForegroundColor Magenta 
    Add-PSSnapin -Name 'Microsoft.Exchange.Management.PowerShell.E2010' 
    } }
    

    after that you put your code and I think it will run perfect.
    If not please post it here...

    Arne


    Arne Tiedemann | Active Directory and Exchange specialist


    Tuesday, August 14, 2012 1:43 PM
  • Hi Arne, 

    Thanks for your reply. I added the lines you provided to the script, and it runs fine from the shell. I modified the scheduled task to then launch powershell.exe -file <path to script>, and I see the same behavior when it executes successfully but the email does not get generated. The task is being executes by our normal account and with the checkbox to run with highest privileges.

    If ((Get-PSSnapin -Name "Microsoft.Exchange.Management.PowerShell.E2010" -ErrorAction "SilentlyContinue") -eq $null) { If ((Get-PSSnapin -Registered -Name "Microsoft.Exchange.Management.PowerShell.E2010" -ErrorAction "SilentlyContinue")) { Write-Host "Load Exchange PSSnapin" -ForegroundColor Magenta Add-PSSnapin -Name 'Microsoft.Exchange.Management.PowerShell.E2010' 
    # Set the Exchange Script path to a variable
    $EnvEScripts = "C:\Program Files\Microsoft\Exchange Server\V14\Scripts" } }
    
    
    # Generate alert if number of databases enabled for provisioning is below the configured limit of 4.
    $count=0
    
    get-mailboxdatabase 'mailstore*' | where {$_.isexcludedfromprovisioning -eq $false} | ForEach-Object {$count++}
    if ($count -lt 12) { 
    
    
    $messageParameters = @{
    			Subject = "Provisioning Database Count Below Limit - $count"
    			Body = get-mailboxdatabase 'mailstore*' -status | where {$_.isexcludedfromprovisioning -eq $false} | select-object name,databasesize,isexcludedfromprovisioning | sort-object name | ConvertTo-Html | Out-String
    			From = "messaging@domain.com"
    			To = "my_smtp@domain.com"
    			SmtpServer = "smtp.domain.com"
    			}                        
    Send-MailMessage @messageParameters -BodyAsHtml   
    
     } 

    Tuesday, August 14, 2012 2:17 PM
  • Hi @all,

    can you add a line for exporting the errors to a File like this:

    ... snip...

    get-mailboxdatabase 'mailstore*' | where {$_.isexcludedfromprovisioning -eq $false} | ForEach-Object {$count++}

    If ($Error[0]) { $error[0] | Out-File -Path <Path2Fil> -append -force
    if ($count -lt 12) {
    "I'm in the foreach" |
    Out-File -Path <Path2Fil> -append -force

    $messageParameters
    = @{
    Subject = "Provisioning Database Count Below Limit - $count"
    Body = get-mailboxdatabase 'mailstore*' -status | where {$_.isexcludedfromprovisioning -eq $false} | select-object name,databasesize,isexcludedfromprovisioning | sort-object name | ConvertTo-Html | Out-String
    From = "messaging@domain.com"
    To = "my_smtp@domain.com"
    SmtpServer = "smtp.domain.com"
    }                       
    Send-MailMessage @messageParameters -BodyAsHtml  
    If ($Error[0]) { $error[0] | Out-File -Path <Path2Fil> -append -force

    }

    Than you can see if the Script go in to the ForEach and what error comes back.


    Arne


    Arne Tiedemann | Active Directory and Exchange specialist

    Tuesday, August 14, 2012 2:25 PM
  • Hi

    Did you try above Script.

    In order to run get-mailboxdatabase CMDLET, you should have both two permisson for the account

    • Organization Management
    • Server Management

    So, could you have a check on that?

    Cheers


    Zi Feng

    TechNet Community Support

    Wednesday, August 15, 2012 9:49 AM
    Moderator
  • Exchange 2010 SP2 RU3

    We have a script created to count the number of mailbox databases that are currently enabled for provisioning, and to generate an email should this number drop below our configured threshold.  We use a specific account to run the scheduled tasks from, and we have no problems running it under those credentials within the Exchange shell. We have other scheduled tasks set up similarly that work fine.

    get-mailboxdatabase 'mailstore_*' | where {$_.isexcludedfromprovisioning -eq $false} | ForEach-Object {$count++}

    if ($count -lt 4) { Send-MailMessage <etc. etc. etc.>} else { }

    We schedule the task via a BAT file, which contains the below command

    PowerShell.exe -executionpolicy bypass -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\V14\Bin\ExShell.psc1" -Command ". '<path to script file>'"

    Is it a problem with how the count is done?

    is there an attempt to send the mail that fails, or does the task scheduler not get that far? put netmon on the machine that runs the task, start it up, run the task, and then check if there is any smtp traffic being generated. if so, see why it failed...

    my guess is that you might be hitting a relatively common problem, in that the account you are using to run scheduler does not "own" the email address that is being tagged to the message. i've come across this myself, and fixed it by assigning extra permissions to the account specified (naughty...)

    have a look at this wonderful explanation from morgan simonsen:

    http://morgansimonsen.wordpress.com/2009/12/15/exploring-task-scheduler/

    • Marked as answer by Zi FengModerator Wednesday, August 22, 2012 2:57 AM
    • Unmarked as answer by davrion Friday, September 14, 2012 6:24 PM
    Wednesday, August 15, 2012 11:23 AM
  • You need to run it like below it's by design if you excute using local powershell instead of remote powershell and you run into a split brain permission issue with powershell and task scheduler.

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; c:\admin\scripts\get-mailbox1.ps1"

    http://social.technet.microsoft.com/Forums/en/exchangesvradmin/thread/12ffef78-c85a-4831-8d48-0d9f44216ddd


    James Chong MCITP | EA | EMA; MCSE | M+, S+ Security+, Project+, ITIL msexchangetips.blogspot.com

    • Marked as answer by Zi FengModerator Wednesday, August 22, 2012 2:57 AM
    • Unmarked as answer by davrion Saturday, September 20, 2014 6:36 PM
    Wednesday, August 15, 2012 2:07 PM
  • This was being caused by a corrupt powershell profile file that someone else had copied/modified on the management server where the task was running from.
    • Marked as answer by davrion Saturday, September 20, 2014 6:36 PM
    Friday, September 14, 2012 6:24 PM