如何使用CR LF换行符在Powershell中的一次调用中写入控制台和日志文件
我有一个powershell脚本,我想通过一次调用写入控制台和日志文件 我在做这个如何使用CR LF换行符在Powershell中的一次调用中写入控制台和日志文件,powershell,newline,Powershell,Newline,我有一个powershell脚本,我想通过一次调用写入控制台和日志文件 我在做这个 Start-Transcript -Path $TargetDir\Log.log Write-Host "Stuff" 。。。这很好用,除了它生成的换行是LF,这意味着我的日志在地球上的每个文本编辑器中都很好,除了记事本 这是我要的 function global:Write-Notepad ( [string] $Message, [string] $ForegroundColor = 'G
Start-Transcript -Path $TargetDir\Log.log
Write-Host "Stuff"
。。。这很好用,除了它生成的换行是LF,这意味着我的日志在地球上的每个文本编辑器中都很好,除了记事本
这是我要的
function global:Write-Notepad
(
[string] $Message,
[string] $ForegroundColor = 'Gray'
)
{
Write-Host "$Message`r" -ForegroundColor $ForegroundColor
}
…它会在每条消息的末尾写一个CR,但它似乎不会写出这样的行
&$ACommand | Write-Notepad
我不确定管道操作员需要什么语法,但我非常感谢您的帮助。试试以下方法:
& $ACommand | Tee-Object -FilePath $TargetDir\Log.log | Write-Host
Tee对象将管道对象的副本发送到文件或变量,同时输出。以下是我确定的解决方案
# This method adds a CR character before the newline that Write-Host generates.
# This is necesary, because notepad is the only text editor in the world that
# doesn't recognize LF newlines, but needs CR LF newlines.
function global:Write-Notepad
(
[string] $Message,
[string] $ForegroundColor = 'Gray'
)
{
Process
{
if($_){ Write-Host "$_`r" }
}
End
{
if($Message){ Write-Host "$Message`r" -ForegroundColor $ForegroundColor }
}
}