处理错误以在工作流中归档

Handle errors to file in Workflow

我有带工作流的 PowerShell 脚本和 foreach -parallel。我在 InlineScript 块的工作流程中设置了 TimeoutSec

当我将睡眠命令设置为 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

您可以使用 ErrorVariable:

create-new-vms -vms $newvmlist -ErrorVariable err
$err |% {$i=0}{ $i++; $_ | out-file "$i.log" }