title: How do I pass a hashtable to a scriptblock?
body: <p>I tried to pass a hashtable to a scriptblock like this where <code>$arg3</code> is my hashtable. However, it failed. How do I do it in the correct way?</p>
  
  <p>It doesn't seem like it is passing anything to the script block.</p>
  
  <pre><code>$commandParameters.ComputerName = $ComputerName
  
  $commandParameters.ScriptBlock = {
  
          param(
              [Parameter()]
              [switch]$arg1 = $false,
              [Parameter()]
              [array]$arg2,
              [Parameter()]
              [hashtable]$arg3
          )
      enter code here
      Doing something here
  }
  
  Invoke-Command @commandParameters -ArgumentList $arg1, @($arg2), $arg3
  </code></pre>
  
  <p>=================================================================</p>
  
  <p>I found the answer myself and it works for me.  This is how I build the associative array and then pass it to the script block.</p>
  
  <p>I am not sure why, but I was using the dot notation ($hash.a.b) to reference the hash table in a function and it works, but it doesn't work for a script block. It looks like I need to use [ ] (e.g. $hash[a][b])in the script block.</p>
  
  <pre><code>$compADGroups = @{}
  
  foreach ( $adGroup in $adGroups ) {
  
      if ( $compADGroups.$computerNameGroup -eq $null ) {
  
          $compADGroups[$computerName] = @{}
  
          $compADGroups[$computerName]["Group"] = @{}
  
          $compADGroups[$computerName]["Group"] = $hashString
      }
  }
  
  $session = New-PSSession -ComputerName 'Computer1'
  
  Invoke-Command -Session $session -ArgumentList $compADGroups -ScriptBlock { param($compADGroups) $compADGroups[$env:computername]["Group"]}
  
  Get-PSSession | Remove-PSSession
  </code></pre>