Hi everyone.

I need assistance with scheduling a task in Exchange 2013 on Server 2016. I have a script that clears out Exchange logs. The script runs fine but I’m having issues getting it to run from the task scheduler.

Here are the settings:

Actions tab: Start a program

Program/script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Add arguments: -ExecutionPolicy Bypass “C:\Users\administrator.PNG\Desktop\ClearLogs.PS1”

I think my problem is that it needs to be run inside EMS and not powershell but I’m not sure what the correct arguments should be to make that wok.

2 Spice ups

You would need to post the script to provide more assistance. Most of the scripts to clean exchange logs do not require any specific Exchange command but if you do you can try adding the following to the beggining of the script:

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn
1 Spice up

EMS can be run as a plugin for Powershell (assuming it’s executed on a system with EMS installed).

http://hkeylocalmachine.com/?p=180

Here is the script:

Set-ExecutionPolicy Unrestricted
$days=0
$IISLogPath="C:\inetpub\logs\LogFiles\"
$ExchangeLoggingPath="C:\Program Files\Microsoft\Exchange Server\V15\Logging\"
$ETLLoggingPath="C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\ETLTraces\"
$ETLLoggingPath2="C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\Logs"
Function CleanLogfiles($TargetFolder)
{
    if (Test-Path $TargetFolder) {
        $Now = Get-Date
        $LastWrite = $Now.AddDays(-$days)
        $Files = Get-ChildItem $TargetFolder -Include *.log,*.blg, *.etl, *.txt -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}
        foreach ($File in $Files)
            {Write-Host "Deleting file $File" -ForegroundColor "white"; Remove-Item $File -ErrorAction SilentlyContinue | out-null}
       }
Else {
    Write-Host "The folder $TargetFolder doesn't exist! Check the folder path!" -ForegroundColor "white"
    }
}
CleanLogfiles($IISLogPath)
CleanLogfiles($ExchangeLoggingPath)
CleanLogfiles($ETLLoggingPath)
CleanLogfiles($ETLLoggingPath2)

What error are you getting on the task scheduler? Does the script works when running from the console?

Yes. The script runs fine from EMS. There is no error. The event logs state that the job starts , then finishes very soon after.

My first thought would be permissions. For that you need to ensure that the task is set to run with highest privileges (there is a check box for that). On the task scheduler itself you can see if there are any errors on the history.

Additionally you could add the following lines to the script so that you save a transcript of what is happening on the console. That should help you catch any error:

#This is the path to save the transcript
$outPath = "C:\users\user\log.txt" 

Start-Transcript -path $outPath 

....SCRIPT.....

#Transcript Ends
Stop-Transcript

No need to keep execution policy everytime in the script you can bypass on task action

$days=0
$IISLogPath="C:\inetpub\logs\LogFiles\"
$ExchangeLoggingPath="C:\Program Files\Microsoft\Exchange Server\V15\Logging\"
$ETLLoggingPath="C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\ETLTraces\"
$ETLLoggingPath2="C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\Logs"
Function CleanLogfiles($TargetFolder)
{
    if (Test-Path $TargetFolder) {
        $Now = Get-Date
        $LastWrite = $Now.AddDays(-$days)
        $Files = Get-ChildItem $TargetFolder -Include *.log,*.blg, *.etl, *.txt -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}
        foreach ($File in $Files)
            {Write-Host "Deleting file $File" -ForegroundColor "white"; Remove-Item $File -ErrorAction SilentlyContinue | out-null}
       }
Else {
    Write-Host "The folder $TargetFolder doesn't exist! Check the folder path!" -ForegroundColor "white"
    }
}
CleanLogfiles($IISLogPath)
CleanLogfiles($ExchangeLoggingPath)
CleanLogfiles($ETLLoggingPath)
CleanLogfiles($ETLLoggingPath2)

https://community.spiceworks.com/how_to/17736-run-powershell-scripts-from-task-scheduler

Make sure to check run with Highest privileged

I’d start by adding a Start-Transcript command inside the script. Then look at the output.

I suspect you are not loading the Exchange cmdlets and that that’s the cause of the script bailing.

Huh, I just did this last week and had no such problems. My script is very similar to yours, and I did not have to do anything special to call the EMS, I just run PowerShell… Note, I did not specify the entire path to PowerShell in my task since the system is smart enough to recognize just “powershell”. Can’t see why that would make a difference but worth a try.

Also, is your local account really called Administrator.PNG?

Try moving your script to a non-user account subfolder (C:\scripts) just for giggles.