Question

Both local and remote machines have PSSession enabled.

I think my problem is that I do not know how to convert and incoming string to a ScriptBlock to be consumed by Invoke-Command. I can invoke all of these commands with an interactive session using Enter-PSSession.

My local script calls the remote script in a script block. I pass the the filename and path on the command line locally with

& '.\CallRemote.ps1' -p "E:\DeployFolder\Scripts\" -f "hello.ps1"

The local script looks like this

Param(
[parameter(Mandatory=$true)]
[alias("p")]
$ScriptPath,
[parameter(Mandatory=$true)]
[alias("f")]
$Scriptfile)
if ($ScriptPath[$ScriptPath.Length - 1] -eq '\')
{
    $ScriptBlock = $ScriptPath + $Scriptfile
}
else
{
    $ScriptBlock = $ScriptPath + '\' + $Scriptfile
}
$appserver = "someurl.com"

$pw = convertto-securestring -AsPlainText -Force -String "password"
$cred = new-object -typename System.Management.Automation.PSCredential -$argumentlist "domain\svc.account",$pw

#initiate remote session for deployment
$session = New-PSSession -ComputerName $appserver -Credential $cred -Name test_remote

#call remote script
Invoke-Command -Session $session -ScriptBlock { $ScriptBlock}
Remove-PSSession -Name test_remote

If I hard-code the Path and Filename prepending a '&' it works. I have found no way to get this to work without hard-coding.

This particular hard-coding works Invoke-Command -Session $session -ScriptBlock { & "E:\DeployFolder\Scripts\hello.ps1"}

These attempts at changing the incoming strings for file and path fail quietly with Invoke-Command -Session $session -ScriptBlock {$ScriptBlock}

  1. $ScriptBlock = "&' " + $ScriptPath + '\' + $Scriptfile + "`'"
  2. $ScriptBlock = "& ' " + $ScriptPath + '\' + $Scriptfile + "'"
  3. $ScriptBlock = "$ScriptPath + '\' + $Scriptfile

This just fails right out Invoke-Command -Session $session -ScriptBlock { & $ScriptBlock} with the error message:

The expression after '&' in a pipeline element produced an invalid object. It must result in a command name, script block or CommandInfo object. + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : BadExpression

No correct solution

OTHER TIPS

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;

Please use:

$cmd = "c:\Programs (x86)\...\command.exe"
Invoke-Command -Session $session -ScriptBlock { & $using:cmd}

For more informations see http://www.padisetty.com/2014/05/all-about-powershell-scriptblock.html.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top