# Problem replace constant with variable

**URL:** <https://forums.powershell.org/t/problem-replace-constant-with-variable/4990>\
**Category:** PowerShell Help\
**Created:** [October 7, 2015, 11:58pm UTC](https://forums.powershell.org/t/problem-replace-constant-with-variable/4990 "2015-10-07T23:58:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ja-le](https://avatars.discourse-cdn.com/v4/letter/j/ba9def/32.png) [@ja-le](https://forums.powershell.org/u/ja-le)\
**Post date:** [October 7, 2015, 11:58pm UTC](https://forums.powershell.org/t/problem-replace-constant-with-variable/4990/1 "2015-10-07T23:58:21Z")

</div>

I have two problems:

1. 

I try to start a PERL program from a PowerShell. If I use a constant, it starts:

$Job1 = Start-Job { & “C:/Temp/Pause.pl” 10 }

If I try to construct my program name with a variable, it does not work:

$Cmd = “C:/Temp/Pause.pl”;  
$Job1 = Start-Job { & $Cmd 10 }

#2  
My PERL program looks like this, it basically justs waits for the number of seconds received as a parameter:

my $Delay = $ARGV[0];  
print “Start $Delay\n”;  
sleep $Delay;  
print “End $Delai\n”;

When I start my PERL with this command, and I monitor the job’s state, I would expect to see RUNNING for the duration of the execution of my PERL script, but the job indicates COMPETED after only 1-2 seconds:

$Job1 = Start-Job { & “C:/Temp/Pause.pl” 10 }  
For ($i=1; $i -le 15; $i++)  
{  
Get-Job  
Start-Sleep -Seconds 1  
}

Why does it indicate completed right away, even when my jobs running a PERL program is still running?

---

<div class="post-metadata">

**Author:** ![sebastian-n](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@sebastian-n](https://forums.powershell.org/u/sebastian-n)\
**Post date:** [October 8, 2015, 12:33am UTC](https://forums.powershell.org/t/problem-replace-constant-with-variable/4990/2 "2015-10-08T00:33:56Z")

</div>

The variable isn’t inside the scriptblock “{ }”. You need to pass the value with a param() block inside the scriptblock or use the $using: prefix.

---

<div class="post-metadata">

**Author:** ![ja-le](https://avatars.discourse-cdn.com/v4/letter/j/ba9def/32.png) [@ja-le](https://forums.powershell.org/u/ja-le)\
**Post date:** [October 8, 2015, 1:12am UTC](https://forums.powershell.org/t/problem-replace-constant-with-variable/4990/3 "2015-10-08T01:12:52Z")

</div>

OK, I am a newbie with PowerShell, I did not know { } created a new session where local variables where no longer available. Now I upgraded my program to this. It launches 3 instances of my PERL program, but it shows all 3 jobs as completed, while they are still running, SHY?

Get-Job | Remove-Job

$Cmd = “$PSScriptRoot\Pause.pl”  
Write-Host “Starting jobs`n”;  
$Job1 = Start-Job { & $Using:Cmd 10 }  
$job2 = Start-Job { & $Using:Cmd 20 }  
$Job3 = Start-Job { & $Using:Cmd 30 }

Write-Host “Waiting for jobs`n”  
for ($i=1; $i -le 15; $i++)  
{  
Write-Host "— " $i  
Get-Job  
Start-Sleep -Seconds 1  
}

Write-Host “Done`n”

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:44pm UTC](https://forums.powershell.org/t/problem-replace-constant-with-variable/4990/4 "2024-05-16T20:44:41Z")

</div>


