Start-Transcript cmdlet创建一个记录的全部或部分记录 将PowerShell会话转换为文本文件。笔录包含所有命令 用户键入以及控制台上显示的所有输出。
代码
cls
$global:scriptPath = Split-Path $script:MyInvocation.MyCommand.Path
$global:scriptName = $global:scriptPathAndName.Replace(($global:scriptPath + "\"),"")
$global:scriptNameNoExt = $global:scriptName.Replace(".ps1","")
Start-Transcript -LiteralPath ($global:scriptPath + "\" + (get-date -f "yyyy-MM-dd_HH-mm-ss") + "_" + $global:scriptNameNoExt + ".log")
foreach ($n in 1..3)
{
write-host -NoNewline ("test line " + $n + " ")
}
write-host
Stop-Transcript
控制台输出-按预期显示
Transcript started, output file is C:\temp\2019-02-06_07-13-43_test.log
test line 1 test line 2 test line 3
Transcript stopped, output file is C:\temp\2019-02-06_07-13-43_test.log
文字内容-换行符来自何处?
**********************
Windows PowerShell transcript start
Start time: 20190206071343
Username: xxx
RunAs User: xxx
Machine: xxx
Host Application: xxx
Process ID: 10092
PSVersion: 5.1.14409.1018
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.14409.1018
BuildVersion: 10.0.14409.1018
CLRVersion: 4.0.30319.42000
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
**********************
Transcript started, output file is C:\temp\2019-02-06_07-13-43_test.log
test line 1
test line 2
test line 3
**********************
Windows PowerShell transcript end
End time: 20190206071343
**********************