Dominik
Dominik

Reputation: 331

Powershell running a command from variable

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.

Upvotes: 3

Views: 10825

Answers (3)

Try Below

Invoke-Command -ScriptBlock {&$command}

Upvotes: 1

Paweł Dyl
Paweł Dyl

Reputation: 9143

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.

Upvotes: 5

guiwhatsthat
guiwhatsthat

Reputation: 2434

Define your variable as a scriptblock.

Like that:

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

Invoke-Command -ScriptBlock $ScriptBlock

Upvotes: 2

Related Questions