Proper way to pass array to invoke-command script block RRS feed

  • Question

  • Hello,

    I am having a mental block on this one - I wondering if someone could help me with this.  I have a function to remove Windows Features on a remote machine using Invoke-Command.  I setup an array of the features to remove, but when I pass that array to the script block, it only removes the first time in the array.  If I use enter-pssession and manually type the commands in, it works fine.  Here is my code snipit:

     $CmdScriptBlock = {
    			Remove-WindowsFeature -Name $FeatureList
    Invoke-Command -Session $Session -ScriptBlock $CmdScriptBlock -ArgumentList $Features

    Where $Features looks something like this:

    $Features = @("Telnet-Client","RSAT-DNS-Server","RSAT-File-Services","RSAT-DFS-Mgmt-Con","RSAT-ADDS","RSAT-ADDS-Tools","RSAT-AD-PowerShell","RSAT-AD-Tools","GPMC", "RSAT-DHCP")

    I have also tried just declaring the parameter as $FeatureList instead of [string[]]$featurelist.  Its acts the same way.  I am sure there is a simple solution but for some reason I am getting hung up on this.



    • Edited by NJK-Work Thursday, January 7, 2016 7:28 PM
    Thursday, January 7, 2016 7:26 PM


All replies

  • Looks like I may have to be using this for the ArgumentList instead:

    -ArgumentList (,$Features)

    Still not too clear on why I have to do it this way, but I believe this is the solution.  Sorry, I should have done more research before posting my question.



    Thursday, January 7, 2016 8:26 PM
  • I have confirmed this works:

    -ArgumentList (,$Features)


    • Marked as answer by NJK-Work Thursday, January 7, 2016 8:35 PM
    Thursday, January 7, 2016 8:35 PM
  • Hi,

    This is a old Thread, but i have a hint that could help someone

    For me this works:

     $CmdScriptBlock = {
    			Remove-WindowsFeature -Name $Using:FeatureList
    Invoke-Command -Session $Session -ScriptBlock $CmdScriptBlock

    "$Using:Name" takes the value of the local variable, and you can use it.



    Thursday, October 5, 2017 4:29 PM