Use a variable as parameter in a function from a custom endpoint

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Olaf Soyk Olaf Soyk 3 months ago.

  • Author
    Posts
  • #60814
    Profile photo of Kris
    Kris
    Participant

    Hello and merry Christmas to you all.

    I have two machines (MYCLIENT and MYSERVER). The server has a constrained WSMAN endpoint (SessionType RestrictedRemoteServer, LanguageMode NoLanguage, RunAsVirtualAccount, VisibleFunctions Func1 and Func2). It is registered as NameOfTheCustomEndPoint

    In a script I try
    $TestSession = New-PSSession -ComputerName MYSERVER -ConfigurationName NameOfTheCustomEndPoint
    Invoke-Command -Session $TestSession -ScriptBlock {Func1 -Param1 "Value1" -Param2 "Value2"}
    and this works OK

    Invoke-Command -Session $TestSession -ScriptBlock {$Val1 = "Value1"; Func1 -Param1 $Val1 -Param2 "Value2"}
    fails because of the NoLanguage option

    So how do I pass a variable as a parameter value?

    I found one workaround:
    Import-PSSession -Session $TestSession
    $Val1 = "Value1"
    Func1 -Parm1 $Val1 -Param2 "Value2"

    But I have two questions about this:
    1. Is there another solution?
    2. This allows me to execute script code AND use the exported functionality of the custom/constrained endpoint, do I risk someone abusing this (i.e. executing anything else than Func1 and Func2 on MYSERVER)?

  • #60817
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    Ho ho ho ... 😉

    Did you read the help for Invoke-Command including the examples? I think example number 9 could help you. And the explanation of -Argumentlist could help you as well.

    Merry Christmas to you too and a generous Santa Claus 😀

    • #60841
      Profile photo of Kris
      Kris
      Participant

      Hello Olaf,

      I just tested both the $Using syntax of example 9 and the -Argumentslist and they both work.

      $MyVariable = "Value1"
      Invoke-Command -Session $TestSession -Command {Func1 -Param1 $Args[0] -Param2 "Value2"} -ArgumentList @($MyVariable)
      Invoke-Command -Session $TestSession -Command {Func1 -Param1 $Using:MyVariable -Param2 "Value2"}

      (observation: the $Using method works for privileged accounts, if I connect with an account that has only execute permission on the endpoint then only the -Argumentlist method works)

      Thanks,
      Kris.

    • #60844
      Profile photo of Olaf Soyk
      Olaf Soyk
      Participant

      Great ... glad that it was helpful.

      BTW: Your name is not Kris Kringle, right? 😉 😀

You must be logged in to reply to this topic.