Passing parameters

This topic contains 11 replies, has 4 voices, and was last updated by Profile photo of John Wenzel John Wenzel 2 months ago.

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #48103
    Profile photo of John Wenzel
    John Wenzel
    Participant

    I am making the following call

    .\script.ps1 -value 'value'

    This works

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

    this does not

    param([string]$value)
    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.

    #48109
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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?

    param([string]$value)
    invoke-command -file \\server\script.ps1 -computername computer -argumentlist $value
    
    #48111
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    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: http://powershell.com/cs/blogs/tips/archive/2012/06/18/running-a-script-block-with-parameters.aspx

    #48113
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

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

    #48129
    Profile photo of Craig Duff
    Craig Duff
    Participant

    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 ""
    #48135
    Profile photo of John Wenzel
    John Wenzel
    Participant

    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.

    message.

    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.

    #48137
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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:

    param([string]$value)
    $scriptContent = Get-Content \\server\script.ps1 -Raw
    invoke-command -ScriptBlock $scriptContent -computername computer -argumentlist $value
    
    #48158
    Profile photo of John Wenzel
    John Wenzel
    Participant

    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

    #48160
    Profile photo of Craig Duff
    Craig Duff
    Participant

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

    • This reply was modified 2 months ago by Profile photo of Craig Duff Craig Duff.
    • This reply was modified 2 months ago by Profile photo of Craig Duff Craig Duff.
    • This reply was modified 2 months ago by Profile photo of Craig Duff Craig Duff.
    #48173
    Profile photo of Craig Duff
    Craig Duff
    Participant

    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'
    #48176
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

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

    param([string]$value)
    $scriptContent = Get-Content \\server\script.ps1 -Raw
    invoke-command -ScriptBlock ([scriptblock]::Create($scriptContent)) -computername computer -argumentlist $value
    
    #48178
    Profile photo of John Wenzel
    John Wenzel
    Participant

    Dave Wyatt,

    I owe you a beer.

    Thank you so much for helping me out with this.

Viewing 12 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic.