Author Posts

July 25, 2016 at 12:59 pm

I am making the following call

.\script.ps1 -value 'value'

This works

Write-Output $value
powershell.exe -ExecutionPolicy Bypass -Command {invoke-command -file \\server\script.ps1 -computername computer -argumentlist "value"}

this does not

powershell.exe -ExecutionPolicy Bypass -Command {invoke-command -file \\server\script.ps1 -computername computer -argumentlist $value}

What am I doing wrong? Why can't I get the parameter to pass though?

Write-Output $value, does issue the proper value, but for some reason it is not liked in the second script. I would think that this should be super simple, but it is driving me mad.

July 25, 2016 at 1:12 pm

When you call out to powershell.exe, all of your variables are basically converted to strings. Since you're already _in_ powershell, why not just call Invoke-Command directly?

invoke-command -file \\server\script.ps1 -computername computer -argumentlist $value

July 25, 2016 at 1:15 pm

It's an issue of scope. Check out the Secrets of Powershell Remoting in the eBooks link above. Basically, you need to pass the variable as using:$value (assuming you are using Powershell V3 or above). Take a look at this as well:

July 25, 2016 at 1:16 pm

That shouldn't matter; he's passing $value to the -ArgumentList parameter, which is fine.

July 25, 2016 at 1:28 pm

I'm wondering, like Dave, why the call to powershell.exe inside of powershell. Aside from that, I don't think you can pass parameters to powershell.exe's -Command script block. You can build a string like so:

powershell.exe -ExecutionPolicy Bypass -Command ""

July 25, 2016 at 1:40 pm

I was using the other command because it was getting me around the

invoke-command : File \\server\script.ps1 cannot be loaded because you have elected to not run this software now.


Setting Set-ExecutionPolicy Bypass on the remote machine doesn't seem to help me get passed not signing the scripts.

Thanks for your help though. It is very much appreciated.

July 25, 2016 at 1:43 pm

That's odd. I didn't think your local ExecutionPolicy should matter to Invoke-Command. I wonder if that's a bug, maybe you can get around it like this:

$scriptContent = Get-Content \\server\script.ps1 -Raw
invoke-command -ScriptBlock $scriptContent -computername computer -argumentlist $value

July 25, 2016 at 2:39 pm

Invoke-Command : Cannot bind parameter 'ScriptBlock'. Cannot convert the "My script contents" value of type "System.String" to type "System.Management.Automation.ScriptBlock".
At E:\script.ps1:9 char:29
+ invoke-command -ScriptBlock $scriptContent -computername server -argumentlist ...
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeCommandCommand

July 25, 2016 at 2:40 pm

For whatever reason the forum ate my snippet and won't let me post it.

  • This reply was modified 2 years ago by  Craig Duff.
  • This reply was modified 2 years ago by  Craig Duff.
  • This reply was modified 2 years ago by  Craig Duff.

July 25, 2016 at 2:46 pm

I don't think you can invoke a script that resides on a UNC path with invoke-command to a remote computer due to Kerberos double hop limitations.

What I've done when I've needed to do this interactively (generally use group policy to do stuff like that):

Copy-Item \\server\script.ps1 \\computer\C$\
Invoke-Command -Computer Computer -FilePath 'C:\script.ps1'

July 25, 2016 at 2:47 pm

oops, forgot that you can't directly cast strings to ScriptBlocks, have to call the Create method:

$scriptContent = Get-Content \\server\script.ps1 -Raw
invoke-command -ScriptBlock ([scriptblock]::Create($scriptContent)) -computername computer -argumentlist $value

July 25, 2016 at 2:55 pm

Dave Wyatt,

I owe you a beer.

Thank you so much for helping me out with this.