Function disappears after Import-Module
I have a problem with Import-Module.
I have a psm1 file which exports function “Write-FromWithinPsmFile“. This psm1 is imported into two ps1 files using Import-Module.
Also the main ps1 files calls the other ps1 file.
Now I found two cases, where calling the function “Write-FromWithinPsmFile” works fine and the other way shows some confusing result, which I cannot explain. The function “disappears” after the main script executes the second script. But only in case it executes the second script from within a function.
This is the Test.psm1 file:
function Write-FromWithinPsmFile($Text)
{
Write-Host “code in Test.psm1: $Text”
}
Export-ModuleMember -Function Write-FromWithinPsmFile
This is the script Start.ps1:
param (
[Parameter()]
[switch]
$WillFail
)
Import-Module “C:tmpTest.psm1” -Scope Local -Force -ErrorAction Stop
function Invoke-ScriptFromFunction
{
. “C:tmpCalledScript_repro.ps1”
}
Write-FromWithinPsmFile “called from Start.ps1 – first time”
if ($WillFail)
{
# execute the script from within a function
Invoke-ScriptFromFunction
}
else
{
# execute the script directly
. “C:tmpCalledScript_repro.ps1”
}
Write-FromWithinPsmFile “called from Start.ps1 – second time”
And this is the CalledScript_repro.ps1, which is called by the main script:
Import-Module “C:tmpTest.psm1” -Scope Local -ErrorAction Stop -Force
# do something …..
This is the successfull execution: powershell -File C:tmpStart.ps1 -Verbose
This is the failing one: powershell -File C:tmpStart.ps1 -WillFail -Verbose
It tells me:
Write-FromWithinPsmFile : The term ‘Write-FromWithinPsmFile’ is not recognized as the name of a
cmdlet, function, script file, or operable program.
Both calls will succeed, in case I change the CalledScript_repro.ps1 to this (I removed the -Force):
Import-Module “C:tmpTest.psm1” -Scope Local -ErrorAction Stop
# do something …..
Some time ago I added the -Force, because it is more convenient when debugging and writing the code.
So my question is:
What am I doing wrong? Do I need to do the Remove-Module explicitly? How should I use the Import-Module?
I have a problem with Import-Module.I have a psm1 file which exports function “Write-FromWithinPsmFile”. This psm1 is imported into two ps1 files using Import-Module.Also the main ps1 files calls the other ps1 file.Now I found two cases, where calling the function “Write-FromWithinPsmFile” works fine and the other way shows some confusing result, which I cannot explain. The function “disappears” after the main script executes the second script. But only in case it executes the second script from within a function. This is the Test.psm1 file: function Write-FromWithinPsmFile($Text)
{
Write-Host “code in Test.psm1: $Text”
}
Export-ModuleMember -Function Write-FromWithinPsmFile This is the script Start.ps1: param (
[Parameter()]
[switch]
$WillFail
)
Import-Module “C:tmpTest.psm1” -Scope Local -Force -ErrorAction Stop
function Invoke-ScriptFromFunction
{
. “C:tmpCalledScript_repro.ps1”
}
Write-FromWithinPsmFile “called from Start.ps1 – first time”
if ($WillFail)
{
# execute the script from within a function
Invoke-ScriptFromFunction
}
else
{
# execute the script directly
. “C:tmpCalledScript_repro.ps1”
}
Write-FromWithinPsmFile “called from Start.ps1 – second time” And this is the CalledScript_repro.ps1, which is called by the main script: Import-Module “C:tmpTest.psm1” -Scope Local -ErrorAction Stop -Force
# do something ….. This is the successfull execution: powershell -File C:tmpStart.ps1 -VerboseThis is the failing one: powershell -File C:tmpStart.ps1 -WillFail -VerboseIt tells me:Write-FromWithinPsmFile : The term ‘Write-FromWithinPsmFile’ is not recognized as the name of acmdlet, function, script file, or operable program.Both calls will succeed, in case I change the CalledScript_repro.ps1 to this (I removed the -Force): Import-Module “C:tmpTest.psm1” -Scope Local -ErrorAction Stop
# do something ….. Some time ago I added the -Force, because it is more convenient when debugging and writing the code.So my question is:What am I doing wrong? Do I need to do the Remove-Module explicitly? How should I use the Import-Module? Read More