Calling additional scripts as jobs
I have a master script that is attempting to execute additional scripts against a list of servers it pulls from a table. I would like these additional scripts to be called asynchronously.
These additional scripts require a number of parameters that I cannot for the life of me get passed from the main script.
The child scripts all use the following parameters:
param(
[string]$sqlsrv=$null,
[string]$db=$null,
[string]$server=$null,
[string]$instance=$null
)
These values are generated by the master script. I can easily call anyone of the child scripts from the command line like so:
.\serverstatus.ps1 test test1 test2 test3 Where test,test1,test2,test3 are the parameter values.
The only way I am aware of to call these child scripts asynchronously is using Start-Job. I cannot get Start-Job to accept my parameters. This seems to get me close:
Start-Job serverstatus.ps1 $sqlsrv $db $server $instance
I get this error:
`Start-Job : Cannot bind parameter 'InitializationScript'.
Cannot convert the "TESTSERVER" value of type "System.String" to type System.Management.Automation.ScriptBlock".
At C:\SQLPS\ServerList.ps1:69 char:12
+ Start-Job <<<< serverstatus.ps1 $sqlsrv $db $server $instance
+ CategoryInfo : InvalidArgument: (:) [Start-Job], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.StartJobCommand`
I have tried many methods of passing these parameters based on information found online. I just can't seem to make any progress. Any guidance is greatly appreciated.
*EDIT*
Ok using your example, I am attempting to do the same. here are my results. The output of Receive-Job is what is killing me. I can't seem to get anything meaningful from it.
ProcessServers.ps1
{Gathers a list of servers and instances}
Start-Job -Fi开发者_JAVA技巧lePath .\serverstatus.ps1 -ArgumentList $sqlsrv,$destdb,$server,$instance
If I just write-output on the Start-Job line, all variables are properly populated as expected. Output looks like so:
Start-Job -FilePath .\GetServerDetails.ps1 -ArgumentList RADC00,ServerHistory,RADCHR01,RADCHR01
For testing purposes, I have modified GetServerDetails.ps1 to simply output the passed parameters. It looks as so:
GetServerDetails.ps1
param($sqlsrv,$db,$server,$instance)
"Parameters are: $sqlsrv, $destdb, $server, $instance"
So from the command line I run:
Start-Job -FilePath .\GetServerDetails.ps1 -ArgumentList RADC00,ServerHistory,RADCHR01,RADCHR01
Instead of output like you get, I get this:
PS C:\SQLPS> Start-Job -FilePath .\GetServerDetails.ps1 -ArgumentList RADC00,ServerHistory,RADCHR01,RADCHR01
Id Name State HasMoreData Location Command
-- ---- ----- ----------- -------- -------
37 Job37 Running True localhost param($sqlsrv,$destdb,...
PS C:\SQLPS> Receive-Job -id 37
PS C:\SQLPS>
Use the -ArgumentList
parameter on Start-Job
to pass in your parameters. The ArgumentList
parameter should be the last parameter for that command e.g.:
PS> Get-Content script.ps1
param($p1, $p2, $p3)
"p1 is $p1, p2 is $p2, p3 is $p3"
PS> Start-Job -FilePath .\script.ps1 -ArgumentList 1,"2",(get-date)
Id Name State HasMoreData Location Command
-- ---- ----- ----------- -------- -------
1 Job1 Running True localhost param($p1, $p2, $p3)...
PS> Receive-Job -id 1
p1 is 1, p2 is 2, p3 is 03/11/2011 16:42:26
精彩评论