3

I am trying to run a command that I have stored as a string in a variable, however when I try to run it with "Invoke-Command" I am told it needs to be "ScriptBlock" and not a string. The command is:

ASIMPORT.EXE -rexactdb-01 -DTEST001 -u -~ I -URL X:\test.xml -Tglentries -OPT18 –Oauto

I am trying to run it as:

Invoke-Command -ScriptBlock $command

Tried with and without "ScriptBlock", always get the same error. Googling it, I honestly do not understand how should I approach the solution, so any advice is appreciated.

3 Answers 3

5

Instead of Invoke-Command, you need Invoke-Expression:

$command = 'svn help patch'
Invoke-expression $command

See cmdlet description:

The Invoke-Expression cmdlet evaluates or runs a specified string as a command and returns the results of the expression or command. Without Invoke-Expression, a string submitted at the command line would be returned (echoed) unchanged.

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

1 Comment

This is the most useful answer, since Invoke-Command does not allow any results to be returned from the command.
2

Define your variable as a scriptblock.

Like that:

$ScriptBlock = [ScriptBlock]::Create("get-process")

Invoke-Command -ScriptBlock $ScriptBlock

Comments

1

Try Below

Invoke-Command -ScriptBlock {&$command}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.