PowerShell脚本中的日志记录和作业控制[范围]通过计划任务运行

时间:2016-11-10 01:09:42

标签: powershell logging scope scheduled-tasks jobs

我有一个执行powershell脚本的计划任务。

无论如何,我从脚本的其余部分得到log4net输出。当$job=Start-BitsTransfer部分运行时,我什么也得不到。

如果我只是在PShell执行脚本,就像冠军一样。作业控制/逻辑工作,我得到输出。

当我在计划任务中运行时,SCRIPT范围(我可以从ss64.com/ps/syntax-scopes告诉)不会输出,我无法真正访问或控制。

以下是杀死我的脚本部分:

$log.Info("TestPath is:" +(Test-Path $DestDir\$file))
if (!(Test-Path $DestDir\$file)) 
{
    $log.Info('COPY $SrcDir\$file - $DestDir\')
    $job = Start-BitsTransfer -Asynchronous "$SrcDir\$file" "$DestDir\$file" -DisplayName $file

    $log.Info("JUST RAN JOB")
    $log.Info("JUST RAN JOB::jobstate =" +$(Get-Job -Name $file))
    WHILE ( $job.JobState -eq 'Transferring' -or $job.JobState -eq 'Connecting' )
    {
        $log.Info("jobstate =" +$job.JobState)
        Start-Sleep -Seconds 600
    }
    $log.Info("jobstate= " +$job.JobState+" -- starting Complete command")
    TRY { 
        $log.Info("Completing Job= " +$job.JobState + " with ID: " +$(Get-BitsTransfer | select $_.JobID))
        Get-BitsTransfer | complete-bitstransfer 
        $log.Info("Completed Job= " +$job.JobState)
    } CATCH { 
        $log.Error("ERROR Completing Job " ) }
    }

最接近我来博客/文章我需要is here,但没有得到我需要的东西(由warren f。)和similar question here, - and here在stackoverflow上提交时,但没有解决方案......

我已经尝试过(我记得很难忘记):

  • 在脚本中使用log4net
  • 将计划任务配置中的输出发送到文件
  • set-psdebug -trace
  • Warren F的文章(上面链接)
  • 将作业输出发送到脚本代码中的文件

AAARRRGGGG !! 再次,TIA。

njoylif

BTW,这是示例日志输出:

  

2016-11-09 17:06:34,222 [Pipeline Execution Thread] INFO TestPath   是:错误2016-11-09 17:06:34,425 [管道执行线程]信息   COPY \ 10.104.192.107 \ d $ \ BULocal \ 2016-10-30.bak - F:\ BURemote \   2016-11-09 17:06:34,519 [管道执行线程] INFO JUST RAN JOB   2016-11-09 17:06:35,207 [管道执行线程] INFO JUST RAN   JOB :: jobstate = 2016-11-09 17:06:35,207 [管道执行主题]   INFO jobstate = - 开始完成命令2016-11-09 17:06:35,238   [管道执行线程] INFO完成作业=带ID:   Microsoft.BackgroundIntelligentTransfer.Management.BitsJob   Microsoft.BackgroundIntelligentTransfer.Management.BitsJob   Microsoft.BackgroundIntelligentTransfer.Management.BitsJob   Microsoft.BackgroundIntelligentTransfer.Management.BitsJob   Microsoft.BackgroundIntelligentTransfer.Management.BitsJob   Microsoft.BackgroundIntelligentTransfer.Management.BitsJob   Microsoft.BackgroundIntelligentTransfer.Management.BitsJob   Microsoft.BackgroundIntelligentTransfer.Management.BitsJob   Microsoft.BackgroundIntelligentTransfer.Management.BitsJob   Microsoft.BackgroundIntelligentTransfer.Management.BitsJob   Microsoft.BackgroundIntelligentTransfer.Management.BitsJob   Microsoft.BackgroundIntelligentTransfer.Management.BitsJob   Microsoft.BackgroundIntelligentTransfer.Management.BitsJob   Microsoft.BackgroundIntelligentTransfer.Management.BitsJob   Microsoft.BackgroundIntelligentTransfer.Management.BitsJob   Microsoft.BackgroundIntelligentTransfer.Management.BitsJob   Microsoft.BackgroundIntelligentTransfer.Management.BitsJob   Microsoft.BackgroundIntelligentTransfer.Management.BitsJob   Microsoft.BackgroundIntelligentTransfer.Management.BitsJob 2016-11-09   17:06:35,582 [管道执行线程] INFO完成作业=

see here

0 个答案:

没有答案