powershellpowershell-workflow

Pass arguments to Invoke-Command


This is my script:

workflow Run-RemoteScript {
    Param(
        [Parameter(Mandatory,Position=0)][string[]]$Targets,
        [Parameter(Mandatory,Position=1)][PSCredential]$Credentials,
        [Parameter(Mandatory,Position=1)][String]$Path
    )
    foreach -parallel ($Target in $Targets) {
        parallel {
            "Executing on: $Target"
            InlineScript {
                Invoke-Command -FilePath $using:Path -ComputerName $using:Target -Credential $using:Credentials 
            }
        }
    }
}

This workflow is nested to a function. How do I pass the parameters from my top-level function to the Invoke-Command? Inside the InlineScript{}? $using:MyVar does not seem to work.


Solution

  • Look at the help for Invoke-Command. There are a couple of ways to do this without the additional complication of a workflow. Look at -ThrottleLimit or -InDisconnectedSession parameters for instance. Example 16 is of possible relevance