You can create a ScriptBlock
from a String
by using the static Create()
method.
$ScriptPath = 'c:\test';
$ScriptFile = 'test.ps1';
$ScriptBlock = [ScriptBlock]::Create("$ScriptPath\$ScriptFile");
...
...
The other issue I see with what you're trying to do is that you're using the $ScriptBlock
variable inside of the ScriptBlock
that you're sending to the remote computer. Unless that variable is getting defined elsewhere, you are not going to be able to pass parameters that way. You will need to use the $args
automatic variable instead.
# This file must exist on the remote filesystem
$ScriptFile = 'c:\test\test.ps1';
# Invoke the script file on the remote system
Invoke-Command -Session $Session -ScriptBlock { & $args[0]; } -ArgumentList $ScriptFile;