Passing parameters

This topic contains 11 replies, has 4 voices, and was last updated by  John Wenzel 1 year, 4 months ago.

  • Author
    Posts
  • #48103

    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

    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

    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

    Dave Wyatt
    Moderator

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

  • #48129

    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 ""
    • #48160

      Craig Duff
      Participant

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

      • This reply was modified 1 year, 4 months ago by  Craig Duff.
      • This reply was modified 1 year, 4 months ago by  Craig Duff.
      • This reply was modified 1 year, 4 months ago by  Craig Duff.
  • #48135

    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

    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

    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

  • #48173

    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

    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

    John Wenzel
    Participant

    Dave Wyatt,

    I owe you a beer.

    Thank you so much for helping me out with this.

You must be logged in to reply to this topic.