问题
I have "ModuleA" that imports functions from modules "ADAutoDeployUtilities" and "Common_LoggingNotifications" using the code below. However, I keep receiving the exception (listed below) when I try to import Common_LoggingNotifications using -force. If I do not load Common_LoggingNotifications, only the ADAutoDeployUtilities functions are imported (which doesn't include writeJobLog) and everything runs fine, although I get the indicated exception when running on the server. I have also provided a screenshot of the output using -verbose. Two things I don't understand from the screenshot; any thoughts?
- Why does "writeJobLog" get removed when I import ADAutoDeployUtilities (writeJobLog doesn't belong to that module)?
- Why is "writeJobLog" not recognized, as it is clearly being imported when Common_LoggingNotifications is imported?
In "ModuleA"...
# Load Module Dependencies
$ModulesPath = $PreCompareInfoObject.ModulesPath # "c:\modules" for example
$ModulesList = "ADAutoDeployUtilities,Common_LoggingNotifications"
$ModulesList.Split(",") | foreach {
if (-not (get-module $ModulesPath$_)) {
#import-module $ModulesPath$_ -ErrorAction SilentlyContinue
import-module $ModulesPath$_ -force -verbose
}
}
# writeJobLog below throws error: The term 'writeJobLog' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again
writeJobLog "xxxxx"
Screenshot after running "ModuleA"...
来源:https://stackoverflow.com/questions/40005664/import-module-with-force-removes-imported-module-function