我已经编写了一些PowerShell脚本来归档来自log4net的旧日志,并且如果它们超过特定年龄,则从归档中删除任何文件。我有三个脚本,一个调用其他脚本并根据归档过程的成功邮寄一份报告。Powershell脚本没有从计划任务运行
我写了一个powershell脚本来安排调用其他两个脚本的主要任务。
当我从powershell控制台运行脚本时,所有脚本都按预期运行。
我运行下面的命令来运行所有我的脚本:
CD e:\PowerShellScripts
.\ArchiveAndDeleteLogFiles.ps1 "E:\log4Net\WindowsServices\MailToolService" 24 "E:\log4Net\WindowsServices\MailToolService\archive" 720
我实际上运行的是负责运行其他两个PowerShell脚本具有以下参数定义的脚本(我饶了你所有实施细则)
<#
.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
)
我需要动态调度它们,因为我想将它们放置在构建过程中,以便在部署网站时确保通过PowerShell设置清理日志文件。为了实现这一点,我已经创建了一个PowerShell脚本,像这样:
# 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
的问题是,虽然任务在Windows/PowerShell的计划失败,出现以下错误:
任务计划失败启动用户“XXXXXXXXX”的“\ Microsoft \ Windows \ PowerShell \ ScheduledJobs \ Archive and Delete旧文件 - 邮件服务”任务。其他数据:错误值:2147750687.
这可能是一个海拔问题吗?我在这里对Powerhell很陌生。我认为以前的尝试一直在运行,根本就没有完成。
我已经有基本的尝试捕获和邮件运行在脚本本身,但我感觉我传递参数错误或什么的。
任何想法?
[编辑]我已经经历了一些测试,看起来问题在我正在使用的调度方法。它在计划任务下构建一个动态XML文档,该文档包含传递给脚本的所有参数。我不知道一个PowerShell计划任务的模式,但它看起来要传递的参数如下:
<?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>
所以我改变了命令正在运行下面的代码,并通过PowerShell中运行它窗口看看会发生什么。它给了我一个乱码,表明它期望在第1行char 244的run方法中以某种形式或另一种形式出现一个参数。我想知道在它创建的XML文件中生成的语法是否存在某些问题, PowerShell参数。似乎不太可能我怀疑我需要查看Microsoft.PowerShell.ScheduledJob。ScheduledJobDefinition命名空间
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()"
谢谢JP,我稍后再说。现在做其他的事情,但我很快就会回到 – IAMMRBONGO
看起来这不是问题。我停止了它,但它并没有实际执行。我猜测我没有设法正确安排它 – IAMMRBONGO
考虑它,我认为它坐在等待输入参数,没有运行。我的猜测是这是参数传递的方式(或者说没有被传递) – IAMMRBONGO