Start-Transcript
创建指向文本文件的 PowerShell 会话的所有或部分的记录。
语法
ByPath (默认值)
Start-Transcript
[[-Path] <String>]
[-Append]
[-Force]
[-NoClobber]
[-IncludeInvocationHeader]
[-UseMinimalHeader]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
ByLiteralPath
Start-Transcript
[[-LiteralPath] <String>]
[-Append]
[-Force]
[-NoClobber]
[-IncludeInvocationHeader]
[-UseMinimalHeader]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
ByOutputDirectory
Start-Transcript
[[-OutputDirectory] <String>]
[-Append]
[-Force]
[-NoClobber]
[-IncludeInvocationHeader]
[-UseMinimalHeader]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
说明
Start-Transcript cmdlet 创建指向文本文件的所有或部分 PowerShell 会话的记录。 脚本包括用户键入的所有命令以及控制台上显示的所有输出。
默认情况下,Start-Transcript 使用默认名称将脚本存储在以下位置:
- 在 Windows 上:
$HOME\Documents - 在 Linux 或 macOS 上:
$HOME
默认文件名为 PowerShell_transcript.<computername>.<random>.<timestamp>.txt。
从 Windows PowerShell 5.0 开始,Start-Transcript 包括所有脚本生成的文件名中的主机名。 文件名还包含名称中的随机字符,以防止同时启动两个或多个脚本时发生覆盖或重复。 如果将脚本存储在集中位置,则包括计算机名称非常有用。 随机字符串可防止猜测文件名以获取对文件的未经授权的访问。
如果目标文件没有字节顺序标记(BOM),Start-Transcript 默认在目标文件中 Utf8NoBom 编码。
示例
示例 1:使用默认设置启动脚本文件
Start-Transcript
此命令在默认文件位置启动脚本。
示例 2:在特定位置启动脚本文件
Start-Transcript -Path "C:\transcripts\transcript0.txt" -NoClobber
此命令在 Transcript0.txt的 C:\transcripts 文件中启动脚本。
NoClobber 参数可防止覆盖任何现有文件。 如果 Transcript0.txt 文件已存在,该命令将失败。
示例 3:使用唯一名称启动脚本文件并将其存储在文件共享上
以下示例创建一个脚本文件,该文件的名称唯一,足以存储在共享位置。 文件名基于用户名、运行 PowerShell 的计算机的主机名、PowerShell 的版本以及日期和时间构造。 脚本存储在 \\Server01\Transcripts 文件共享中。
$sharePath = '\\Server01\Transcripts'
$username = $Env:USERNAME
$hostname = hostname
$version = $PSVersionTable.PSVersion.ToString()
$datetime = Get-Date -F 'yyyyMMddHHmmss'
$filename = "Transcript-${username}-${hostname}-${version}-${datetime}.txt"
$Transcript = (Join-Path -Path $sharePath -ChildPath $filename).ToString()
Start-Transcript
脚本文件的完整路径存储在 $Transcript 首选项变量中。 有关 $Transcript 首选项变量的详细信息,请参阅 about_Preference_Variables。
参数
-Append
指示此 cmdlet 将新脚本添加到现有文件的末尾。 使用 Path 参数指定文件。
参数属性
| 类型: | SwitchParameter |
| 默认值: | None |
| 支持通配符: | False |
| 不显示: | False |
参数集
(All)
| Position: | Named |
| 必需: | False |
| 来自管道的值: | False |
| 来自管道的值(按属性名称): | False |
| 来自剩余参数的值: | False |
-Confirm
在运行 cmdlet 之前,提示你进行确认。
参数属性
| 类型: | SwitchParameter |
| 默认值: | False |
| 支持通配符: | False |
| 不显示: | False |
| 别名: | cf |
参数集
(All)
| Position: | Named |
| 必需: | False |
| 来自管道的值: | False |
| 来自管道的值(按属性名称): | False |
| 来自剩余参数的值: | False |
-Force
允许 cmdlet 将脚本追加到现有的只读文件。 在只读文件上使用时,cmdlet 会将文件权限更改为读写。 使用此参数时,cmdlet 无法替代安全限制。
参数属性
| 类型: | SwitchParameter |
| 默认值: | None |
| 支持通配符: | False |
| 不显示: | False |
参数集
(All)
| Position: | Named |
| 必需: | False |
| 来自管道的值: | False |
| 来自管道的值(按属性名称): | False |
| 来自剩余参数的值: | False |
-IncludeInvocationHeader
指示此 cmdlet 在运行命令时记录时间戳。
参数属性
| 类型: | SwitchParameter |
| 默认值: | None |
| 支持通配符: | False |
| 不显示: | False |
参数集
(All)
| Position: | Named |
| 必需: | False |
| 来自管道的值: | False |
| 来自管道的值(按属性名称): | False |
| 来自剩余参数的值: | False |
-LiteralPath
指定脚本文件的位置。 与 Path 参数不同,LiteralPath 参数的值与类型化完全相同。 不会将任何字符解释为通配符。 如果路径包含转义字符,请将它括在单引号中。 单引号通知 PowerShell 不要将任何字符解释为转义序列。
参数属性
| 类型: | String |
| 默认值: | None |
| 支持通配符: | False |
| 不显示: | False |
| 别名: | PSPath, LP |
参数集
ByLiteralPath
| Position: | 0 |
| 必需: | False |
| 来自管道的值: | False |
| 来自管道的值(按属性名称): | False |
| 来自剩余参数的值: | False |
-NoClobber
指示此 cmdlet 不会覆盖现有文件。 默认情况下,如果指定路径中存在脚本文件,Start-Transcript 覆盖该文件而不发出警告。
参数属性
| 类型: | SwitchParameter |
| 默认值: | None |
| 支持通配符: | False |
| 不显示: | False |
| 别名: | NoOverwrite |
参数集
(All)
| Position: | Named |
| 必需: | False |
| 来自管道的值: | False |
| 来自管道的值(按属性名称): | False |
| 来自剩余参数的值: | False |
-OutputDirectory
指定要在其中保存脚本的特定路径和文件夹。 PowerShell 会自动分配脚本名称。
参数属性
| 类型: | String |
| 默认值: | None |
| 支持通配符: | False |
| 不显示: | False |
参数集
ByOutputDirectory
| Position: | 0 |
| 必需: | False |
| 来自管道的值: | False |
| 来自管道的值(按属性名称): | False |
| 来自剩余参数的值: | False |
-Path
指定脚本文件的位置。 输入 .txt 文件的路径。 不允许使用通配符。 如果路径中的任何目录不存在,命令将失败。
如果未指定路径,Start-Transcript 将使用 $Transcript 全局变量的值中的路径。 如果尚未创建此变量,Start-Transcript 将脚本存储在默认位置和文件名中。
参数属性
| 类型: | String |
| 默认值: | None |
| 支持通配符: | False |
| 不显示: | False |
参数集
ByPath
| Position: | 0 |
| 必需: | False |
| 来自管道的值: | False |
| 来自管道的值(按属性名称): | False |
| 来自剩余参数的值: | False |
-UseMinimalHeader
在脚本前面添加一个简短的标头,而不是默认包含的详细标头。 此参数已在 PowerShell 6.2 中添加。
参数属性
| 类型: | SwitchParameter |
| 默认值: | None |
| 支持通配符: | False |
| 不显示: | False |
参数集
(All)
| Position: | Named |
| 必需: | False |
| 来自管道的值: | False |
| 来自管道的值(按属性名称): | False |
| 来自剩余参数的值: | False |
-WhatIf
显示 cmdlet 运行时会发生什么情况。 cmdlet 未能运行。
参数属性
| 类型: | SwitchParameter |
| 默认值: | False |
| 支持通配符: | False |
| 不显示: | False |
| 别名: | 无线 |
参数集
(All)
| Position: | Named |
| 必需: | False |
| 来自管道的值: | False |
| 来自管道的值(按属性名称): | False |
| 来自剩余参数的值: | False |
CommonParameters
此 cmdlet 支持通用参数:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 有关详细信息,请参阅 about_CommonParameters。
输入
None
不能用管道将对象传送到此 cmdlet。
输出
String
此 cmdlet 返回一个字符串,其中包含确认消息和输出文件的路径。
备注
若要停止脚本,请使用 Stop-Transcript cmdlet。
若要记录整个会话,请将 Start-Transcript 命令添加到配置文件。 有关详细信息,请参阅 about_Profiles。