我的PowerShell脚本包含工作流和foreach -parallel
。我在TimeoutSec
块的工作流程中设置了InlineScript
。
当我将睡眠命令设置为15秒时,我收到以下错误:
Microsoft.PowerShell.Utility\Write-Error : The activity has exceeded the specified maximum running time of 10 seconds. At create-new-vms:25 char:25 + + CategoryInfo : NotSpecified: (:) [Write-Error], TimeoutException + FullyQualifiedErrorId : System.TimeoutException,Microsoft.PowerShell.Commands.WriteErrorCommand + PSComputerName : [localhost]
我想将此错误写入日志文件(每个foreach
元素的每个错误一个文件)。这该怎么做?我必须在脚本中添加什么?
我的剧本:
$newvmlist = "test1", "test2"
workflow create-new-vms {
param(
[string[]]$vms
)
foreach -parallel ($vm in $vms) {
$run = InlineScript {
# Create New VM
echo " "
echo " "
echo "___"
echo "VM Name - $Using:vm "
echo "----"
echo " "
echo " "
sleep 15
} -PSActionRunningTimeoutSec 10
$run
}
}
create-new-vms -vms $newvmlist
答案 0 :(得分:2)
您可以使用ErrorVariable
:
create-new-vms -vms $newvmlist -ErrorVariable err
$err |% {$i=0}{ $i++; $_ | out-file "$i.log" }