IAMMRBONGO
IAMMRBONGO

Reputation: 73

Powershell script not running from scheduled tasks

I've written some powershell scripts to archive old logs from log4net and delete any from the archive if they are past a certain age. I've got three scripts, one calls the other scripts and mails a report depending upon the success of the archive process.

I've then written a powershell script to schedule the main task that calls the other two scripts.

When I run the script from the powershell console all of my scripts run as expected.

I run the following command to run all of my scripts:

CD e:\PowerShellScripts
.\ArchiveAndDeleteLogFiles.ps1 "E:\log4Net\WindowsServices\MailToolService" 24 "E:\log4Net\WindowsServices\MailToolService\archive" 720

The script I am actually running that is responsible for running the other two powershell scripts has the following parameter definition (I shall spare you all the implementation details)

<#
.SYNOPSIS
    Archives old files and deletes old archive files
.DESCRIPTION
    Script will archive any files (using 7-Zip) that are older than specified in hours in a specified directory, script will move them into a directory also specified by the user.
    Script will then (optionally) delete old archived files that are over a number of hours old (again specified by the user). If the optional time passed is 0 then the script will not delete old archived files
.PARAMETER FolderLocation
    The location on disk of the folder containing the files that you need to archive
.PARAMETER AgeOfFilesToArchive
    The amount of time in hours before a file should be archived
.PARAMETER ArchiveLocation
    The location of the archive
.PARAMETER AgeOfFilesToDelete
    The amount of time in hours before a file should be deleted
.PARAMETER 7ZipExecutableLocation
    The location of the 7Zip Executable
.NOTES  
    File Name  : ArchiveAndDeleteOldFiles.ps1  
    Author     : Me 
    Requires   : PowerShell V3, 7Zip   

.LINK
    https://xxxxxxxx
.EXAMPLE
    .\ArchiveAndDeleteLogFiles "C:\LogFileLocation" 1 "C:\ArchivedLogFileLocation" 1

#>

param
(
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
$FolderLocation,

[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[ValidatePattern('\d+')]
$AgeOfFilesToArchive,

[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
$ArchiveLocation,

[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[ValidatePattern('\d+')]
$AgeOfFilesToDelete,

[Parameter(Mandatory=$False)]
$7ZipExecutableLocation
)

I need to schedule them dynamically, as I want to place these in a build process so that when a website is deployed it ensures that a clear up of log files is also set up via powershell. In order to achieve this I've created a Powershell Script like so:

# Import the module
Import-Module PSScheduledJob

# Add a trigger
$trigger = New-JobTrigger -Daily -At "06:00"
$folderLocation =
$AgeOfFilesToArchive = 24
#properties
$properties = @{
     "FolderLocation" = "E:\log4Net\WindowsServices\MailToolService";
     "AgeOfFilesToArchive" = 24;
     "ArchiveLocation" = "E:\log4Net\WindowsServices\MailToolService     \archive";
     "AgeOfFilesToDelete" = 720}

 # Register the job
 Register-ScheduledJob -Name "Archive And Delete Old Files - Mail Service" -Trigger $trigger -FilePath "e:\PowerShellScripts\ArchiveAndDeleteLogFiles.ps1" -ArgumentList $properties

The problem is that although the task is scheduled under Windows/powershell it fails with the following error:

Task Scheduler failed to start "\Microsoft\Windows\PowerShell\ScheduledJobs\Archive And Delete Old Files - Mail Service" task for user "XXXXXXXXX". Additional Data: Error Value: 2147750687.

Is this likely to be an elevation problem? I'm kind of new to powershell here. I think that the previous attempt keeps running and simply never manages to complete.

I've got basic try catch and mail running in the scripts themselves but I get the feeling that I am passing parameters wrong or something.

Any ideas?

[EDIT] I've gone through a few tests and it looks like the problem is within the scheduling method that I'm using. It builds a dynamic XML document under scheduled tasks which looks to contain all of the parameters that it is passing to my script. I don't know the schema of a powershell scheduled task but it looks to pass the parameters as follows:

<?xml version="1.0"?><Keys xmlns="" z:Assembly="0" z:Type="System.Object[]" z:Id="53" z:Size="4">
<anyType xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays" z:Assembly="0" z:Type="System.String" z:Id="54">ArchiveLocation</anyType>
<anyType xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays" z:Assembly="0" z:Type="System.String" z:Id="55">AgeOfFilesToArchive</anyType>
<anyType xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays" z:Assembly="0" z:Type="System.String" z:Id="56">FolderLocation</anyType>
<anyType xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays" z:Assembly="0" z:Type="System.String" z:Id="57">AgeOfFilesToDelete</anyType>

So I changed the command being run to the code below and run it through the powershell window to see what happens. It gives me back a garbled message stating that it's expecting a parameter in some form or another within the run method at line 1 char 244. I'm wondering if there is some problem with the syntax it generates in the XML file it created containing the powershell parameters. It seems unlikely I suspect that I need to look into the Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition namespace

  Powershell.exe -NoExit -NoLogo -NonInteractive -WindowStyle Maximized -Command "Import-Module PSScheduledJob; $jobDef = [Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition]::LoadFromStore('Archive And Delete Old Files - Mail Service', 'C:\Users\Administrator\AppData\Local\Microsoft\Windows\PowerShell\ScheduledJobs'); $jobDef.Run()"

Upvotes: 2

Views: 719

Answers (1)

JPBlanc
JPBlanc

Reputation: 72612

This Error means : "duplicate task already running"

You perhaps try to start it interactively two times. So first I would reboot my computer (Try first to stop the task). Then I will check the following :

enter image description here

Upvotes: 1

Related Questions