我使用start-job创建一个powershell脚本作为后台作业,然后使用start-transcript记录它的输出.以下是两者的代码:
一个
start-job -filepath ./b.ps1 -ArgumentList 'test.txt'
wait-job *
Run Code Online (Sandbox Code Playgroud)
b
param([parameter(Mandatory = $true)][string]$logfile)
Set-PSDebug -Strict
$ErrorActionPreference = 'Stop'
start-transcript $logfile
Run Code Online (Sandbox Code Playgroud)
输出./a.ps1
Id Name State HasMoreData Location Command
-- ---- ----- ----------- -------- -------
1 Job1 Running True localhost param...
1 Job1 Failed False localhost param...
2 Job2 Failed False localhost param...
Run Code Online (Sandbox Code Playgroud)
输出./b.ps1 -log c:\ test.txt
Transcript started, output file is test.txt
Transcript stopped, output file is C:\test.txt
Run Code Online (Sandbox Code Playgroud)
我还通过设置"echo here"行来做一些测试,以确认是播放的行.
显然,后台作业不支持Start-Transcript.如果您执行Receive-Job查看脚本输出,您可能会看到如下错误:
This host does not support transcription.
+ CategoryInfo : NotImplemented: (:) [Start-Transcript], PSNotSupportedException
+ FullyQualifiedErrorId : NotSupported,Microsoft.PowerShell.Commands.StartTranscriptCommand
Run Code Online (Sandbox Code Playgroud)
在PowerShell 2.0中我推荐你切换Set-PSDebug -strict到Set-StrictMode -Version 2.0- 它会捕获更多潜在的错误.
| 归档时间: |
|
| 查看次数: |
2267 次 |
| 最近记录: |