我已经编写了一些powershell脚本来存档来自log4net的旧日志,并且如果它们已超过某个年龄,则从存档中删除任何文件。我有三个脚本,一个调用其他脚本并根据存档过程的成功发送报告。
然后我编写了一个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
#&GT;
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下安排的,但它失败并出现以下错误:
任务计划程序无法启动&#34; \ Microsoft \ Windows \ PowerShell \ ScheduledJobs \存档和删除旧文件 - 邮件服务&#34;用户&#34; XXXXXXXXX&#34;的任务。附加数据:错误值:2147750687。
这可能是一个海拔问题吗?我在这里对PowerShell有点新意。我认为之前的尝试一直在运行,而且根本无法完成。
我已经在脚本中运行了基本的try catch和mail,但我觉得我传递参数错误或其他什么。
有什么想法吗?
[编辑]我已经完成了一些测试,看起来这个问题属于我正在使用的调度方法。它在计划任务下构建动态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()"