PoshCode Logo PowerShell Code Repository

Start-Job proxy function by Jan Egil Ring 6 years ago (modification of post by Jan Egil Ring view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2492"></script>download | new post

Example on how to use Proxy Cmdlets in combination with object events.

For more information see:
http://blog.powershell.no/2011/02/07/powershell-using-proxy-cmdlets-in-combination-with-object-events

Update 07.02.2011: Updated the function, the first version didn`t work as intended.

  1. <#
  2.         Example on how to use Proxy Cmdlets in combination with object events.
  3.         For more information see:
  4.        
  5.         http://blog.powershell.no/2011/02/07/powershell-using-proxy-cmdlets-in-combination-with-object-events
  6.  
  7.         For more information about proxy functions, see the following article on the
  8.         Microsoft PowerShell Team blog:
  9.  
  10.         http://blogs.msdn.com/powershell/archive/2009/01/04/extending-and-or-modifing-commands-with-proxies.aspx
  11. #>
  12.  
  13. function Start-Job {
  14.         <#
  15.                 To create a proxy function for the Start-Job cmdlet, paste the results of the following command into the body of this function and then remove this comment:
  16.                 [Management.Automation.ProxyCommand]::Create((New-Object Management.Automation.CommandMetaData (Get-Command Start-Job)))
  17.         #>
  18.  
  19. [CmdletBinding(DefaultParameterSetName='ComputerName')]
  20. param(
  21.     [Parameter(ValueFromPipelineByPropertyName=$true)]
  22.     [System.String]
  23.     ${Name},
  24.  
  25.     [Parameter(ParameterSetName='ComputerName', Mandatory=$true, Position=0)]
  26.     [Alias('Command')]
  27.     [System.Management.Automation.ScriptBlock]
  28.     ${ScriptBlock},
  29.  
  30.     [System.Management.Automation.PSCredential]
  31.     ${Credential},
  32.  
  33.     [Parameter(ParameterSetName='FilePathComputerName', Position=0)]
  34.     [Alias('PSPath')]
  35.     [System.String]
  36.     ${FilePath},
  37.  
  38.     [System.Management.Automation.Runspaces.AuthenticationMechanism]
  39.     ${Authentication},
  40.  
  41.     [Parameter(Position=1)]
  42.     [System.Management.Automation.ScriptBlock]
  43.     ${InitializationScript},
  44.  
  45.     [Switch]
  46.     ${RunAs32},
  47.        
  48.         [System.Management.Automation.ScriptBlock]
  49.     ${OnCompletionAction},
  50.  
  51.     [Parameter(ValueFromPipeline=$true)]
  52.     [System.Management.Automation.PSObject]
  53.     ${InputObject},
  54.  
  55.     [Alias('Args')]
  56.     [System.Object[]]
  57.     ${ArgumentList})
  58.  
  59. begin
  60. {
  61.     try {
  62.         $outBuffer = $null
  63.         if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
  64.         {
  65.             $PSBoundParameters['OutBuffer'] = 1
  66.         }
  67.         $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Start-Job', [System.Management.Automation.CommandTypes]::Cmdlet)
  68.        
  69.                 # define string variable to become the target command line
  70.         #region Initialize helper variable to create command
  71.         $scriptCmdPipeline = ''
  72.         #endregion
  73.  
  74.         # add new parameter handling
  75.         #region Process and remove the Path parameter if it is present
  76.         if ($OnCompletionAction) {
  77.             $PSBoundParameters.Remove('OnCompletionAction') | Out-Null
  78.             $scriptCmdPipeline += " | foreach-object{
  79.    `$job = Register-ObjectEvent -InputObject `$_ -EventName StateChanged -SourceIdentifier JobEndAlert -Action {
  80.          if(`$sender.State -eq 'Completed')
  81.     {
  82.           `& {
  83.           $OnCompletionAction
  84.           }
  85.      Unregister-Event -SourceIdentifier JobEndAlert -Force
  86.     }
  87.    }        
  88.          }"
  89.         }
  90.         #endregion
  91.                
  92.                 $scriptCmd = {& $wrappedCmd @PSBoundParameters }
  93.                
  94.                   $scriptCmd = $ExecutionContext.InvokeCommand.NewScriptBlock(
  95.                 [string]$scriptCmd + $scriptCmdPipeline
  96.             )
  97.                
  98.         $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
  99.         $steppablePipeline.Begin($PSCmdlet)
  100.     } catch {
  101.         throw
  102.     }
  103. }
  104.  
  105. process
  106. {
  107.     try {
  108.         $steppablePipeline.Process($_)
  109.     } catch {
  110.         throw
  111.     }
  112. }
  113.  
  114. end
  115. {
  116.     try {
  117.         $steppablePipeline.End()
  118.     } catch {
  119.         throw
  120.     }
  121. }
  122. <#
  123.  
  124. .ForwardHelpTargetName Start-Job
  125. .ForwardHelpCategory Cmdlet
  126.  
  127. #>
  128.  
  129.  
  130.        
  131. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me