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:15am
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
}
Free Windows Admin Tool Kit Click here and download it now
August 14th, 2012 10:17am
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
August 14th, 2012 10:25am
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
Free Windows Admin Tool Kit Click here and download it now
August 15th, 2012 5:49am
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.
September 14th, 2012 2:24pm