繁体   English   中英

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

[英]issue with scheduling a powershell script in task scheduler windows 2008 server

我试图在任务计划程序上运行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

您不能对SCHTASKS做同样的事情吗?

http://ss64.com/nt/schtasks.html

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

例如:

http://www.robvanderwoude.com/ntadmincommands.php

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\"" 
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM