Sign in
|
Join PowerShell.com!
|
Help
Home
PowerTips
Ask the Experts
Forums
Webcasts
Blogs
eBook
Script Library
Twitter Grid
Modules
QuickClick
Scripts
Snippets
Videos
Library
»
Script Library
»
Desktop
»
Slideshow
Desktop Slideshow
Share
|
Browse Library
Module Library
QuickClick Library
Script Library
Active Directory
BizTalk
Citrix
Clustering
Desktop
Exchange Server 2003
Exchange Server 2007
File System
Group Policy
Internet Information Server (IIS)
Local Accounts
Logs
Microsoft Office
Microsoft Team Foundation Server
MySQL
Networking
Registry
Remote Desktop Services
Remoting
Security
SharePoint
SQL Server
System Center Virtual Machine Manager
System Center Configuration Manager
System Center Operations Manager
Tutorial
Terminal Server
Using .Net
Virtual Server
VMware
Windows 7
Windows HPC
Windows Server 2000
Windows Server 2003
Windows Server 2008
Windows XP
WMI
Misc
Snippet Library
Video Library
Members Only
Run and time a script or command in the background
##############################################################################
##
## RunACommandInTheBackground
##
## By Carsten Schwartz
##
##############################################################################
<
#
.
SYNOPSIS
Run a command orpowershell script with any number of parameters
in
the
background.
.
DESCRIPTION
Runs a command or powershell script with any number of parameters
in
the
background. The script creates a job runs it and shows start, stop and
elapsed time when the job is terminated. The job is also removed from the
Job list on termination.
.
EXAMPLE
PS >.\
RunACommandInTheBackground.ps1
.\
MakeRelease.ps1
https:
//
svnserver:8443
/
svn
/
OurProduct
/
trunk 613
Starting .\
MakeRelease.ps1
https:
//
svnserver:8443
/
svn
/
OurProduct
/
trunk 613 as Job
#1...
PS > Job
#1 is finished...
Job Started:
10
/
29
/
2010 07:51:34
Job Stopped:
10
/
29
/
2010 07:54:39
Time Elapsed: 00:03:05.3862306
PS >
Get-Job
PS >
#>
function
start-jobhere
(
$sCommand
){
start-job
-argumentlist
(
get-location
),
$sCommand
{
set-location
$args
[0];
invoke-expression
$args
[1] }
-Name
BackgroundRunner
}
if
(
$args
.
length
-gt
0)
{
$sScriptCall
=
$args
[0]
for (
$Count
=
1;
$Count
-le
$args
.
length
;
$Count
++
)
{
$sScriptCall
+=
(
" "
+
$args
[
$Count
])
}
$job
=
start-jobhere
$sScriptCall
"Starting "
+
$sScriptCall
+
"as Job #"
+
$job
.
Id
+
"..."
$creationDate
=
[
string
] (
Get-Date
)
Register-ObjectEvent
$job
StateChanged
-MessageData
$creationDate
-Action
{
[
Console
]::
Beep
(100,100)
Write-Host
"Job #$($sender.Id) is finished..."
$CreationTime
=
[
datetime
]
$event
.
MessageData
$TerminationTime
=
[
datetime
]
$event
.
TimeGenerated
$ElapsedTime
=
$TerminationTime
-
$CreationTime
Write-Host
(
"Job Started:
{0}"
-f
$CreationTime
)
Write-Host
(
"Job Stopped:
{0}"
-f
$TerminationTime
)
Write-Host
(
"Time Elapsed: {0}"
-f
$ElapsedTime
)
Write-Host
(prompt)
-NoNewline
$eventSubscriber
|
Unregister-Event
$eventSubscriber
.
Action
|
Remove-Job
Remove-Job
$sender
.
Id
} |
Out-Null
}
Loading...
Run and time...
Generic Delete...
Open DialogBox...
Open DialogBox...
View all files
Copyright 2011 PowerShell.com. All rights reserved.