2

I have a shell script which should start a .exe in the background:

$strPath = get-location
$block = {& $strPath"\storage\bin\storage.exe" $args}
start-job -scriptblock $block -argumentlist "-f", $strPath"\storage\conf\storage.conf"

In a preceding Question I found out that I need absolute Paths. However the $strPath variable isn't interpreted, if you look at the command:

PS Q:\mles\etl-i_test> .\iprog.ps1 --start1
Start Storage

Id              Name            State      HasMoreData     Location             Command
--              ----            -----      -----------     --------             -------
37              Job37           Running    True            localhost            & $strPath"\storage\bi...

How can I fix this?

Edit: I understand I need to pass the path as an argument, and how? Something like:

$block = {& $args[0]"\storage\bin\storage.exe" $args[1] $args[2]}
start-job -scriptblock $block -argumentlist $strPath, "-f", $strPath"\storage\conf\storage.conf"

?

1
  • In $block the variable $strPath is empty, because the scriptblock is a whole new script. Your scriptblock needs to accept arguments. Oh yeah, Don't use Hungarian Notation. Not a huge deal but practice makes perfect. Commented Apr 2, 2013 at 16:45

1 Answer 1

4

The contents of the script block will be executed in another instance of PowerShell.exe (as the job) which won't have access to your variables. This is why you need to send them in the Start-Job argumentlist. Send all the data the job will need to function as an argument. The full path to storage.exe for example, e.g.

$path = (Get-Location).Path
$block = {& $args[0] $args[1] $args[2]}

start-job -scriptblock $block -argumentlist `
    "$path\storage\bin\storage.exe" `
    "-f", `
    "$path\storage\conf\storage.conf"
Sign up to request clarification or add additional context in comments.

2 Comments

I was using this blogs.technet.com/b/heyscriptingguy/archive/2013/05/22/… but I like your solution better. Thanks.
Are you missing a column in your answer?

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.