如何在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 **********************
您可以在上面的输出中注意到,还记录了执行命令,计算机名称,开始时间和结束时间的用户。