Invoke-Command with multiple parameters

This topic contains 9 replies, has 5 voices, and was last updated by Profile photo of Kahlan Amnell Kahlan Amnell 1 year, 3 months ago.

  • Author
  • #27108
    Profile photo of Kahlan Amnell
    Kahlan Amnell

    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
    Profile photo of Nathan Hartley
    Nathan Hartley

    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.


  • #27132
    Profile photo of Kahlan Amnell
    Kahlan Amnell

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

  • #27134
    Profile photo of Flynn Bundy
    Flynn Bundy

    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
    Profile photo of Tim Pringle
    Tim Pringle

    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 {
       $argList | out-file -Path c:\windows\temp\myvariables.txt
        C:\test.ps1 -Param1 $argList.Param1 -intParam $argList.intParam
    } -ArgumentList $argList
  • #27165
    Profile photo of Kahlan Amnell
    Kahlan Amnell

    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
    Profile photo of Kahlan Amnell
    Kahlan Amnell

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

  • #27167
    Profile photo of Kahlan Amnell
    Kahlan Amnell

    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
    Profile photo of Rob Simmers
    Rob Simmers

    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
    Profile photo of Kahlan Amnell
    Kahlan Amnell

    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.