PowerShell Invoke-Command treating scriptblock as text not script to execute

I am trying to execute a powershell script passing a couple of parameters to it during the execution and for some reason the Invoke-Command appears to be treating what is inside the -ScriptBlock as just text and not a script to be executed.

Lines in script to execute this call
===========================
$AppName = "Stellar"
$CodeVersion = "2001a"
$DeployCmdResources = "F:\DeploymentScripts\deploy-SIDEA.ps1 Prd F:\Dropbox\$AppName\$CodeVersion\Resources"
$remote=New-PSSession -ComputerName $DeployHost
Invoke-Command $remote -ScriptBlock {$DeployCmdResources}

Results when the Invoke-Command is run
===================================
PS C:\Windows\system32> Invoke-Command $remote -ScriptBlock {"F:\DeploymentScripts\deploy-SIDEA.ps1 Prd F:\Dropbox\Stellar\2001a\Resources"}
F:\DeploymentScripts\deploy-SIDEA.ps1 Prd F:\Dropbox\Stellar\2001a\Resources

No errors reported just a return of the scriptblock I am trying to get executed on the remote computer.

Any help debugging this issue would be appreciated.


btw; I am running PowerShell V3 on both side and remoting is working because creating an interactive session and running Get- Process -name notepad work and Execution Policy is set for ByPass
sekoonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
Try these changes.
$DeployCmdResources = {F:\DeploymentScripts\deploy-SIDEA.ps1 Prd F:\Dropbox\$AppName\$CodeVersion\Resources}
$remote=New-PSSession -ComputerName $DeployHost
Invoke-Command $remote -ScriptBlock $DeployCmdResources

Open in new window

0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
"For some reasons ..." - the reason is simply that you push a string into the pipe instead of a command. It is the same if you just say
"F:\DeploymentScripts\deploy-SIDEA.ps1 Prd F:\Dropbox\Stellar\2001a\Resources"

Open in new window

All you will see is the string echoed. As footech showed, you do not need the double quotes here at all, but if you would need them (e.g. because of spaces in the path), you'll have to use the ampersand operator:
Invoke-Command $remote -ScriptBlock { & "F:\DeploymentScripts\deploy-SIDEA.ps1" Prd F:\Dropbox\Stellar\2001a\Resources }

Open in new window

If you have to stick with providing the commands to execute dynamically, using a scriptblock var as shown by footech is one way.
If you need to have a string, e.g. because you need to manipulate the commands by some logic, Invoke-Expression is your friend:
$AppName = "Stellar"
$CodeVersion = "2001a"
$DeployCmdResources = "F:\DeploymentScripts\deploy-SIDEA.ps1 Prd F:\Dropbox\$AppName\$CodeVersion\Resources"
$remote=New-PSSession -ComputerName $DeployHost
Invoke-Command $remote -ScriptBlock { Invoke-Expression $DeployCmdResources }

Open in new window

0
sekoonAuthor Commented:
Tried both examples and the results were that FooTech's returned as if the scriptblock was a string and not a command to execute. Qelmo's example was successful until I replaces the static values with variables then those values were not passed along to the script I was executing and I got the following error;

*** Error: Dropbox Directory: F:\Dropbox\\\Resources does not exist.
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,ValidateDropbox
    + PSComputerName        : aspweb000.stellarhome.internal


I changed the Invoke-Command to the following and got it to execute on the remote by the powershell script that I called to execute returned access error which I am not sure if it was because I was not calling it using a scriptblock or not. I do have WSManSSP one both the client where I am executing this script and on the server was the called script is executing but if it is a Remoting issue then I will open a new question and close this one.

Invoke-Command -FilePath "\\$DeployHost\F$\DeploymentScripts\deploy-VRBO.ps1" -ArgumentList $DeployEnv,$DeployBoxPath -ComputerName $DeployHost

using this as I said the called script was able to see the passed in parameters.
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Footech's code example is correct, so you seem to have made something different for it to fail ...

What's your exact code you used with a scriptblock and dynamic vars?
What's the contents of the argument vars you provided lastly?
The examples show how to construct a static command from variables, and for the scope of this question that looks sufficient.

The access error results from credential issues with Remoting. Whether you run a scriptblock or file remotely doesn't matter; in fact, the -FilePath reads the file locally, builds a scriptblock and sends that to the remote target.
0
sekoonAuthor Commented:
Here is both script  and run time result and it appears that a " between the {} caused the issue with being treated as a string but now pass through and gets executed as the command. But as you can see in the run time both parameters are not being passed into the Deploy-SIDEA.ps1 script when executed.  

Script
======
Clear-Host

$AppName = "Stellar"
$CodeVersion = "2001a"
$DeployHosts = "aspweb000.stellarhome.internal"
$DeployEnv = "prd"
$DeployBoxPath = "\\$DeployHost\F$\Dropbox\$AppName\$CodeVersion\Resources"
$DeployCmdResources = {F:\DeploymentScripts\deploy-SIDEA.ps1 Prd F:\Dropbox\$AppName\$CodeVersion\Resources}

