title: Set-PSDebug -Trace 2 is causing my powershell code to throw an error that doesn't occur without tracing on
body: <p>Imagine a script <code>test.ps1</code> that looks like this:</p>
  
  <pre><code>Param (
      [ValidateNotNullOrEmpty()]
      $VMHost,
  
      [switch]
      $trace
  )
  
  $ErrorActionPreference = "Stop"
  Set-StrictMode -Version 2
  
  trap {
      Set-PSDebug -Trace 0
      $_.InvocationInfo
      break
  }
  
  if ($trace) {
      Set-PSDebug -Trace 2
  }
  
  if ($VMHost) {
      $VMHost = @(@($VMHost) | % {
          $h = $_
          Switch ($h.GetType().Name) {
              'String' {
                  "isastring"
              }
              default {
                  Write-Error -Category InvalidType -TargetObject $h -ErrorAction Stop -Message 'VMHost argument must be a String or a VMHost object.'
              }
          }
      })
  }
  </code></pre>
  
  <p>When run normally it does this:</p>
  
  <pre><code>PS&gt; &amp; .\test.ps1 -VMHost "foo"
  foo
  </code></pre>
  
  <p>When run with the <code>-trace</code> argument however it does this:</p>
  
  <pre><code>PS&gt; &amp; .\test.ps1 -VMHost "foo" -trace
  DEBUG:   21+ if ( &gt;&gt;&gt;&gt; $VMHost) {
  DEBUG:   22+      &gt;&gt;&gt;&gt; $VMHost = @(@($VMHost) | % {
  DEBUG:   22+     $VMHost = @( &gt;&gt;&gt;&gt; @($VMHost) | % {
  DEBUG:   22+     $VMHost = @(@( &gt;&gt;&gt;&gt; $VMHost) | % {
  DEBUG:   22+     $VMHost = @(@($VMHost) | %  &gt;&gt;&gt;&gt; {
  DEBUG:     ! CALL function '&lt;ScriptBlock&gt;'  (defined in file 'path...\test.ps1')
  DEBUG:   23+          &gt;&gt;&gt;&gt; $h = $_
  DEBUG:     ! SET $h = 'foo'.
  DEBUG:     ! SET $switch = 'String'.
  DEBUG:   24+         Switch ( &gt;&gt;&gt;&gt; $h.GetType().Name) {
  DEBUG:     ! SET $switch = ''.
  DEBUG:   35+      &gt;&gt;&gt;&gt; })
  DEBUG:     ! SET $VMHost = ''.
  DEBUG:   13+      &gt;&gt;&gt;&gt; Set-PSDebug -Trace 0
  DEBUG:     ! CALL function '&lt;ScriptBlock&gt;&lt;trap&gt;'  (defined in file
  'path...\test.ps1')
  
  
  MyCommand             :
  BoundParameters       : {}
  UnboundArguments      : {}
  ScriptLineNumber      : 23
  OffsetInLine          : 19
  HistoryId             : -1
  ScriptName            : path...\test.ps1
  Line                  :     $VMHost = @(@($VMHost) | % {
  
  PositionMessage       : At path...\test.ps1:23 char:19
                          +     $VMHost = @(@($VMHost) | % {
                          +                   ~~~~~~~
  PSScriptRoot          : C:\Users\ereisner\TenableAppliancePackaging
  PSCommandPath         : C:\Users\ereisner\TenableAppliancePackaging\test.ps1
  InvocationName        :
  PipelineLength        : 0
  PipelinePosition      : 0
  ExpectingInput        : False
  CommandOrigin         : Internal
  DisplayScriptPosition :
  
  The variable cannot be validated because the value System.Object[] is not a valid value for the VMHost variable.
  At path...\test.ps1:22 char:19
  +     $VMHost = @(@($VMHost) | % {
  +                   ~~~~~~~
      + CategoryInfo          : MetadataError: (:) [], ParentContainsErrorRecordException
      + FullyQualifiedErrorId : ValidateSetFailure
  </code></pre>
  
  <p>How is trace debugging causing an error like this?</p>
  
  <p>What is powershell doing here?</p>
  
  <p>Can this filter/assignment be cleanly rewritten to avoid this problem (bug?)?</p>
  
  <p>Powershell version info:</p>
  
  <pre><code>PS&gt; $PSVersionTable
  Name                           Value
  ----                           -----
  PSVersion                      3.0
  WSManStackVersion              3.0
  SerializationVersion           1.1.0.1
  CLRVersion                     4.0.30319.42000
  BuildVersion                   6.2.9200.16481
  PSCompatibleVersions           {1.0, 2.0, 3.0}
  PSRemotingProtocolVersion      2.2
  </code></pre>