Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

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;
share|improve this answer
    
This too quietly fails. –  Blanthor Feb 17 at 17:36
    
@Blanthor: Updated post - I think I see a problem with your variable scoping. –  Trevor Sullivan Feb 17 at 17:39
    
I'm a newbie with Powershell I tried declaring var $ScriptBlock and it threw an exception. –  Blanthor Feb 17 at 17:52
    
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. –  Blanthor Feb 17 at 21:22
    
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. –  Trevor Sullivan Feb 17 at 21:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.