To get anywhere productive, you'll sooner or later create Modules, that invoke Scripts.
You'll hear about 'dot sourcing'…and your pain begins.
Dot Sourcing is the term for “including another script as if it were part of the current script”.
Why it's not simply called include, beats me. But it's like this:
#notice that damn small dot. And the use of $PSCommandRoot that became available in PS3 onwards . "$PSCommandRoot/../LIB/MyScript.ps1"
And it will work. And you'll create a whole bunch of files, which you'll include them all, and then invoke a method…and it will fail. It will simply say that the command is not recognized.
The reason is – wait for it – the dot source ps1 files are only in scope within the function they were dot sourced. Which is fine, until you get to defining *.psm1 functions. If your psm1.function invokes another psm1 function to include ps1 files and methods, the ps1 methods will be available within psm1.function2…but not within psm1.function1 (since psm1 didn't dot source function2).
Holy shit.
But you can fix get around that if you change the function scope. The following will work:
# in Foo.psm1
function Foo(){
Foo2;
#normally fails, because outside of dot source scope
# but this time work as functions are explicitly [script]
# scoped...
return Bar
}
function Foo2(){
. $PSScriptRoot\Bar.ps1
# works because dot source functions are in same scope as this function
Bar
}
# in Tools.ps1
# notice the [script]
function [script] Bar(){
return "Bar!"
}
I'm starting to think that *.psm1's might not be much extra work after all………
Another possible reason is: a script that you invoked earlier than the one you're trying to use has a syntax error. And is jamming up all subsequent scripts.
And you'll get no indication of what it is, or where it is.
Good luck finding it…