PowerShell / PowerShell/PowerShell
Import-Module -Scope Local -Force in nested scripts removes the module from outer scripts
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- C#
- Sterne
- 55.5k
- Forks
- 8.5k
- Ø Merge
- 1 T. 2 Std.
- Gemergte PRs (30 T.)
- 88
Beschreibung
Prerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
When using Import-Module -Scope Local in nested scripts, the module gets removed from the calling script after calling a script which also contains Import-Module -Scope Local for the same module.
MyModule.psm1 contents:
function Test-Something
{
Write-Host "Test-Something called"
}
OuterScript.ps1 contents:
import-module -Scope Local -Force .\MyModule.psm1
Test-Something
.\InnerScript.ps1
Test-Something
InnerScript.ps1 contents:
import-module -Scope Local -Force .\MyModule.psm1
Write-Host "In InnerScript.ps1, module imported."
Expected behavior
PS D:\Source\Powershell\ImportModuleRepro> .\OuterScript.ps1
Test-Something called
In InnerScript.ps1, module imported.
Test-Something called
Actual behavior
PS D:\Source\Powershell\ImportModuleRepro> .\OuterScript.ps1
Test-Something called
In InnerScript.ps1, module imported.
Test-Something: D:\Source\Powershell\ImportModuleRepro\OuterScript.ps1:7
Line |
7 | Test-Something
| ~~~~~~~~~~~~~~
| The term 'Test-Something' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct
| and try again.
Error details
PS D:\Source\Powershell\ImportModuleRepro> get-error
Exception :
Type : System.Management.Automation.CommandNotFoundException
ErrorRecord :
Exception :
Type : System.Management.Automation.ParentContainsErrorRecordException
Message : The term 'Test-Something' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
HResult : -2146233087
TargetObject : Test-Something
CategoryInfo : ObjectNotFound: (Test-Something:String) [], ParentContainsErrorRecordException
FullyQualifiedErrorId : CommandNotFoundException
InvocationInfo :
ScriptLineNumber : 7
OffsetInLine : 1
HistoryId : 9
ScriptName : D:\Source\Powershell\ImportModuleRepro\OuterScript.ps1
Line : Test-Something
PositionMessage : At D:\Source\Powershell\ImportModuleRepro\OuterScript.ps1:7 char:1
+ Test-Something
+ ~~~~~~~~~~~~~~
PSScriptRoot : D:\Source\Powershell\ImportModuleRepro
PSCommandPath : D:\Source\Powershell\ImportModuleRepro\OuterScript.ps1
InvocationName : Test-Something
CommandOrigin : Internal
ScriptStackTrace : at <ScriptBlock>, D:\Source\Powershell\ImportModuleRepro\OuterScript.ps1: line 7
at <ScriptBlock>, <No file>: line 1
CommandName : Test-Something
TargetSite :
Name : LookupCommandInfo
DeclaringType : System.Management.Automation.CommandDiscovery, System.Management.Automation, Version=7.3.6.500, Culture=neutral, PublicKeyToken=31bf3856ad364e35
MemberType : Method
Module : System.Management.Automation.dll
Message : The term 'Test-Something' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Data : System.Collections.ListDictionaryInternal
Source : System.Management.Automation
HResult : -2146233087
StackTrace :
at System.Management.Automation.CommandDiscovery.LookupCommandInfo(String commandName, CommandTypes commandTypes, SearchResolutionOptions searchResolutionOptions, CommandOrigin commandOrigin,
ExecutionContext context)
at System.Management.Automation.ExecutionContext.CreateCommand(String command, Boolean dotSource)
at System.Management.Automation.PipelineOps.AddCommand(PipelineProcessor pipe, CommandParameterInternal[] commandElements, CommandBaseAst commandBaseAst, CommandRedirection[] redirections, ExecutionContext
context)
at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][]
commandRedirections, FunctionContext funcContext)
at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
TargetObject : Test-Something
CategoryInfo : ObjectNotFound: (Test-Something:String) [], CommandNotFoundException
FullyQualifiedErrorId : CommandNotFoundException
InvocationInfo :
ScriptLineNumber : 7
OffsetInLine : 1
HistoryId : 9
ScriptName : D:\Source\Powershell\ImportModuleRepro\OuterScript.ps1
Line : Test-Something
PositionMessage : At D:\Source\Powershell\ImportModuleRepro\OuterScript.ps1:7 char:1
+ Test-Something
+ ~~~~~~~~~~~~~~
PSScriptRoot : D:\Source\Powershell\ImportModuleRepro
PSCommandPath : D:\Source\Powershell\ImportModuleRepro\OuterScript.ps1
InvocationName : Test-Something
CommandOrigin : Internal
ScriptStackTrace : at <ScriptBlock>, D:\Source\Powershell\ImportModuleRepro\OuterScript.ps1: line 7
at <ScriptBlock>, <No file>: line 1
Environment data
PS D:\Source\Powershell\ImportModuleRepro> $PSVersionTable
Name Value
---- -----
PSVersion 7.3.6
PSEdition Core
GitCommitId 7.3.6
OS Microsoft Windows 10.0.19045
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Visuals
No response
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit der Ausführung der gemeldeten Reproduktion mit OuterScript.ps1, InnerScript.ps1 und MyModule.psm1 unter Verwendung von Import-Module -Scope Local -Force. Verfolge, wie sich der verschachtelte Import auf die Verfügbarkeit von Befehlen im äußeren Skript auswirkt. Als abgeschlossen gilt die Aufgabe, wenn der abschließende Aufruf von Test-Something erfolgreich ist, das gemeldete Verhalten des inneren Skripts beibehalten wird und für dieses Szenario eine Regressionstestabdeckung hinzugefügt wurde.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- powershell
- Bereich
- cli
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100