Passing parameters

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.

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

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:

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

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

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.

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

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: (:slight_smile: [Invoke-Command], ParameterBindingException
    • FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeCommandCommand

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

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'

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

Dave Wyatt,

I owe you a beer.

Thank you so much for helping me out with this.