4

I am attempting to use implicit remoting. We have a jump server in a different data center. I want to run Invoke-Command implicitly on that jump server to be able to run commands on other servers it can access. I am able to export Invoke-Command, and import the module with a prefix. The problem I am running into is that it says it cannot convert a string to a scriptblock.

Here is an example:

Invoke-xxxCommand -ComputerName 'whatever-server' -ScriptBlock {
    Write-Host $env:COMPUTERNAME
}

I understand I can just use a PSSession to connect to the jump server. I am curious why this does not work.

Here is the error:

Cannot bind parameter 'ScriptBlock'. Cannot convert the "Write-Host $env:COMPUTERNAME" value of type "System.String" to type "System.Management.Automation.ScriptBlock".

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
tony
  • 51
  • 8
  • 1
    How do you run a command "implicitly"? What's `Invoke-xxxCommand`, something custom you cooked up? Did you perform spooky black magic on `Invoke-Command` itself and are you now expecting that to work somehow? Judging by the message, *something* is failing with remoting that causes the scripblock to be forwarded as its string representation (which does not roundtrip), but I'm unclear on how this situation came to be. – Jeroen Mostert Jul 28 '17 at 21:54
  • Note that if you're somehow trying to get double-hop remoting to work (that is, from machine A you go to machine B which then executes commands on machine C), you have to get quite a bit more tricky than this. When you remote from A to B, B can execute commands under your identity locally, but it cannot, by default, forward it to machine C. There are solutions to this, but that's best left to other questions. – Jeroen Mostert Jul 28 '17 at 22:10
  • 1
    @JeroenMostert _How do you run a command "implicitly"?_ Here's [a guide on implicit remoting](https://technet.microsoft.com/en-us/library/ff720181.aspx) – BenH Jul 28 '17 at 22:42
  • I believe that this is because everything gets deserialized in the pipe between your session and the remote session. Try using `Invoke-Expression` instead, and see if that works better for you. – TheMadTechnician Jul 28 '17 at 22:48
  • 1
    Possible related to the fact, that `ScriptBlock`s deserialized as strings. – user4003407 Jul 29 '17 at 01:52
  • To add to @PetSerAl's comment: see https://github.com/PowerShell/PowerShell/issues/4218 – mklement0 Jul 29 '17 at 03:03
  • @JeroenMostert - yes, I have the double hop issue taken care of. – tony Jul 29 '17 at 03:04
  • @PetSerAl - I agree that is the problem. Just not sure how to resolve. – tony Jul 29 '17 at 03:04
  • @TheMadTechnician - good idea. I am at home now, but will try again when I am back at work. Thanks!!! – tony Jul 29 '17 at 03:05
  • @BenH - yes, that is exactly what implicit remoting is. Thank you for answering that! – tony Jul 29 '17 at 03:06
  • 1
    Not sure why this got downvoted... – tony Jul 29 '17 at 03:06
  • 1
    @mklement0 - yeah, that seems to be dead on. Thanks! – tony Jul 29 '17 at 14:51
  • @TheMadTechnician - still does not work, even when using Invoke-Expression. Maybe this just cannot be done this way. – tony Jul 31 '17 at 14:28
  • If you dig deeper into the powershell remoting, you will notice that it creates a proxy text based implementation of the module's cmdlets. In essense, it is probing for the cmdlet's contracts, renders to its best the sections of the cmdlet with the only difference being the implementation. Some parameter types cannot be used in this context and you will notice that the type of the `ScriptBlock` has been altered. Try to first `ToString` the script block and then use it as value to the proxy cmdlet. – Alex Sarafian Aug 23 '18 at 10:38

0 Answers0