这是我开发的基于文件年龄移动文件的PS脚本。这个进程写入事件日志。我是Powershell的新手,无法弄清楚为什么下面的脚本不起作用。它没有运行我也找不到任何错误
function WriteEventLog ($message, $entrytype)
{
Write-EventLog -Logname "Application" -Source "MoveFilesScript" -Message $message -EntryType $entrytype -EventID 4001
}
$message = "Log file message goes here"
$entrytype = "Information"
WriteEventLog $message $entrytype
# File move related variables
$span = 30
$today = get-date
$CutoffTime = $today-$span
$SourcePath = "C:\tt\datfiles\Export\Intial"
$DestPath = "C:\tt\datfiles"
$files = dir $SourcePath
# empty arrays to hold file lists
$movedFiles = @()
$skippedFiles = @()
foreach ($file in $files)
{
if ($file.lastwritetime -gt $CutoffTime)
{
$skippedFiles += $file.fullname
$message = "Not moving newer: " -join $file.fullname
$entrytype = "Information"
WriteEventLog $message $entrytype
}
else
{
$newname = $DestPath + "\" + $file.name + "-" + $file.lastwritetime.tostring("yyyy-MMM-dd")
$olderror = $error
move-item -ev a -path $file.fullname -destination $newname
$a.count
echo $movedFiles
if ($a.count -gt 0)
{
$message = $a[0].tostring()
$type = "Error"
WriteEventLog $message $type
}
else
{
$movedFiles += $file.fullname
}
}