Chained AddScript methods breaks pipeline


#Chained .AddScript()

$Scriptblock = 
{
  $VerbosePreference = 'Continue'
  $DebugPreference   = 'Continue'

  Write-Verbose 'Verbose Message'
  Write-Debug 'Debug Message'
  Write-Output 'Pipeline Output'
}


$LogStart =
 {
   &{ 
      $DebugPreference = 'Continue'
      Write-Debug "Start(Ticks) = $((get-date).Ticks)"
    }
 }

$LogEnd =
 {
   &{
      $DebugPreference = 'Continue' 
      Write-Debug "End(Ticks) = $((get-date).Ticks)"
    }
 }


$RunspacePool = [RunspaceFactory]::CreateRunspacePool(1,1)
$RunspacePool.Open()

     $Job = [powershell]::Create().
              AddScript($LogStart).
              AddScript($ScriptBlock).
              AddScript($LogEnd)

     $Job.RunspacePool = $RunspacePool
     $job =
     New-Object PSObject -Property @{
                                     Pipe = $Job
                                     Result = $Job.BeginInvoke()
                                    }
     
 
 
  Do {} Until ($Job.Result.IsCompleted) 

  $Job.Pipe.Streams
  $Job.Pipe.EndInvoke($Job.Result)
  $Job.Pipe.Dispose()
  $RunspacePool.Close()
  $RunspacePool.Dispose()

''
'#'*20
''

#Single AddScript

  $Scriptblock = 
{
  $VerbosePreference = 'Continue'
  $DebugPreference   = 'Continue'

  Write-Verbose 'Verbose Message'
  Write-Debug 'Debug Message'
  Write-Output 'Pipeline Output'
}

$LogStart =
@'
   &{ 
      $DebugPreference = 'Continue'
      Write-Debug "Start(Ticks) = $((get-date).Ticks)"
    }
'@

$LogEnd =
@'
   &{
      $DebugPreference = 'Continue' 
      Write-Debug "End(Ticks) = $((get-date).Ticks)"
    }
'@

$JobScript = 
[Scriptblock]::Create($LogStart + $Scriptblock + $LogEnd)

$RunspacePool = [RunspaceFactory]::CreateRunspacePool(1,1)
$RunspacePool.Open()

     $Job = [powershell]::Create().AddScript($JobScript)

     $Job.RunspacePool = $RunspacePool
     $job =
     New-Object PSObject -Property @{
                                     Pipe = $Job
                                     Result = $Job.BeginInvoke()
                                    }
     
 
 
  Do {} Until ($Job.Result.IsCompleted) 

  $Job.Pipe.Streams
  $Job.Pipe.EndInvoke($Job.Result)
  $Job.Pipe.Dispose()
  $RunspacePool.Close()
  $RunspacePool.Dispose()

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s