如何在 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.