我试图在任务计划程序上运行powershell脚本,但无法理解适合我的脚本的日志记录命令。我想让这个按计划运行。在任务计划程序中安排powershell脚本的问题windows 2008服务器
脚本会删除x天前的文件和文件夹,并创建输出日志。
function Out-Log {
param (
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[string]$message,
[switch]$Error
)
$logPath = $env:TEMP + "\Filedeletion.log"
$message | Out-File -FilePath $logPath -Append
}
trap
{
"error:" | Out-Log
$_.exception.message | Out-Log
break
}
$Error.Clear()
try
{
"Starting" | Out-Log
$DateToDelete = 1
$dateLimit = (Get-Date).AddDays(-$DateToDelete)
$StartFolder = "c:\TEST1"
Get-ChildItem -Recurse -Force -Path $StartFolder |
foreach {
$currentItemIsFolder = $_.PsIsContainer;
$curentItemIsOld = $_.LastWriteTime -lt $dateLimit
if ($curentItemIsOld -and (-not $currentItemIsFolder))
{
"Removing '$($_.fullname)'." | Out-Log
Remove-Item -Path ($_.fullname) -Force -WhatIf
}
}
}
finally
{
if ($Error)
{
"`$error stack:" | Out-Log
$error | foreach {$_.exception.ToString() | Out-Log}
}
"Stopping" | Out-Log
}
我试图用
Powershell -file "c:\Powershell\Filedeletion_logs_test.ps1"
通过批处理运行PowerShell的。
我试过检查PowerShell中的命令/?但没有找到适用于我的脚本的任何合适的日志记录命令。
任何人都可以请帮忙吗?
通常情况下,我只用启动转录和使用-Verbose开关上的删除,项目的命令。 – mjolinor 2013-03-04 11:33:52
我想安排在PowerShell命令模式下,我想-verbose是不支持的日志命令。 – user1926332 2013-03-04 11:46:10
尝试使用开始脚本,并在您的脚本中设置$ VerbosePreference'继续' – mjolinor 2013-03-04 12:10:29