Write-Host "AppNAme: " $AppName
Write-Host "CodeVersion: "$CodeVersion
Write-Host "DeployHost: " $DeployHost
Write-Host "DeployEnv: " $DeployEnv
Write-Host "DeployBoxPath: " $DeployBoxPath
Write-Host "DeployCmdResources: " $DeployCmdResources

Read-Host "Stop review variables"

$remote=New-PSSession -ComputerName $DeployHost
Write-Host "remote: " $remote
Get-PSSession

Read-Host "Check PSSession status then press ENTER, or Ctrl-C top Abort"

Invoke-Command $remote -ScriptBlock $DeployCmdResources





Script runtime output
=====================
AppNAme:  Stellar
CodeVersion:  2001a
DeployHost:  aspweb000.homeaway.live
DeployEnv:  prd
DeployBoxPath:  \\aspweb000.stellarhome.internal\F$\Dropbox\Stellar\2001a\Resources
DeployCmdResources:  F:\DeploymentScripts\deploy-SIDEA.ps1 Prd F:\Dropbox\$AppName\$CodeVersion\Resources
Stop review variables:

remote:  [PSSession]Session6

 Id Name            ComputerName    State         ConfigurationName     Availability
 -- ----            ------------    -----         -----------------     ------------
  6 Session6        aspweb000.st... Opened        Microsoft.PowerShell     Available
Check PSSession status then press ENTER, or Ctrl-C top Abort:


*** Beginning Installation ***
WebFilesType:  WebFilesLive
*** Error: Dropbox Directory: F:\Dropbox\\\Resources does not exist.
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,ValidateDropbox
    + PSComputerName        : stellarhome.internal
 
Exception trapped!
ScriptHalted
At F:\DeploymentScripts\deploy-vrbo.ps1:335 char:3
+         throw Write-Error $errMsg
+         ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], RuntimeException
    + FullyQualifiedErrorId : ScriptHalted
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Ah, now I got you. This is a case where you either need to use a string as command block, or work with script parameters (aka -Argumentlist).
Clear-Host

$AppName = "Stellar"
$CodeVersion = "2001a"
$DeployHosts = "aspweb000.stellarhome.internal"
$DeployEnv = "prd"
$DeployBoxPath = "\\$DeployHost\F$\Dropbox\$AppName\$CodeVersion\Resources"
$DeployCmdResources = "F:\DeploymentScripts\deploy-SIDEA.ps1 Prd F:\Dropbox\$AppName\$CodeVersion\Resources"

Invoke-Command -ComputerName $DeployHost -ScriptBlock { Invoke-Expression $DeployCmdResources }

Open in new window

or
Clear-Host

$AppName = "Stellar"
$CodeVersion = "2001a"
$DeployHosts = "aspweb000.stellarhome.internal"
$DeployEnv = "prd"
$DeployBoxPath = "\\$DeployHost\F$\Dropbox\$AppName\$CodeVersion\Resources"
$DeployCmdResources = {
  param($AppName, $CodeVersion)
  F:\DeploymentScripts\deploy-SIDEA.ps1 Prd F:\Dropbox\$AppName\$CodeVersion\Resources
}

Invoke-Command -ComputerName $DeployHost -ScriptBlock $DeployCmdResources -ArgumentList $AppName, $CodeVersion

Open in new window

The reason for the somewhat cumbersome usage is that the remote script does not have access to the local variables, so you need to either evaluate them prior of sending to remote, or use an argumentlist to provide dynamic vars.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sekoonAuthor Commented:
The Argument method execute properly not showing I have an access issue through PS Remoting that I have to work on but at least not I am getting the server executing the script. The Invoke-Expression method gives me the following error so I will be using the Argument method.

 Cannot bind argument to parameter 'Command' because it is null.
    + CategoryInfo          : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand
    + PSComputerName        : aspweb000.stellarhome.internal
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Stupid me. The Invoke-Expression approach won't work, because the $DeployCmdResources again would be tried to get resolved in the scriptblock - on run time, not compile time.
0
sekoonAuthor Commented:
Sorry that last comment was muffle up with iPad helping my words. The Argument method works fine and now since the server as everything in needs to execute the command I have a access issue with my Test-Path cmdlets in the script I called. But thats another issue so I will close this question out splitting the points and open a new question that I seem bogged down on now.
0
footechCommented:
Another way you can do this, not quite so dynamic but can get you by in a pinch, is to define the variables in the scriptblock which will be executed on the remote computer.
$DeployCmdResources = {
    $AppName = "Stellar"
    $CodeVersion = "2001a"
    F:\DeploymentScripts\deploy-SIDEA.ps1 Prd F:\Dropbox\$AppName\$CodeVersion\Resources
    }
$remote=New-PSSession -ComputerName $DeployHost
Invoke-Command $remote -ScriptBlock $DeployCmdResources

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.