我已經編寫了一些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