Invoke-Command with multiple parameters

This topic contains 9 replies, has 5 voices, and was last updated by  Kahlan Amnell 2 years, 2 months ago.

  • Author
    Posts
  • #27108

    Kahlan Amnell
    Participant

    This command works for me

    Invoke-Command {param($Param1) C:\test.ps1 -Param1 $Param1 } -ArgumentList $MyParam1

    When I add a new integer parameter to my test.ps1 file, I cannot get it to work. I tried this

    Invoke-Command {param($Param1, $intParam) C:\test.ps1 -Param1 $Param1 -intParam $intParam } -ArgumentList $MyParam1, 1

    What is the correct syntax for this?

  • #27109

    Nathan Hartley
    Participant

    Looks right, according to the help for the -ArgumentList parameter.

    Starting with Powershell version 3, you can also pass local variables by prefacing them with using: . See Example 9.

    Invoke-Command
    https://technet.microsoft.com/library/906b4b41-7da8-4330-9363-e7164e5e6970%28v=wps.630%29.aspx

  • #27132

    Kahlan Amnell
    Participant

    It does not throw an error but it's not passing the integer parameter. I verified by writing to a file both parameters.

  • #27134

    Flynn Bundy
    Participant

    As Nathan said. You should just be able to do this:

     $x = 'Hello'
    Invoke-Command -Computername Blah { Write-Host $using:x } 

    after PowerShell 3.0 its very simple with $using:. You may need to link some of the code you are working with if you are still having trouble.

  • #27135

    Tim Pringle
    Participant

    Hi Kahlan,

    Can you try this and let me know if it works? Check the text file created.

    #requires -Version 2
    $argList = @{
        Param1   = 'whatever that is'
        intParam = 1
    }
    
    Invoke-Command -ComputerName myserver -ScriptBlock {
        param($argList) 
       $argList | out-file -Path c:\windows\temp\myvariables.txt
        C:\test.ps1 -Param1 $argList.Param1 -intParam $argList.intParam
    } -ArgumentList $argList
    
  • #27165

    Kahlan Amnell
    Participant

    It's still not working as expected. The content of the myvariables.txt is fine.
    Name Value
    —- —–
    ProfileTokens whatever that is
    TaskID 1

    But in the PowerShell script file I'm trying to execute, the integer parameter is treated as part of the first parameter as I observed when I printed the values of both parameters. By the way, this is the content of the PS file I'm trying to execute:

    param($ProfileTokens, $TaskID)

    Function Main ($ProfileTokens, $TaskID)
    {
    –printing the values of the parameters
    }

    Main $ProfileTokens, $TaskID

  • #27166

    Kahlan Amnell
    Participant

    I found the culprit, it's the comma inside my PS file which calls the main function. Whew! Thanks for your help!

  • #27167

    Kahlan Amnell
    Participant

    I do have a follow up though on this. How can I specify a variable inside a script block? it seems like it does not properly expand the variables. If I specify

    $Tokens = @{}
    #Tokens is a dictionary, add values here

    $argList = @{
    ProfileTokens = $Tokens
    TaskID = 1
    }
    Invoke-Command -ScriptBlock {param($argList)
    C:\Test.ps1 -ProfileTokens $argList.ProfileTokens -TaskID $argList.TaskID
    } -ArgumentList $argList

    This results to $argList.ProfileTokens to be passed to the PS file as null.

  • #27171

    Rob Simmers
    Participant

    Just do a simple test:

    PS C:\Users\Rob> $test = {}
    
    PS C:\Users\Rob> $test.GetType()
    
    IsPublic IsSerial Name                                     BaseType                                                                                                                                      
    -------- -------- ----                                     --------                                                                                                                                      
    True     True     ScriptBlock                              System.Object                                                                                                                        

    So, you're basically making the Invoke-Command a scriptblock. Try specifying the arguments in the scriptblock with using:

  • #27174

    Kahlan Amnell
    Participant

    I'm not sure I understand. Could you give the sample script?
    I also don't see $using: in the intellisense and it results to an error if I use it. I think I read somewhere that it only works if executed in a remote machine. I'm doing this and invoking the command on a local machine

You must be logged in to reply to this topic.