Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As guys abouve mentioned one of the solution is to convert your script to PS Module.</p> <p>But, as soon as you don't need to dot-source functions in functions to global source (I face that problem and not sure if there is a way to solve it: <a href="https://stackoverflow.com/questions/15187510/dot-sourcing-functions-from-file-to-global-scope-inside-of-function">question</a>) you can solve your issue this way:</p> <p>ScopesA.ps1:</p> <pre><code>$VERSION = "1.0" $overridenFromAntotherFile = "original" # Invoke ScopesB.ps1 via &amp; &amp; .\ScopesB.ps1 Function Write-Version { Write-Host "ScopesA.ps1 version $VERSION" } Write-Version Write-Host $overridenFromAntotherFile </code></pre> <p>ScopesB.ps1:</p> <pre><code>$VERSION = '2.0' $global:overridenFromAntotherFile = 'overriden' function Write-Version { Write-Host "ScopesB.ps1 version $VERSION" } Write-Version </code></pre> <p>Output:</p> <pre><code>ScopesB.ps1 version 2.0 ScopesA.ps1 version 1.0 overriden </code></pre> <p>The idea is to use &amp; invocation instead of dot-sourcing (you can read about them in my <a href="http://personal-code.blogspot.com/2012/08/one-character-drama.html" rel="nofollow noreferrer">article</a> but it is not much to say more than &amp; invokes something without adding it to current scope and . invokes and adds to scope).</p> <p>And still, you can access your global scope from ScopeB.ps1 via scope modifier (this is also mentioned in <a href="http://personal-code.blogspot.com/2012/08/one-character-drama.html" rel="nofollow noreferrer">same article</a> with examples). This is explained with <strong>$overridenFromAntotherFile</strong> variable in script above.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload