Desktop Slideshow

  1. ############################################################################## 
  2. ## 
  3. ## RunACommandInTheBackground 
  4. ## 
  5. ## By Carsten Schwartz 
  6. ## 
  7. ############################################################################## 
  8.  
  9. <# 
  10.  
  11. .SYNOPSIS 
  12.  
  13. Run a command orpowershell script with any number of parameters in the 
  14. background. 
  15.  
  16. .DESCRIPTION 
  17.  
  18. Runs a command or powershell script with any number of parameters in the 
  19. background. The script creates a job runs it and shows start, stop and 
  20. elapsed time when the job is terminated. The job is also removed from the 
  21. Job list on termination. 
  22.  
  23. .EXAMPLE 
  24.  
  25. PS >.\RunACommandInTheBackground.ps1 .\MakeRelease.ps1 https://svnserver:8443/svn/OurProduct/trunk 613 
  26. Starting .\MakeRelease.ps1 https://svnserver:8443/svn/OurProduct/trunk 613 as Job #1...  
  27. PS > Job #1 is finished... 
  28. Job Started:  10/29/2010 07:51:34 
  29. Job Stopped:  10/29/2010 07:54:39 
  30. Time Elapsed: 00:03:05.3862306 
  31. PS > Get-Job 
  32. PS > 
  33.  
  34. #> 
  35.  
  36. function start-jobhere($sCommand){ 
  37.   start-job -argumentlist (get-location),$sCommand { set-location $args[0]; 
  38.   invoke-expression $args[1] } -Name BackgroundRunner 
  39.  
  40. if ($args.length -gt 0) 
  41.   $sScriptCall = $args[0]  
  42.   for ($Count=1; $Count -le $args.length; $Count++
  43.   
  44.     $sScriptCall += (" " + $args[$Count]) 
  45.   
  46.   $job = start-jobhere $sScriptCall 
  47.   "Starting " + $sScriptCall + "as Job #" + $job.Id + "..." 
  48.    
  49.   $creationDate = [string] (Get-Date
  50.   Register-ObjectEvent $job StateChanged -MessageData $creationDate -Action
  51.     [Console]::Beep(100,100) 
  52.     Write-Host "Job #$($sender.Id) is finished..." 
  53.     $CreationTime    = [datetime] $event.MessageData 
  54.     $TerminationTime = [datetime] $event.TimeGenerated 
  55.     $ElapsedTime     = $TerminationTime - $CreationTime 
  56.     Write-Host ("Job Started:  {0}" -f $CreationTime
  57.     Write-Host ("Job Stopped:  {0}" -f $TerminationTime
  58.     Write-Host ("Time Elapsed: {0}" -f $ElapsedTime
  59.     Write-Host (prompt) -NoNewline 
  60.     $eventSubscriber | Unregister-Event 
  61.     $eventSubscriber.Action | Remove-Job 
  62.     Remove-Job $sender.Id 
  63.   } | Out-Null 
 
Loading...
Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.