【问题标题】:start-Transcript not capturing all output to log file..?start-Transcript 未将所有输出捕获到日志文件..?
【发布时间】:2012-10-21 01:51:19
【问题描述】:

我有下面的代码,它通过并获取计划任务信息,并将屏幕上出现的输出放到日志文件中。

但是,我注意到,除了“访问被拒绝”的服务器之外,所有错误都被记录下来——我怎样才能在日志文件中记录这些错误。

下面是代码:

Start-Transcript -path $scheduledpath\logging.txt -append

foreach ($name in $names) 
{
    Write-Host "Running Against Server $name" -ForegroundColor Magenta
    if ( Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue ) 
        {
            #$Command = "schtasks.exe /query /S $name /fo CSV /v >c:\tools\Scheduled\$name.csv"
            $Command = "schtasks.exe /query /S $name /fo CSV /v >$scheduledpath\$name.csv"
            Invoke-Expression $Command
            Clear-Variable Command -ErrorAction SilentlyContinue
        }

    else{
            Write-Host "$name is Down" -ForegroundColor Red
        }

}

Stop-Transcript

这是屏幕上的输出:

> Running Against Server SV064909 
> SV064909 is Down 
> Running Against Server SV081372 
> SV081372 is Down 
> Running Against Server YBEF008690_vorher_SV064930 
> YBEF008690_vorher_SV064930 is Down 
> Running Against Server Alt_SV064921 
> Alt_SV064921 is Down 
> Running Against Server SV073632 
> ERROR: Access is denied. 
> Running Against Server SV073633 
> ERROR: Access is denied.

这是 LOG 文件中的输出....没有显示 ACCESS IS DENIED...?

> Running Against Server SV064909 
> SV064909 is Down 
> Running Against Server SV081372 
> SV081372 is Down 
> Running Against Server YBEF008690_vorher_SV064930 
> YBEF008690_vorher_SV064930 is Down 
> Running Against Server Alt_SV064921 
> Alt_SV064921 is Down 
> Running Against Server SV073632 
> Running Against Server SV073633

【问题讨论】:

    标签: powershell powershell-2.0


    【解决方案1】:

    在连接错误315857 中报告了本机命令输出未记录在 Start-Transcript 输出但输出到控制台的这种行为。请参阅变通方法以了解可能的解决方案。

    【讨论】:

    【解决方案2】:

    最简单的解决方法是将本机命令的结果通过管道传送到Out-Host

    schtasks.exe /query /S $name /fo CSV /v >$scheduledpath\$name.csv | Out-Host
    

    【讨论】:

    • 现在,管道已爆裂,您无法通过管道将输出传输到其他任何内容。这是此解决方法最明显的缺点。
    • 我怀疑 (schtasks.exe /query /S $name /fo CSV /v >$scheduledpath\$name.csv | Out-Host) 会破坏管道,尽管现在不处理错误。
    【解决方案3】:

    以上都不适合我。 我发现a link from 2009 讨论了这个问题。但是微软blog from 2010 为我解决了这个问题。

    简而言之,将本机命令输出通过管道传输到 Out-Default。

      schtasks.exe /query /S $name /fo CSV /v | Out-Default
    

    {我自己并没有真正尝试过这条线,但你明白了。}

    【讨论】:

      【解决方案4】:

      啊,正确答案应该是:

      $Command = "schtasks.exe /query /S $name /fo CSV /v 2>&1 >$scheduledpath\$name.csv"
      

      即管道 stderror(管道 2)到标准输出(管道 1),以便两者都显示在标准输出中。

      【讨论】:

        【解决方案5】:

        获取服务标签

            $servicetag = Get-WmiObject win32_bios | Select-Object -ExpandProperty SerialNumber 
            Write-output $servicetag
        

        我在脚本顶部的一行中使用了 Write-output, 在那之后,他们都是Write-Host。但是每个“写入”输出现在都显示在我的脚本中。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-03-31
          • 2015-05-19
          • 1970-01-01
          • 2021-10-31
          • 2020-02-28
          • 1970-01-01
          • 2021-01-09
          相关资源
          最近更新 更多