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}
- $ScriptBlock = "
&
' " + $ScriptPath + '\' + $Scriptfile + "`'" - $ScriptBlock = "&
' " + $ScriptPath + '\' + $Scriptfile + "
'" - $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