1

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

2 Answers 2

0

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;
Sign up to request clarification or add additional context in comments.

5 Comments

This too quietly fails.
@Blanthor: Updated post - I think I see a problem with your variable scoping.
I'm a newbie with Powershell I tried declaring var $ScriptBlock and it threw an exception.
Wouldn't $args[0] be the name of the script, CallRemote.ps1? It seems to me that the -ScriptBlock should have the path of the ps1 I'm trying to invoke remotely, hello.ps1.
You're using the call operator & (ampersand), inside the ScriptBlock, to call a PowerShell script file. In order to do this, you need to pass in a String that represents the path to the script file. The way to do that is with the -ArgumentList parameter.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.