Link to home
Start Free TrialLog in
Avatar of sekoon
sekoon

asked on

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
SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
"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

Avatar of sekoon
sekoon

ASKER

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.
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.
Avatar of sekoon

ASKER

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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sekoon

ASKER

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
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.
Avatar of sekoon

ASKER

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.
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