powershell - 在任务计划程序Windows 2008 Server中计划Powershell脚本的问题

标签 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 + "\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 /中的命令?但找不到适用于我的脚本的任何合适的日志记录命令。

谁能帮忙吗?

最佳答案

您不需要在任务计划中运行powershell的单独的批处理文件。您需要的只是这个不错的教程。它是分步详细的,但是非常容易设置。
http://community.spiceworks.com/how_to/show/17736-run-powershell-scripts-from-task-scheduler

关于powershell - 在任务计划程序Windows 2008 Server中计划Powershell脚本的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15200183/

相关文章:

powershell - 无法运行 PowerShell 卸载脚本

powershell - 使用动态构造的排除模式数组,使用 Get-ChildItem -Exclude 过滤子文件夹

Powershell 3克隆一个对象有意外的结果 - 影响其他对象

powershell - Powershell命令仅在我在ise中运行选择时有效

powershell - 为什么我需要在单引号中转义美元符号?

arrays - 对于 PowerShell ArrayList,哪个更快。Add 或 += 运算符?

Powershell在数组列表中查找值

sql-server - 调用-SqlCmd QueryTimeout

c# - 代表所有的 PowerShell 枚举值

.net - 如何使用 PowerShell 检查文件是否超过特定时间?