【问题标题】:Redirect whole script output to file and show in console at the same time将整个脚本输出重定向到文件并同时在控制台中显示
【发布时间】:2017-04-22 09:33:42
【问题描述】:

当我运行此脚本时,它会要求我填写变量 $ComputerName 和 $OutputFilePath,这是我想将整个脚本输出存储在 .txt 或 .CSV 中的路径。我将整个脚本包装在$(*script*)*>&1 > $OutputFilePath 中。它成功地将输出保存到指定路径,但没有将输出打印到控制台。

如何将整个输出保存到文件并同时打印到控制台?

param(
[Parameter(Mandatory=$true)][string]$ComputerName = $env:COMPUTERNAME,
[Parameter(Mandatory=$true)][string]$OutputFilePath 
)#end param

$(
# All Installed Windows features
Write-Output "===================================================================================================="

Write-Output "ALL INSTALLED WINDOWS FEATURES:" 
Write-Verbose -Message "Searching installed features..." -Verbose
$obj=Get-WindowsFeature | Where-Object {$_.Installed} | Select-Object Name, InstallState | Format-Table -AutoSize
Write-Output $obj 

Write-Output "===================================================================================================="
)*>&1 > $OutputFilePath

【问题讨论】:

    标签: windows powershell server


    【解决方案1】:

    我倾向于使用自定义的“Write-Terminal”函数,该函数输出到控制台并同时附加到日志文件(使用 WriteLog 函数)。它还使您能够根据消息类型更改控制台颜色。

    Function Write-Terminal
    {
    <#
    .SYNOPSIS
                Write output to the screen
    .DESCRIPTION
                <Description of function>
    .PARAMETER
                <Input params and descriptions>
    .RETURNS
                <What the function returns>
    .EXAMPLE
                <Function example>
    #>
        Param
        (
            [string]$strOut,
            [string]$strType
        )  
    
        switch -wildcard ($strOut)
        {
            "ERR:*"     { write-host $strOut -ForegroundColor RED -BackgroundColor BLACK }
            "WARN:*"    { write-host $strOut -ForegroundColor CYAN -BackgroundColor BLACK }
            "WARNING:*" { write-host $strOut -ForegroundColor CYAN -BackgroundColor BLACK }
            "INFO:*"    { write-host $strOut -ForegroundColor YELLOW -BackgroundColor BLACK }
            "DEBUG:*"   { write-host $strOut -ForegroundColor DARKGREEN -BackgroundColor BLACK }
            "OTH:*"     { write-host $strOut -ForegroundColor MAGENTA -BackgroundColor BLACK }  
            default     { write-host $strOut }      
        }
        WriteLog $strOut
    } #END Write-Terminal
    
    Function WriteLog
    {
    <#
    .SYNOPSIS
                Write output to a log file
    .DESCRIPTION
                <Description of function>
    .PARAMETER
                <Input params and descriptions>
    .RETURNS
                <What the function returns>
    .EXAMPLE
                <Function example>
    #>
        Param
        (
            [string]$strOut,
            [bool]$blnNoDate
        )    
        $strDateTime = get-date -uFormat "%d-%m-%Y %H:%M:%S"
        If (-not ($blnNoDate)) 
        {
            $strOutPut = ($strDateTime+ ": " + $strOut)
        } 
        else 
        {
            $strOutPut = $strOut
        }
        "$strOutPut" | out-file -filepath "path/to/logfile" -append
    } #END WriteLog
    

    所以在我的脚本中,每次我想写入控制台和日志文件时,我都会使用类似的东西:

    Write-Terminal "Some message"
    

    或者,如果我想在控制台中以红色文本显示错误消息,我会使用:

    Write-Terminal "ERR: Some message"
    

    请参阅下面的通用脚本“模板”中的扩展示例,我现在用于所有 PS 脚本(从网络上的各种来源编译):

    https://github.com/Asnivor/PowerShellScriptTemplate

    它还允许您使用现有框架同时自动记录错误。

    hth.

    【讨论】:

      【解决方案2】:

      您可以使用 tee-object(专为此目的而设计并在 v2 中也受支持)来做到这一点,如下所示:

      "testing" *>&1 | Tee-Object -FilePath .\OutputFile.txt
      

      【讨论】:

      • 当我尝试 task.ps1 *>&1 | Tee-Object -FilePath $OutputFilePath 它给了我错误:“术语 'task.ps1' 未被识别为 cmdlet 的名称”
      • @user1994521 你能试试这样的脚本完整路径吗:D:\logmod.ps1 *>1 | Tee-Object -FilePath test.txt
      【解决方案3】:

      Start-Transcript 可能是您正在寻找的。一旦被调用,它就会在后台运行并将您的控制台输入和输出写入文本文件 - 在此期间您的控制台正常运行。

      它是这样使用的:

      Start-Transcript -Path "C:\transcripts\transcript.txt"
      #yourcode here
      Stop-Transcript
      

      【讨论】:

      • 它给了我这个错误:“停止转录:这个主机不支持转录。”我需要在 powershell 版本 min 上执行此操作。 V2 到最新版本。
      • 您是在控制台而不是 ISE 中运行它吗?转录仅适用于 PS v5 及更高版本的 ISE。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2015-06-27
      • 2021-10-21
      • 1970-01-01
      • 2011-02-22
      • 2014-01-09
      相关资源
      最近更新 更多