title: Get-Variable defined in a scriptblock from a psm function
body: <p>I have the following piece of code:</p>
  <pre><code>$x = 'xyz'
  &amp; {
      $y = 'abc'
      foo
  }
  </code></pre>
  <p>The <code>foo</code> function is defined in the <code>foo.psm1</code> module which is imported before the script block is started.</p>
  <p>Inside the <code>foo</code> function, I call <code>Get-Variable</code> which shows me <code>x</code> but it doesn't show <code>y</code>. I tried playing with the <code>-Scope</code> parameter: <code>Local</code>, <code>Script</code>, <code>Global</code>, <code>0</code> - which is the local scope from what I understood from the docs, <code>1</code> - which is the parent scope.</p>
  <p>How could I get the <code>y</code> variable inside the <code>foo</code> function?</p>
  <p>I'm not looking for a solution such as passing it as an argument. I want something as <code>Get-Variable</code> but sadly it doesn't see it for some reason.</p>
  <p><em><strong>UP</strong></em></p>
  <p>Based on the comments received, probably more context is needed.</p>
  <p>Say that foo receives a <code>ScriptBlock</code> which is using the <code>$using:</code> syntax.</p>
  <pre><code>$x = 'xyz'
  &amp; {
      $y = 'abc'
      foo -ScriptBlock {
          Write-Host $using:x
          Write-Host $using:y
      }
  }
  </code></pre>
  <p>I'm 'mining' these variables as follows:</p>
  <pre><code>$usingAsts = $ScriptBlock.Ast.FindAll( { param($ast) $ast -is [System.Management.Automation.Language.UsingExpressionAst] }, $true) | ForEach-Object { $_ -as [System.Management.Automation.Language.UsingExpressionAst] }
  foreach ($usingAst in $usingAsts) {
      $varAst = $usingAst.SubExpression -as [System.Management.Automation.Language.VariableExpressionAst]
      $var = Get-Variable -Name $varAst.VariablePath.UserPath -ErrorAction SilentlyContinue
  }       
  </code></pre>
  <p>This is how I'm using <code>Get-Variable</code> and in the case presented above, <code>y</code> cannot be found.</p>