我用无限循环执行我的脚本,并存储日志。但它返回此错误
+ $Log = Start-Transcript -Path $Log_Path -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Start-Transcript], PSInvalidOperationException
+ FullyQualifiedErrorId : CannotStartTranscription,Microsoft.PowerShell.Commands.StartTranscriptCommand
这是我的代码
for(;;)
{
# CONFIG FILE LOAD
#============================================#
[xml]$Config_File = Get-Content .Config.xml
# LOG
#============================================#
$Log_Path = $Config_File.Automation_Config.Path.Log
$Log = Start-Transcript -Path $Log_Path -Force
Get-Date
Write-Host ">>Log_File: $Log"
######## DOING SOME PROCESS ########
}
你不能Start-Transcript
两次。使用Stop-Transcript
.
如果不确定是否正在运行听录,请创建一个空try-catch
:
try { Stop-Transcript -ErroAction Stop } catch { <# Do Nothing #> }
就我个人而言,在交互式测试自动脚本时,我在脚本开始时使用这种try-catch
结构,并且必须在中间手动停止它们。
重要说明:在 Windows 8、8.1、2012、2012R2 上使用脚本将Write-Host
记录到日志文件中。使用Write-Output
或阅读本文并安装KB3014136