Short PS script not working when called via scheduled task
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?
August 14th, 2012 9:21am
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
Free Windows Admin Tool Kit Click here and download it now
August 14th, 2012 9:49am
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
}
August 14th, 2012 10:23am
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
Free Windows Admin Tool Kit Click here and download it now
August 14th, 2012 10:32am
Hi
Did you try above Script.
In order to run get-mailboxdatabase CMDLET, you should have both two permisson for the account
Organization ManagementServer Management
So, could you have a check on that?
Cheers
Zi Feng
TechNet Community Support
August 15th, 2012 5:49am
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/
Free Windows Admin Tool Kit Click here and download it now
August 15th, 2012 7:23am
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/
August 15th, 2012 7:23am
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-0d9f44216dddJames Chong MCITP | EA | EMA; MCSE | M+, S+ Security+, Project+, ITIL msexchangetips.blogspot.com
Free Windows Admin Tool Kit Click here and download it now
August 15th, 2012 10:07am
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-0d9f44216dddJames Chong MCITP | EA | EMA; MCSE | M+, S+ Security+, Project+, ITIL msexchangetips.blogspot.com
August 15th, 2012 10:07am
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.
Free Windows Admin Tool Kit Click here and download it now
September 14th, 2012 2:24pm