0

How can I invoke a powershell script from within another script? This is not working:

$param1 = "C:/Users/My Folder/file1"
$param2 = "C:/Users/My Folder/file2"
$command = "C:/Users/My Folder/second.ps1"

Invoke-expression $command -File1 $param1 -File2 $param2

... Second.ps1:

param(
[string]File1, [string]File2)...
user3108001
  • 1
  • 1
  • 1

2 Answers2

0

If there are no spaces:

Invoke-expression "$command $param1 $param2"

If you know where the spaces are:

Invoke-expression "$command `$param1HasSpaces` $param2"    

NB: If your execution policy is restricted (check with get-executionpolicy use:

Invoke-Expression "powershell -executionpolicy bypass -command `"$command $param1 $param2`""
JohnLBevan
  • 22,735
  • 13
  • 96
  • 178
  • That gives me: "Invoke-Expression: A positional parameter cannot be found that accepts: 'C:/Users/My Folder/second.ps1 ... – user3108001 Aug 27 '15 at 19:04
0

You can do it like this if you slightly change your approach. Basically create the command string you want to execute, then create a scriptblock object from that and then use Invoke-Command instead of Invoke-Expression.

$param1 = "C:/Users/My Folder/file1"
$param2 = "C:/Users/My Folder/file2"
$command = "C:/Users/My Folder/second.ps1"

$str = '{0} -File1 "{1}" -File2 "{2}"' -f ($command, $param1, $param2)
$sb  =  [scriptblock]::Create($str)

Invoke-Command -ScriptBlock $sb
campbell.rw
  • 1,366
  • 12
  • 22