1

I know there are other questions here on Stackoverflow regarding this topic, but none of them resolved my case. I have a full path to a given program, examples might be:

  • C:\"Program Files (x86)"\Lorem\program1.exe
  • C:\"Program Files"\Ipsum\program2.exe

If I try to execute it directly on the terminal via C:\"Program Files (x86)"\Lorem\program1.exe /S it will run with no issues, but if I try using my script it won't work:

# $full_path contains the string for the path, e.g. C:\"Program Files (x86)"\supersoftware\uninst.exe

Write-Host $full_path  # Will print on the screen: C:\"Program Files (x86)"\Lorem\uninst.exe
Invoke-Command -ComputerName localhost -ScriptBlock { cmd /c $full_path '"/S"' }  # /S is used for silent uninstall

How can I make PS execute it as if I were there on the terminal typing C:\"Program Files (x86)"\Lorem\uninst.exe /S and hitting enter?

2
  • When using a pathname that contains spaces, you quote the entire pathname, not just the portion that has spaces. Thus, your program should be referenced as "C:\Program Files (x86)\Lorem\Program1.exe", not C:\"Program Files (x86)"\Lorem\Program1.exe Commented Aug 4, 2017 at 13:28
  • Ok, I'm passing it as "C:\..." with quotes outside and not inside. Now I'm getting a new error: '/S' not recognized as an internal or external command Commented Aug 4, 2017 at 13:31

1 Answer 1

2

Because the script block is invoked in a different session the variable will not exist by default. You can use the using scope modifier to get around this.

Invoke-Command -ComputerName ServerName -ScriptBlock { cmd /c $using:full_path /S }

Note that the object will be serialized for remoting. This won't effect a string, but complex objects will typically only contain the original property values.

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

3 Comments

Invoke-Command -ComputerName localhost -ScriptBlock { Start-Process -FilePath "$($using:full_path)" -ArgumentList "/s" }
If it instantly finished running there is a good chance it didn't wait for the process to finish. If the session closes before the process finishes the process will be terminated. Try something like Start-Process -FilePath $using:full_path -ArgumentList '/S' -Wait.
It worked with Invoke-Command -ComputerName ServerName -ScriptBlock { cmd /c $using:full_path /S }, I was checking the wrong place!!!

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.