Jonathan
Jonathan

Reputation: 2855

PowerShell V6 command invocation does not work in script

I run this command script:

$hm = "$Env:USERPROFILE" $eclipse_path = "$hm\eclipse\committers-2019-09\eclipse\eclipse.exe" $sp = { "$eclipse_path -data C:\SharedData\Projects\Tutorial &" Write-Host "Eclipse starting" } Invoke-Command -ScriptBlock $sp

with the following results:

>>> bin/dev.ps1 C:\Users\jgoss\eclipse\committers-2019-09\eclipse\eclipse.exe -data C:\SharedData\Projects\Tutorial & Eclipse starting >>>

It appears that the main command was not executed but the echo command was executed. If I run the main command standalone in Windows Terminal as shown, the command works as desired:

>>> C:\Users\jgoss\eclipse\committers-2019-09\eclipse\eclipse.exe -data C:\SharedData\Projects\Tutorial &

I simply cut and paste the contents of the first command in the script block and it worked as I want. The program eclipse.exe was started in the background. Why does this not work within a script block?

Upvotes: 0

Views: 43

Answers (1)

js2010
js2010

Reputation: 27576

This would get it to work, but there are easier ways to do it. I'm using notepad as an example anyone can reproduce.

$hm = "c:\windows\system32"
$eclipse_path = "$hm\notepad.exe"
$sp = {
  & $eclipse_path c:\users\js\foo\note.ps1
  Write-Host "Eclipse starting" } 
Invoke-Command -ScriptBlock $sp

For example in your $profile you can add the eclipse folder to your path:

$eclipse_path = "$home\eclipse\committers-2019-09\eclipse"
$env:path += $eclipse_path

Then you could simply run eclipse and whatever filename. You could use control-r to search the command history for the last time you ran it.

Upvotes: 1

Related Questions