PowerShell crashes when using point access scripts

I am having some scope issues when powershell scripts are for point sources. Suppose I have one script 'A.ps1':

$VERSION = "1.0" # Dot source B.ps1 . .\B.ps1 function Write-Version { Write-Host "A.ps1 version $VERSION" } Write-Version 

And script B.ps1

 $VERSION = "2.0" function Write-Version { Write-Host "B.ps1 version $VERSION" } Write-Version 

The result of A.ps1 will be:

 B.ps1 version 2.0 A.ps1 version 2.0 

Why this happens is obvious. The $VERSION variable from B.ps1 is placed in A.ps1 and overwrites this variable. In fact, this also happens with Write-Version , but here A.ps1 overwrites the B-version, but since Write-Version is called in B.ps1 before this happens, we can still see the output of the B Write-Version function .

The question, of course, is how to prevent this? I tried various options for features, but this doesn't seem to work when dot-sourcing. And since B.ps1 has the functions that I need in area A, just calling B.ps1 is probably not an option.

Does anyone have any ideas?

+4
source share
3 answers

You can do this by making the B.ps1 module and renaming it B.psm1. Add an Export-ModuleMember so your functions are accessible to other scripts.

This will be B.psm1:

 $VERSION = "2.0" function Write-Version { Write-Host "B.ps1 version $VERSION" } Write-Version # Only items specified here will be exported. If Export-ModuleMember is not used, # ALL members (functions, variables, and aliases) will be exported. In this case # if $VERSION was exported, $VERSION will be set to "2.0" in script A.ps1 Export-ModuleMember -Function Write-Version 

And A.ps1 will be:

 $VERSION = "1.0" # Import B.psm1 Import-Module .\B.psm1 function Write-Version { Write-Host "A.ps1 version $VERSION" } Write-Version # Use B.psm1 `Write-Version` function B\Write-Version 
+1
source

Modules were created in Powershell V2 to solve these problems using a point source. Save the script with the extension psm1 and use the Import-Module cmdlet instead of the point source code in the code.

+2
source

As the guys noted, one solution is to convert your script module to PS.

But, as soon as you don’t need the dot-source functions in the global source functions (I encounter this problem and am not sure if there is a way to solve it: question ), you can solve this problem as follows:

ScopesA.ps1:

 $VERSION = "1.0" $overridenFromAntotherFile = "original" # Invoke ScopesB.ps1 via & & .\ScopesB.ps1 Function Write-Version { Write-Host "ScopesA.ps1 version $VERSION" } Write-Version Write-Host $overridenFromAntotherFile 

ScopesB.ps1:

 $VERSION = '2.0' $global:overridenFromAntotherFile = 'overriden' function Write-Version { Write-Host "ScopesB.ps1 version $VERSION" } Write-Version 

Output:

 ScopesB.ps1 version 2.0 ScopesA.ps1 version 1.0 overriden 

The idea is to use and call instead of dot-sourcing (you can read about them in my article but it will not say much more and cause something without adding it to the current scope and call and add to the scope).

And yet you can access your global scope from ScopeB.ps1 using the scope modifier (this is also mentioned in the same article with examples) This is explained using the $ overridenFromAntotherFile variable in the script above.

0
source

Source: https://habr.com/ru/post/1386522/


All Articles