如何在 PowerShell 中使用转录?


Powershell 中的转录类似于记录会话。因此,每当你开始在 PowerShell 中进行转录时,它就会开始录制你的指令和输出,并且无论是否有任何错误输出,它都会被记录。若要开始转录,你需要在开始时运行 Start-Transcript 命令,然后无论你写什么内容,它都会被记录下来。

若要开始录制,你需要编写 Start-Transcript 命令,并且必须提供转录的路径,如以下示例所示,

示例

Start-Transcript -Path C:\Temp\sessionrecord.txt

输入上述命令后,你将收到如下所示的消息。

Start-Transcript -Path .\Sessionrecording.txt

输出

PS E:\scripts\Powershell> Start-Transcript -Path .\Sessionrecording.txt
Transcript started, output file is .\Sessionrecording.txt

以下是在开始转录后显示的 PowerShell 会话屏幕。

示例

PS E:\scripts\Powershell> Get-Service | Select -First 2
Status Name DisplayName
------ ---- -----------
Stopped AarSvc_777b7 Agent Activation Runtime_777b7
Running AdobeARMservice Adobe Acrobat Update Service


PS E:\scripts\Powershell> wrongcommand wrongcommand : The term 'wrongcommand' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if
a path was included, verify that the path is correct and try again.
At line:1 char:1
+ wrongcommand
+ ~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (wrongcommand:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

上述命令应该记录在转录中。同时,你可以检查转录文件,以了解命令和输出是否被并行记录。

若要停止转录,你需要运行 Stop-Transcript 命令。

示例

PS E:\scripts\Powershell> Stop-Transcript
Transcript stopped, output file is E:\scripts\Powershell\Sessionrecording.txt

我们现在将检查当前位置存储的转录文件 sessionrecording.txt

**********************
Windows PowerShell transcript start
Start time: 20200711122407
Username: DESKTOP-9435KM9\admin
RunAs User: DESKTOP-9435KM9\admin
Configuration Name:
Machine: DESKTOP-9435KM9 (Microsoft Windows NT 10.0.18362.0)
Host Application: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Process ID: 4520
PSVersion: 5.1.18362.752
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.18362.752
BuildVersion: 10.0.18362.752
CLRVersion: 4.0.30319.42000
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
**********************
Transcript started, output file is .\Sessionrecording.txt
PS E:\scripts\Powershell> Get-Service | Select -First 2

Status Name DisplayName
------ ---- -----------
Stopped AarSvc_777b7 Agent Activation Runtime_777b7
Running AdobeARMservice Adobe Acrobat Update Service


PS E:\scripts\Powershell> wrongcommand
wrongcommand : The term 'wrongcommand' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ wrongcommand
+ ~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (wrongcommand:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
wrongcommand : The term 'wrongcommand' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if
a path was included, verify that the path is correct and try again.
At line:1 char:1
+ wrongcommand
+ ~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (wrongcommand:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

PS E:\scripts\Powershell> Stop-Transcript
**********************
Windows PowerShell transcript end
End time: 20200711124434
**********************

你可以在上述输出中注意到,执行 命令、计算机名、开始时间和结束时间 的用户也被记录下来了。

更新于: 11-11-2020

1K+ 浏览量

启动您的 职业

通过完成课程获得认证

开始
广告
© . All rights reserved.