2

I've investigated this forum. But do not seem to understand it good enough to find my answer. I'm absolutely new at Powershell.

I've a situation that I've started a ps1-script and need to execute another ps1-script to start in the background with passing parameters from the first script to the second.

Something like this (script1.ps1):

$var1 = "p1 (string value)"
$var2 = "p2 (string value)"
$var3 = "p3 (string value)"
$var4 = 4 # numeric value

$counter = 1

while ($counter -le $var4)
{
    #call to script2.ps1 with passing the $var1 through $var4 as
    #parameters so that script2.ps1 executes as a background process
    #and works with the received parameters $var1 through $var4
    # ??
    $counter= $counter + 1
}

exit
########################################

I can't find 1 way to pass the parameters and start script2.ps1 in the background.

Really hoping that someone can help me.

THIA, Wim

1
  • why don't you want to pass them as array and just process them one by one in the script? Commented Apr 25, 2017 at 17:21

2 Answers 2

1

This will help you About_Jobs

Get-Help Start-Job

https://sqlblog.org/2011/01/29/powershell-start-job-scriptblock-sad-panda-face

Sign up to request clarification or add additional context in comments.

Comments

1

This did the trick for me.

$var1 = "p1 (string value)"
$var2 = "p2 (string value)"
$var3 = "p3 (string value)"
$var4 = 4 # numeric value

$counter = 1
while ($counter -le $var4)
{

    $Params = "-Param1 $var1 -Param2 $var2 -Param3 $var3"
    $Script = [scriptblock]::create("d:\temp\scriptname.ps1 $Params")
    Start-Job -ScriptBlock $Script

    $counter = $counter+1
}

exit

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.