When you do some work, you may be asked for a work record. Sometimes referred to as evidence or trails, it's important to keep track of what you did and what happened. When working with Powershell, keep the following two records:
A record of what was output when the command was executed. There are several ways to record output with Powerehll.
In addition to this, there is a method of selecting the content output on the screen and saving it by copying, but the explanation is omitted.
History of executed commands. How to get command execution history in Powershell
-Copy history file ・ Get-History
Use> (redirect) to output the executed content as it is to text. Orthodox method.
[command] > [text file].txt
Specify the text file to be output by adding> after the command.
PS > echo "Sea urchin" > test.txt PS > Get-Content test.txt Sea urchin
The redirected text file will be overwritten. If you want to add it, redirect with >>.
PS > echo "Sea urchin" >> test.txt
When using a redirect, the command execution result is not displayed on the screen. If you want to display it on the screen, use Tee-Object.
PS > echo "Sea urchin" | Tee-Object test.txt Sea urchin PS > Get-Content test.txt Sea urchin
Execute the following command when starting and ending the recording of text.
Start-Transcript → Start recording Stop-Transcript → End of recording
** Start recording **
> Start-Transcript The transcript has started. Output file: [Current path]\PowerShell_transcript.HORUS.Wm+ZpDyv.[Date and time].txt
Recording starts after the command is executed.
** End of recording **
> Stop-Transcript The transcript has been stopped. Output file: [Current path]\Documents\PowerShell_transcript.HORUS.Wm+ZpDyv.[Date and time].txt
Recording is stopped after the command is executed. Various information is recorded in the header of the saved text file. The header of the output file is as follows.
** Output content **
********************** Start Windows PowerShell transcript Start time: 20200111180312 username: [User] RunAs user: [User] Configuration name: computer: [hostname](Microsoft Windows NT 10.0.18362.) Host application: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe Process ID: 13508 PSVersion: 5.1.18362.145 PSEdition: Desktop PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.18362.145 BuildVersion: 10.0.18362.145 CLRVersion: 4.0.30319.42000 WSManStackVersion: 3.0 PSRemotingProtocolVersion: 2.3 SerializationVersion: 220.127.116.11 **********************
Information such as the start time, end time, and host name of recording is automatically entered.
Pipe the output of the command to Export-Csv. The output is saved in the file attached to the option.
<command> | Export-Csv <file name>.csv
Since the output output to the screen is formatted in CSV, the data that is not displayed is not saved. The output CSV can be read as it is with Excel or the like.
Pipe the output of the command to Export-Clixml. The output is saved in the file attached to the option.
<command> | Export-Clixml <file name>.xml
Import the xml file output by the following command.
Import-Clixml <file name>.xml
After importing, you will see the exported output. Put the output in a variable or process it through a pipe to utilize it.
When you execute a command with Powershell, the executed command is output to a text file as a history and saved. You can check the location of the saved file by executing the following command.
Also, it seems that you can change the output location with the following command.
Set-PSReadLineOption -HistorySavePath [File path to change]
However, once you close the prompt, it will revert to the default location.
You can display the command execution history with the ** Get-history ** command. 。
PS C:\Users\Main> Get-History Id CommandLine -- ----------- 1 Get-History 2 Get-History
If you have properties, you can also display command execution history, execution time, and end time data.
|ExecutionStatus||Command execution result|
|StartExecutionTime||Command execution start time|
|EndExecutionTime||Command execution end time|
When all properties are displayed with Select-Object *, this information is displayed.
PS C:\Users\Main> Get-History | Select-Object * Id : 1 CommandLine : Get-History | Select-Object * ExecutionStatus : Completed StartExecutionTime : 2020/01/16 19:54:41 EndExecutionTime : 2020/01/16 19:54:41 Id : 2 CommandLine : Get-History | Select-Object * ExecutionStatus : Completed StartExecutionTime : 2020/01/16 19:54:43 EndExecutionTime : 2020/01/16 19:54:43
With Powershell, you can combine command execution history and output history to reliably record when and what kind of data was acquired.