Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 是否可以将写调试消息传输到日志文件?_Powershell_Powershell 2.0 - Fatal编程技术网

Powershell 是否可以将写调试消息传输到日志文件?

Powershell 是否可以将写调试消息传输到日志文件?,powershell,powershell-2.0,Powershell,Powershell 2.0,写入详细消息不会重定向到Out文件 我的高级功能在没有任何跟踪的情况下失败,因此我希望放置调试消息。当脚本被执行时,它将不会在控制台中运行,因此我无法看到消息 如果消息可以传输到日志文件,那么我可以看到它。写调试是否可以将值传递给日志文件?如果您使用的是Powershell v3+,则可以将调试流重定向到文本文件: 示例文件:test.ps1 $debugPreference = "Continue" Write-Output "Output msg" Write-Debug "Debug ms

写入详细消息不会重定向到Out文件

我的高级功能在没有任何跟踪的情况下失败,因此我希望放置调试消息。当脚本被执行时,它将不会在控制台中运行,因此我无法看到消息


如果消息可以传输到日志文件,那么我可以看到它。写调试是否可以将值传递给日志文件?

如果您使用的是Powershell v3+,则可以将调试流重定向到文本文件:

示例文件:test.ps1

$debugPreference = "Continue"
Write-Output "Output msg"
Write-Debug "Debug msg"
从powershell命令行:

c:\temp\test.ps1 5>c:\debug.txt
c:\debug.txt然后显示一行“debug msg”

5>c:\debug.txt段有点晦涩难懂,让我们把它分解一下。“5”表示调试流,这是第5个内部Powershell流,如所述和所示。然后,c:\debug.txt命令它将所有调试输出重定向到该文件

您还可以使用5>&1,它将调试流重定向到正常标准输出

如果您使用的是Powershell v2,则可以查看提供良好日志记录的脚本,也可以使用脚本本地函数,如下所述:

$DebuglogPreference = 'Continue'
function Write-DebugLog 
{
   param( [string]$message, [string]$filepath = 'c:\debug.txt' )

   if ($DebuglogPreference -eq 'Continue') 
   {
       $message | Out-File $filepath -append
   }
}

Write-DebugLog "Debug msg"

PowerShell只允许将流重定向到文件或成功输出流。重定向到任何其他流(例如
5>&4
)将引发错误。此外,问题被标记为
powershell-v2
,它只支持成功和错误流。PowerShell v3引入了警告、详细和调试流。删除了对5>&4的引用,并为PowerShell v2添加了其他选项。