在任务计划程序windows 2008 server中计划powershell脚本时出现问题

在任务计划程序windows 2008 server中计划powershell脚本时出现问题,powershell,powershell-2.0,powershell-3.0,powershell-1.0,Powershell,Powershell 2.0,Powershell 3.0,Powershell 1.0,我正在尝试在任务计划程序上运行powershell脚本,但无法理解适合我的脚本的日志记录命令。我想让它按计划运行 脚本将删除超过x天的文件和文件夹,并创建输出日志 function Out-Log { param ( [Parameter(Mandatory=$true,ValueFromPipeline=$true)] [string]$message, [switch]$Error ) $logPath = $env:TEMP + "\File

我正在尝试在任务计划程序上运行powershell脚本,但无法理解适合我的脚本的日志记录命令。我想让它按计划运行

脚本将删除超过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-文件“c:\Powershell\filedeletation\u logs\u test.ps1”

通过批处理运行powershell

我已尝试检查Powershell/中的命令?但是没有找到任何适合我的脚本的日志记录命令


有人可以帮忙吗?

您不需要在任务计划中运行powershell的单独批处理文件。你所需要的就是这本漂亮的教程。它是详细的一步一步,但很容易设置。

您不能对任务执行相同的操作吗

您应该能够通过TYPE命令通过管道传输服务器列表,并将任务添加到服务器集中

例如:


通常情况下,我只会使用start-transcript并在Remove-Item命令上使用-Verbose开关。我想在powershell命令模式下安排此操作-Verbose,我猜-Verbose是不受支持的日志记录命令。请尝试使用start-transcript,并将脚本中的$VerbosePreference设置为“Continue”Hi,到目前为止,我还没有在powershell中使用这些脚本,所以我不知道它在我当前脚本中的位置。有没有其他的选择,或者你能和我分享一些帮助,把它们包括在我的脚本中。我不明白这个问题。。。是你的日志不起作用吗?
FOR /F %%A IN (servers.txt) DO (
SCHTASKS /CREATE /S %%A /U "system" /P "" /TN "Powershell Task" /TR "Powershell -file \"c:\my folder\script.ps1\"" 
)